From cb490921f22949a6d3fa95fca52a9a3b419ce636 Mon Sep 17 00:00:00 2001 From: iapyang Date: Tue, 5 Jul 2022 15:43:49 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=89=93=E5=8C=85=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 | 13 ++++++ bin/cli/worker/cli.worker.js | 87 ++++++++++++++++++++++++++++++++++++ package.json | 8 +++- 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 bin/cli/cli.js create mode 100644 bin/cli/worker/cli.worker.js diff --git a/bin/cli/cli.js b/bin/cli/cli.js new file mode 100644 index 000000000..d5fa75280 --- /dev/null +++ b/bin/cli/cli.js @@ -0,0 +1,13 @@ +const workerCmd = require('./cli.worker'); + +const cmds = new Map([ + ['worker', workerCmd], +]); + +const cmd = process.argv[1]; + +if (cmds.has(cmd)) { + cmds.get(cmd)?.exec(); +} else { + throw new Error('命令不存在'); +} diff --git a/bin/cli/worker/cli.worker.js b/bin/cli/worker/cli.worker.js new file mode 100644 index 000000000..aa742e3fb --- /dev/null +++ b/bin/cli/worker/cli.worker.js @@ -0,0 +1,87 @@ +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 => { + if (typeof structure[name] === 'object') { + fs.mkdirSync(path.resolve(root, name)); + + scanAndCreate(structure[name], path.resolve(root, `./${name}`)); + } else if (structure[name] === '') { + fs.appendFileSync(path.resolve(root, name), ''); + } else if (typeof structure[name] === 'string') { + 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']) { + throw new Error('command not found'); + } + + if (!args['--name']) { + throw new Error('fileName not found'); + } + + const name = args['--name']; + + const structure = { + [`${name}_worker`]: { + 'main_thread': { + action: {}, + [`${name}_main_thread.ts`]: path.resolve(__dirname, './template/main_thread_template.ts'), + }, + utils: { + 'action_type.ts': '', + }, + 'worker_thread': { + action: {}, + [`${name}_worker_thread.ts`]: path.resolve(__dirname, './template/worker_thread_template.ts'), + }, + }, + }; + + scanAndCreate(structure); + }, +}; + +// 结构 +// -xxx_worker +// -|--main_thread +// -|--|--action +// -|--|--xxx_main_thread.ts +// -|--utils +// -|--|--action_type.ts +// -|--worker_thread +// -|--|--action +// -|--|--worker_main_thread.ts diff --git a/package.json b/package.json index 8f415033a..0dbc4e7a0 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,9 @@ "description": "fineui", "main": "dist/fineui_without_conflict.min.js", "types": "dist/lib/index.d.ts", + "bin": { + "fui-cli": "./bin/cli/cli.js" + }, "devDependencies": { "@babel/core": "^7.17.4", "@babel/polyfill": "7.6.0", @@ -81,5 +84,8 @@ "registry": "https://registry.npmjs.org" }, "author": "fanruan", - "license": "MIT" + "license": "MIT", + "dependencies": { + "shelljs": "0.8.5" + } }