diff --git a/.eslintrc b/.eslintrc index cb2d8acd2..12defcd8a 100644 --- a/.eslintrc +++ b/.eslintrc @@ -17,7 +17,6 @@ }, "parser": "@typescript-eslint/parser", "parserOptions": { - "project": "./tsconfig.json", "ecmaVersion": 6, "sourceType": "module", "ecmaFeatures": { @@ -34,7 +33,10 @@ "comma-dangle": ["error", "never"] // 多行对象字面量中要求拖尾逗号 } }, { - "files": ["webpack/*.js", "types/*.ts", "typescript/*.ts","typescript/**/*.ts", "./*.js", "lib/**/*.js", "lib/*.js"], + "files": ["webpack/*.js", "./*.js", "lib/**/*.js", "lib/*.js", "./bin/*.js", "./bin/**/*.js"], + "extends": "plugin:@fui/esnext" + }, { + "files": ["types/*.ts", "typescript/*.ts","typescript/**/*.ts"], "extends": "plugin:@fui/typescript" }] } diff --git a/.npmignore b/.npmignore index a6aad7230..a5bc0942a 100644 --- a/.npmignore +++ b/.npmignore @@ -38,3 +38,5 @@ !.eslintrc !dist/2.0/jsy.min.css !dist/2.0/bi.min.css +!bin/* +!bin/**/* diff --git a/bin/cli/cli.js b/bin/cli/cli.js new file mode 100644 index 000000000..732fc1133 --- /dev/null +++ b/bin/cli/cli.js @@ -0,0 +1,44 @@ +#!/usr/bin/env node + +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; + } + }); + + return args; +} + +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 (cmds.has(cmd)) { + cmds.get(cmd)?.exec(getArgs(startIndex + 2)); +} else { + throw new Error(`Command ${cmd} not supported`); +} diff --git a/bin/cli/worker/cli.worker.js b/bin/cli/worker/cli.worker.js new file mode 100644 index 000000000..0c086db08 --- /dev/null +++ b/bin/cli/worker/cli.worker.js @@ -0,0 +1,62 @@ +const fs = require('fs'); +const path = require('path'); + +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 args => { + if (!args.init) { + throw new Error(`Command init not found in args`); + } + + if (!args.name) { + throw new Error('Command --name=... not found in args'); + } + + 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/bin/cli/worker/template/action_type.ts b/bin/cli/worker/template/action_type.ts new file mode 100644 index 000000000..e69de29bb diff --git a/bin/cli/worker/template/main_thread_template.ts b/bin/cli/worker/template/main_thread_template.ts new file mode 100644 index 000000000..8df764495 --- /dev/null +++ b/bin/cli/worker/template/main_thread_template.ts @@ -0,0 +1,5 @@ +class CrudMainTreadWorker extends BI.Workers.MainThreadWorker { + protected initActions(): void { + // to init some actions + } +} diff --git a/bin/cli/worker/template/worker_thread_template.ts b/bin/cli/worker/template/worker_thread_template.ts new file mode 100644 index 000000000..fc457c9c7 --- /dev/null +++ b/bin/cli/worker/template/worker_thread_template.ts @@ -0,0 +1,5 @@ +class CrudWorkerTreadWorker extends BI.Workers.MainThreadWorker { + protected initActions(): void { + // to init some actions + } +} diff --git a/package.json b/package.json index efe86cf09..91c648bc4 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", @@ -82,4 +85,4 @@ }, "author": "fanruan", "license": "MIT" -} \ No newline at end of file +}