1 | const fs = require('fs-extra');
|
2 | const path = require('path');
|
3 | const argv= process.argv;
|
4 | const commands = [];
|
5 | const utils = require('./utils');
|
6 | const _ = require('underscore');
|
7 |
|
8 | const commandsDir = path.resolve(__dirname, 'commands');
|
9 | const commandsFile = fs.readdirSync(commandsDir);
|
10 |
|
11 | commandsFile.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 |
|
24 | if(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 |
|
31 | const yargs = require('yargs');
|
32 |
|
33 | commands.forEach(command=>{
|
34 | let m = command.module;
|
35 | yargs.command(command.name, m.desc, m.setOptions, m.run);
|
36 | })
|
37 |
|
38 | yargs
|
39 | .usage('Usage: $0 [command]')
|
40 | .help('h')
|
41 | .alias('h', 'help')
|
42 | .argv;
|