-
-
Notifications
You must be signed in to change notification settings - Fork 259
/
load-module.js
61 lines (44 loc) · 1.46 KB
/
load-module.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
/* global CloudCmd */
const exec = require('execon');
const tryToCatch = require('try-to-catch/legacy');
const {promisify} = require('es6-promisify');
const loadJS = promisify(require('load.js').js);
const {
kebabToCamelCase,
} = require('../common/util');
/**
* function load modules
* @params = {name, path, func, dobefore, arg}
*/
module.exports = function loadModule(params) {
if (!params)
return;
const {path} = params;
const name = params.name || path && kebabToCamelCase(path);
const doBefore = params.dobefore;
if (CloudCmd[name])
return;
CloudCmd[name] = () => {
exec(doBefore);
const prefix = CloudCmd.PREFIX;
const pathFull = prefix + CloudCmd.DIRCLIENT_MODULES + path + '.js';
return loadJS(pathFull).then(async () => {
const newModule = async (f) => f && f();
const module = CloudCmd[name];
Object.assign(newModule, module);
CloudCmd[name] = newModule;
CloudCmd.log('init', name);
await module.init();
return newModule;
});
};
CloudCmd[name].show = async (...args) => {
CloudCmd.log('show', name, args);
const m = CloudCmd[name];
const [e, a] = await tryToCatch(m);
if (e)
return console.error(e);
a.show(...args);
};
};