mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-18 00:47:01 +00:00
feature(load-module) add
This commit is contained in:
parent
146b32b215
commit
4b528280ba
24 changed files with 1352 additions and 1491 deletions
53
client/load-module.js
Normal file
53
client/load-module.js
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
'use strict';
|
||||
|
||||
/* global CloudCmd */
|
||||
|
||||
const exec = require('execon');
|
||||
const tryToCatch = require('try-to-catch/legacy');
|
||||
const {
|
||||
kebabToCamelCase,
|
||||
} = require('../common/util');
|
||||
|
||||
/**
|
||||
* function load modules
|
||||
* @params = {name, path, func, dobefore, arg}
|
||||
*/
|
||||
module.exports = function loadModule(params) {
|
||||
if (!params)
|
||||
return;
|
||||
|
||||
let path = params.path;
|
||||
const name = params.name || path && kebabToCamelCase(path);
|
||||
const doBefore = params.dobefore;
|
||||
|
||||
if (CloudCmd[name])
|
||||
return;
|
||||
|
||||
CloudCmd[name] = () => {
|
||||
exec(doBefore);
|
||||
return import(`./modules/${path}` /* webpackChunkName: "cloudcmd-" */).then(async (module) => {
|
||||
const newModule = async (f) => f && f();
|
||||
|
||||
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);
|
||||
};
|
||||
};
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue