Skip to content

Commit 4d14071

Browse files
committed
feature(cloudcmd) add plugins
1 parent 3993403 commit 4d14071

File tree

8 files changed

+186
-7
lines changed

8 files changed

+186
-7
lines changed

HELP.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,14 @@ const config = {
335335
prefix /* base URL or function which returns base URL (optional) */
336336
};
337337

338+
const plugins = [
339+
__dirname + '/plugin.js'
340+
];
341+
338342
app.use(cloudcmd({
339-
socket, /* used by Config, Edit (optional) and Console (required) */
340-
config, /* config data (optional) */
343+
socket, /* used by Config, Edit (optional) and Console (required) */
344+
config, /* config data (optional) */
345+
plugins, /* optional */
341346
}));
342347

343348
server.listen(port);

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,14 @@ const config = {
8989
prefix /* base URL or function which returns base URL (optional) */
9090
};
9191

92+
const plugins= [
93+
__dirname + '/plugin.js'
94+
];
95+
9296
app.use(cloudcmd({
93-
socket, /* used by Config, Edit (optional) and Console (required) */
94-
config, /* config data (optional) */
97+
socket, /* used by Config, Edit (optional) and Console (required) */
98+
config, /* config data (optional) */
99+
plugins, /* optional */
95100
}));
96101

97102
server.listen(port);

