UNPKG

1.09 kBJavaScriptView Raw
1const fs = require('fs-extra');
2const path = require('path');
3const argv= process.argv;
4const commands = [];
5const utils = require('./utils');
6const _ = require('underscore');
7
8const commandsDir = path.resolve(__dirname, 'commands');
9const commandsFile = fs.readdirSync(commandsDir);
10
11commandsFile.forEach(function (file) {
12 if (path.extname(file) !== '.js') return null;
13 let commandModule = require(path.resolve(commandsDir, file));
14 if (!commandModule) {
15 throw new Error(`Module isn't exist in the filepath "${commandsDir}/${file}" `);
16 }
17 let commandName = path.basename(file, '.js');
18 commands.push({
19 name: commandName,
20 module: commandModule
21 });
22})
23
24if(argv.length === 2){
25 argv.push('--help');
26}if(argv[2][0] !== '-' && !_.find(commands, {name: argv[2]})){
27 utils.log.error(`Command "${argv[2]}" doesn't exist.`);
28 argv[2] = '-h';
29}
30
31const yargs = require('yargs');
32
33commands.forEach(command=>{
34 let m = command.module;
35 yargs.command(command.name, m.desc, m.setOptions, m.run);
36})
37
38yargs
39.usage('Usage: $0 [command]')
40.help('h')
41.alias('h', 'help')
42.argv;