From eeb4a183da525f278f05c725d5135e6a951cb40e Mon Sep 17 00:00:00 2001 From: iapyang Date: Tue, 15 Nov 2022 17:19:05 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-13523=20chore:=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=B8=8Bfui-cli=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/cli/cli.js | 35 ++++++----------------------------- bin/cli/worker/cli.worker.js | 4 ++-- package.json | 8 ++++++-- 3 files changed, 14 insertions(+), 33 deletions(-) mode change 100644 => 100755 bin/cli/cli.js 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" + } +}