feature(cloudcmd) add plugins

This commit is contained in:
coderaiser 2016-11-28 18:21:13 +02:00
parent 39934033b8
commit 4d14071ede
8 changed files with 186 additions and 7 deletions

View file

@ -14,7 +14,9 @@ const {assign} = Object;
const pathConfig = os.homedir() + '/.cloudcmd.json';
const currentConfig = readjson.sync.try(pathConfig);
module.exports = (config, fn = config) => {
module.exports = (_config, _plugins, _fn) => {
const {config, plugins, fn} = parse(_config, _plugins, _fn);
const app = express();
const server = http.createServer(app);
const after = () => {
@ -28,6 +30,7 @@ module.exports = (config, fn = config) => {
app.use(cloudcmd({
socket,
plugins,
config: assign(defaultConfig(), config)
}));
@ -43,3 +46,25 @@ function defaultConfig() {
};
}
function parse(config, plugins, fn) {
if (typeof plugins === 'undefined')
return {
fn: config,
config: undefined,
plugins: undefined
}
if (typeof fn === 'undefined')
return {
config,
fn: plugins,
plugins: undefined
}
return {
config,
plugins,
fn
};
}

21
test/lib/cloudcmd.js Normal file
View file

@ -0,0 +1,21 @@
'use strict';
const test = require('tape');
const cloudcmd = require('../..');
test('cloudcmd: args: no', (t) => {
const fn = () => cloudcmd();
t.doesNotThrow(fn, /plugins should be an array!/, 'should throw when plugins not an array');
t.end();
});
test('cloudcmd: args: plugins: error', (t) => {
const fn = () => cloudcmd({
plugins: ''
});
t.throws(fn, /plugins should be an array!/, 'should throw when plugins not an array');
t.end();
});

82
test/plugins.js Normal file
View file

@ -0,0 +1,82 @@
'use strict';
const fs = require('fs');
const test = require('tape');
const promisify = require('es6-promisify');
const pullout = require('pullout');
const request = require('request');
const before = require('./before');
const warp = (fn, ...a) => (...b) => fn(...b, ...a);
const _pullout = promisify(pullout);
const get = promisify((url, fn) => {
fn(null, request(url));
});
test('cloudcmd: plugins', (t) => {
const config = {};
const plugins = [];
before(config, plugins, (port, after) => {
get(`http://localhost:${port}/plugins.js`)
.then(warp(_pullout, 'string'))
.then((content) => {
t.equal(content, '', 'should content be empty');
t.end();
after();
})
.catch((error) => {
console.log(error);
});
});
});
test('cloudcmd: plugins', (t) => {
const config = {};
const plugins = [
__filename
];
before(config, plugins, (port, after) => {
get(`http://localhost:${port}/plugins.js`)
.then(warp(_pullout, 'string'))
.then((content) => {
const file = fs.readFileSync(__filename, 'utf8');
t.equal(content, file, 'should return file plugin content');
t.end();
after();
})
.catch((error) => {
console.log(error);
});
});
});
test('cloudcmd: plugins: load error', (t) => {
const config = {};
const noEntry = __filename + Math.random();
const plugins = [
__filename,
noEntry
];
const msg = `ENOENT: no such file or directory, open '${noEntry}'`;
before(config, plugins, (port, after) => {
get(`http://localhost:${port}/plugins.js`)
.then(warp(_pullout, 'string'))
.then((content) => {
const file = fs.readFileSync(__filename, 'utf8') + msg;
t.equal(content, file, 'should return file plugin content');
t.end();
after();
})
.catch((error) => {
console.log(error);
});
});
});