Browse Source

KERNEL-13523 chore: 修复下fui-cli命令

es6
iapyang 2 years ago
parent
commit
eeb4a183da
  1. 35
      bin/cli/cli.js
  2. 4
      bin/cli/worker/cli.worker.js
  3. 8
      package.json

35
bin/cli/cli.js

@ -1,44 +1,21 @@
#!/usr/bin/env node #!/usr/bin/env node
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const workerCmd = require('./worker/cli.worker'); const workerCmd = require('./worker/cli.worker');
function getArgs (startIndex = 1) { const argv = yargs(hideBin(process.argv)).argv;
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;
}
});
return args; const cmd = argv._[0];
}
const cmds = new Map([ const cmds = new Map([
['worker', workerCmd], ['worker', workerCmd],
]); ]);
const baseCmd = 'fui-cli'; if (!cmd) throw new Error('Command is undefined!');
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 (cmds.has(cmd)) { if (cmds.has(cmd)) {
cmds.get(cmd)?.exec(getArgs(startIndex + 2)); cmds.get(cmd)?.exec(argv);
} else { } else {
throw new Error(`Command ${cmd} not supported`); throw new Error(`Command ${cmd} not supported`);
} }

4
bin/cli/worker/cli.worker.js

@ -5,7 +5,7 @@ function first2UpperCase(str) {
return str.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase()); 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) Object.keys(structure)
.forEach(name => { .forEach(name => {
if (typeof structure[name] === 'object') { 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());
}, },
}; };

8
package.json

@ -86,5 +86,9 @@
"registry": "https://registry.npmjs.org" "registry": "https://registry.npmjs.org"
}, },
"author": "fanruan", "author": "fanruan",
"license": "MIT" "license": "MIT",
} "dependencies": {
"@types/yargs": "17.0.13",
"yargs": "17.6.2"
}
}

Loading…
Cancel
Save