lib/client.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ var Util, DOM, CloudFunc, join;
157157
Util.exec.series([
158158
initModules,
159159
baseInit,
160+
loadPlugins,
160161
function() {
161162
CloudCmd.route(location.hash);
162163
}
@@ -194,6 +195,13 @@ var Util, DOM, CloudFunc, join;
194195
Util.exec.if(document.body.scrollIntoViewIfNeeded, func, funcBefore);
195196
};
196197

198+
function loadPlugins(callback) {
199+
var prefix = CloudCmd.PREFIX;
200+
var plugins = prefix + '/plugins.js';
201+
202+
DOM.load.js(plugins, callback);
203+
}
204+
197205
this.join = function(urls) {
198206
var prefix = CloudCmd.PREFIX;
199207

lib/cloudcmd.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var rest = require(DIR_SERVER + 'rest');
1212
var route = require(DIR_SERVER + 'route');
1313
var validate = require(DIR_SERVER + 'validate');
1414
var prefixer = require(DIR_SERVER + 'prefixer');
15+
var pluginer = require(DIR_SERVER + 'plugins');
1516

1617
var apart = require('apart');
1718
var join = require('join-io');
@@ -50,9 +51,13 @@ function getPrefix(prefix) {
5051
module.exports = function(params) {
5152
var p = params || {};
5253
var options = p.config || {};
54+
var plugins = p.plugins;
55+
5356
var keys = Object.keys(options);
5457
var prefix;
5558

59+
checkPlugins(plugins);
60+
5661
keys.forEach(function(name) {
5762
var value = options[name];
5863

@@ -84,7 +89,7 @@ module.exports = function(params) {
8489
if (p.socket)
8590
listen(prefix, p.socket);
8691

87-
return cloudcmd(prefix);
92+
return cloudcmd(prefix, plugins);
8893
};
8994

9095
function defaultTrue(value) {
@@ -169,7 +174,7 @@ function listen(prefix, socket) {
169174
});
170175
}
171176

172-
function cloudcmd(prefix) {
177+
function cloudcmd(prefix, plugins) {
173178
var isOption = function(name) {
174179
return config(name);
175180
};
@@ -260,6 +265,7 @@ function cloudcmd(prefix) {
260265
is : isMinify
261266
}),
262267

268+
pluginer(plugins),
263269
ponseStatic
264270
];
265271

@@ -290,3 +296,10 @@ function setUrl(pref) {
290296
};
291297
}
292298

299+
function checkPlugins(plugins) {
300+
if (typeof plugins === 'undefined')
301+
return;
302+
303+
if (!Array.isArray(plugins))
304+
throw Error('plugins should be an array!');
305+
}

lib/server/plugins.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
var currify = require('currify/legacy');
4+
var files = require('files-io');
5+
6+
module.exports = currify(function(plugins, req, res, next) {
7+
if (req.url !== '/plugins.js')
8+
return next();
9+
10+
if (!plugins || !plugins.length)
11+
return res.send('');
12+
13+
files.readPipe(plugins, res, (e) => {
14+
if (!e)
15+
return;
16+
17+
res.end(e.message);
18+
});
19+
});
20+

test/before.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ const {assign} = Object;
1414
const pathConfig = os.homedir() + '/.cloudcmd.json';
1515
const currentConfig = readjson.sync.try(pathConfig);
1616

17-
module.exports = (config, fn = config) => {
17+
module.exports = (_config, _plugins, _fn) => {
18+
const {config, plugins, fn} = parse(_config, _plugins, _fn);
19+
1820
const app = express();
1921
const server = http.createServer(app);
2022
const after = () => {
@@ -28,6 +30,7 @@ module.exports = (config, fn = config) => {
2830

2931
app.use(cloudcmd({
3032
socket,
33+
plugins,
3134
config: assign(defaultConfig(), config)
3235
}));
3336

@@ -43,3 +46,25 @@ function defaultConfig() {
4346
};
4447
}
4548

49+
function parse(config, plugins, fn) {
50+
if (typeof plugins === 'undefined')
51+
return {
52+
fn: config,
53+
config: undefined,
54+
plugins: undefined
55+
}
56+
57+
if (typeof fn === 'undefined')
58+
return {
59+
config,
60+
fn: plugins,
61+
plugins: undefined
62+
}
63+
64+
return {
65+
config,
66+
plugins,
67+
fn
68+
};
69+
}
70+

test/lib/cloudcmd.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const test = require('tape');
4+
const cloudcmd = require('../..');
5+
6+
test('cloudcmd: args: no', (t) => {
7+
const fn = () => cloudcmd();
8+
9+
t.doesNotThrow(fn, /plugins should be an array!/, 'should throw when plugins not an array');
10+
t.end();
11+
});
12+
13+
test('cloudcmd: args: plugins: error', (t) => {
14+
const fn = () => cloudcmd({
15+
plugins: ''
16+
});
17+
18+
t.throws(fn, /plugins should be an array!/, 'should throw when plugins not an array');
19+
t.end();
20+
});
21+

test/plugins.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const test = require('tape');
5+
const promisify = require('es6-promisify');
6+
const pullout = require('pullout');
7+
const request = require('request');
8+
9+
const before = require('./before');
10+
11+
const warp = (fn, ...a) => (...b) => fn(...b, ...a);
12+
13+
const _pullout = promisify(pullout);
14+
15+
const get = promisify((url, fn) => {
16+
fn(null, request(url));
17+
});
18+
19+
test('cloudcmd: plugins', (t) => {
20+
const config = {};
21+
const plugins = [];
22+
23+
before(config, plugins, (port, after) => {
24+
get(`http://localhost:${port}/plugins.js`)
25+
.then(warp(_pullout, 'string'))
26+
.then((content) => {
27+
t.equal(content, '', 'should content be empty');
28+
t.end();
29+
after();
30+
})
31+
.catch((error) => {
32+
console.log(error);
33+
});
34+
});
35+
});
36+
37+
test('cloudcmd: plugins', (t) => {
38+
const config = {};
39+
const plugins = [
40+
__filename
41+
];
42+
43+
before(config, plugins, (port, after) => {
44+
get(`http://localhost:${port}/plugins.js`)
45+
.then(warp(_pullout, 'string'))
46+
.then((content) => {
47+
const file = fs.readFileSync(__filename, 'utf8');
48+
t.equal(content, file, 'should return file plugin content');
49+
t.end();
50+
after();
51+
})
52+
.catch((error) => {
53+
console.log(error);
54+
});
55+
});
56+
});
57+
58+
test('cloudcmd: plugins: load error', (t) => {
59+
const config = {};
60+
const noEntry = __filename + Math.random();
61+
const plugins = [
62+
__filename,
63+
noEntry
64+
];
65+
66+
const msg = `ENOENT: no such file or directory, open '${noEntry}'`;
67+
68+
before(config, plugins, (port, after) => {
69+
get(`http://localhost:${port}/plugins.js`)
70+
.then(warp(_pullout, 'string'))
71+
.then((content) => {
72+
const file = fs.readFileSync(__filename, 'utf8') + msg;
73+
t.equal(content, file, 'should return file plugin content');
74+
t.end();
75+
after();
76+
})
77+
.catch((error) => {
78+
console.log(error);
79+
});
80+
});
81+
});
82+

0 commit comments

Comments
 (0)