@helios-chain-labs/helios-cli
Version:
simple CLI for helios node
39 lines (31 loc) • 1.15 kB
JavaScript
const fs = require('fs');
const path = require('path');
function run(argv, fn) {
const command = argv.command;
const currentDir = __dirname;
const npmNodeModulesGlobalDir = path.join(currentDir, '..');
processCommand(command, {
argv,
pwd: process.cwd(),
npmNodeModulesGlobalDir: npmNodeModulesGlobalDir,
}, err => {
if (err) console.error(err);
fn(err ? 1 : 0);
});
};
function processCommand(commandKey, env, fn) {
const commandsPath = path.join(env.npmNodeModulesGlobalDir, 'lib/commands');
const commands = [... fs.readdirSync(commandsPath)]
.filter(x => !['example.js'].includes(x) && x.endsWith('.js'))
.map(x => [x, require(path.join(commandsPath, x))])
.map(x => ({ name: x[0].replace('.js', ''), use: x[1], type: 'normal' }));
const command = commands.find(x => x.name === commandKey);
if (command != undefined) {
command.use({ command: commandKey, ...env })
.then(() => fn())
.catch(error => fn(error))
} else {
fn(`Command ${command} not found.`);
}
};
module.exports = run;