mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-29 02:30:28 +00:00
feature: client: load-module: migrate to ESM
This commit is contained in:
parent
9950cacad9
commit
917f585137
2 changed files with 8 additions and 8 deletions
58
client/load-module.mjs
Normal file
58
client/load-module.mjs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/* global CloudCmd */
|
||||
import exec from 'execon';
|
||||
import {tryToCatch} from 'try-to-catch';
|
||||
import {js} from 'load.js';
|
||||
import pascalCase from 'just-pascal-case';
|
||||
|
||||
const loadJS = js;
|
||||
const noJS = (a) => a.replace(/.js$/, '');
|
||||
|
||||
/**
|
||||
* function load modules
|
||||
* @params = {name, path, func, dobefore, arg}
|
||||
*/
|
||||
export const loadModule = (params) => {
|
||||
if (!params)
|
||||
return;
|
||||
|
||||
const {path} = params;
|
||||
|
||||
const name = path && noJS(pascalCase(path));
|
||||
const doBefore = params.dobefore;
|
||||
|
||||
if (CloudCmd[name])
|
||||
return;
|
||||
|
||||
CloudCmd[name] = async () => {
|
||||
exec(doBefore);
|
||||
|
||||
const {DIR_MODULES} = CloudCmd;
|
||||
const pathFull = `${DIR_MODULES}/${path}.js`;
|
||||
|
||||
await loadJS(pathFull);
|
||||
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);
|
||||
|
||||
return await a.show(...args);
|
||||
};
|
||||
};
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue