diff --git a/bin/cli/cli.js b/bin/cli/cli.js old mode 100644 new mode 100755 index 732fc1133..513024023 --- a/bin/cli/cli.js +++ b/bin/cli/cli.js @@ -1,44 +1,21 @@ #!/usr/bin/env node +const yargs = require('yargs/yargs'); +const { hideBin } = require('yargs/helpers'); const workerCmd = require('./worker/cli.worker'); -function getArgs (startIndex = 1) { - const args = {}; - process.argv - .slice(startIndex, process.argv.length) - .forEach(arg => { - // long arg - if (arg.slice(0, 2) === '--') { - const longArg = arg.split('='); - const longArgFlag = longArg[0].slice(2, longArg[0].length); - const longArgValue = longArg.length > 1 ? longArg[1] : true; - args[longArgFlag] = longArgValue; - // flags - } else if (arg[0] === '-') { - const flags = arg.slice(1, arg.length); - args[flags] = true; - } - }); +const argv = yargs(hideBin(process.argv)).argv; - return args; -} +const cmd = argv._[0]; const cmds = new Map([ ['worker', workerCmd], ]); -const baseCmd = 'fui-cli'; - -const startIndex = process.argv.findIndex(argv => argv.indexOf(baseCmd) !== -1); - -if (startIndex === -1) { - throw new Error(`Command ${baseCmd} not found in args`); -} - -const cmd = process.argv[startIndex + 1]; +if (!cmd) throw new Error('Command is undefined!'); if (cmds.has(cmd)) { - cmds.get(cmd)?.exec(getArgs(startIndex + 2)); + cmds.get(cmd)?.exec(argv); } else { throw new Error(`Command ${cmd} not supported`); } diff --git a/bin/cli/worker/cli.worker.js b/bin/cli/worker/cli.worker.js index 555c99cf0..b67513fbb 100644 --- a/bin/cli/worker/cli.worker.js +++ b/bin/cli/worker/cli.worker.js @@ -5,7 +5,7 @@ function first2UpperCase(str) { return str.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase()); } -function scanAndCreate(structure, workerName, root = process.env.INIT_CWD) { +function scanAndCreate(structure, workerName, root) { Object.keys(structure) .forEach(name => { if (typeof structure[name] === 'object') { @@ -60,7 +60,7 @@ module.exports = { }, }; - scanAndCreate(structure, name); + scanAndCreate(structure, name, args.where ? path.resolve(args.where) : process.cwd()); }, }; diff --git a/package.json b/package.json index 998f59bfd..ebc245f87 100644 --- a/package.json +++ b/package.json @@ -86,5 +86,9 @@ "registry": "https://registry.npmjs.org" }, "author": "fanruan", - "license": "MIT" -} \ No newline at end of file + "license": "MIT", + "dependencies": { + "@types/yargs": "17.0.13", + "yargs": "17.6.2" + } +}