From 720d4e295790a76c1fe3dd3f80b91c3bf3ed0fe0 Mon Sep 17 00:00:00 2001 From: iapyang Date: Wed, 6 Jul 2022 20:31:06 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=B0=8F=E5=B0=8F=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/cli/cli.js | 31 +++++++++++++++++++++++++++++-- bin/cli/worker/cli.worker.js | 35 +++++------------------------------ 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/bin/cli/cli.js b/bin/cli/cli.js index 7cd058e0c..7921da776 100644 --- a/bin/cli/cli.js +++ b/bin/cli/cli.js @@ -2,14 +2,41 @@ 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).split(''); + args[flags] = true; + } + }); + + return args; +} + const cmds = new Map([ ['worker', workerCmd], ]); -const cmd = process.argv[1]; +const startIndex = process.argv.findIndex(argv => argv === 'fui-cli'); + +if (startIndex === -1) { + throw new Error('commad not found'); +} + +const cmd = process.argv[startIndex + 1]; if (cmds.has(cmd)) { - cmds.get(cmd)?.exec(); + cmds.get(cmd)?.exec(getArgs(startIndex + 2)); } else { throw new Error('命令不存在'); } diff --git a/bin/cli/worker/cli.worker.js b/bin/cli/worker/cli.worker.js index aa742e3fb..13ad48a5d 100644 --- a/bin/cli/worker/cli.worker.js +++ b/bin/cli/worker/cli.worker.js @@ -1,29 +1,6 @@ const fs = require('fs'); const path = require('path'); -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).split(''); - flags.forEach(flag => { - args[flag] = true; - }); - } - }); - - return args; -} - function scanAndCreate(structure, root = process.cwd()) { Object.keys(structure) .forEach(name => { @@ -37,23 +14,21 @@ function scanAndCreate(structure, root = process.cwd()) { const content = fs.readFileSync(structure[name]).toString(); fs.appendFileSync(path.resolve(root, name), content); - } + } }); } module.exports = { - exec: async () => { - const args = getArgs(process.argv); - - if (!args['-init']) { + exec: async args => { + if (!args.init) { throw new Error('command not found'); } - if (!args['--name']) { + if (!args.name) { throw new Error('fileName not found'); } - const name = args['--name']; + const name = args.name; const structure = { [`${name}_worker`]: {