Browse Source

chore: 小小重构下

es6
iapyang 2 years ago
parent
commit
720d4e2957
  1. 31
      bin/cli/cli.js
  2. 35
      bin/cli/worker/cli.worker.js

31
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('命令不存在');
}

35
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`]: {

Loading…
Cancel
Save