From 5b5b45bb8f6cf5ca1a0101e5ef72f42d60040602 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 21 Dec 2022 17:26:11 +0800 Subject: [PATCH 01/13] feat: first impl --- .npmrc | 1 + package.json | 2 +- src/cmd/main.ts | 17 ---- src/config/plugin.ts | 6 -- src/help.ts | 79 +++++++++++++++++++ src/index.ts | 3 +- src/lifecycle.ts | 37 +++++++++ src/meta.json | 3 + test/cmd/debug.test.ts | 20 ----- test/cmd/dev.test.ts | 20 ----- test/cmd/main.test.ts | 10 --- test/fixtures/README.md | 0 {src => test/fixtures/egg-bin}/bin/cli.ts | 1 - test/fixtures/egg-bin/cmd/cov.ts | 23 ++++++ {src => test/fixtures/egg-bin}/cmd/debug.ts | 14 ---- {src => test/fixtures/egg-bin}/cmd/dev.ts | 11 +-- test/fixtures/egg-bin/cmd/main.ts | 8 ++ test/fixtures/egg-bin/cmd/test.ts | 21 +++++ .../fixtures/egg-bin}/config/framework.ts | 0 test/fixtures/egg-bin/config/plugin.ts | 8 ++ test/fixtures/egg-bin/index.ts | 3 + test/fixtures/egg-bin/package.json | 6 ++ test/index.test.ts | 65 ++++++++++++++- test/test-utils.ts | 6 +- test/tsconfig.json | 6 ++ 25 files changed, 265 insertions(+), 105 deletions(-) create mode 100644 .npmrc delete mode 100644 src/cmd/main.ts delete mode 100644 src/config/plugin.ts create mode 100644 src/help.ts create mode 100644 src/lifecycle.ts create mode 100644 src/meta.json delete mode 100644 test/cmd/debug.test.ts delete mode 100644 test/cmd/dev.test.ts delete mode 100644 test/cmd/main.test.ts delete mode 100644 test/fixtures/README.md rename {src => test/fixtures/egg-bin}/bin/cli.ts (98%) create mode 100644 test/fixtures/egg-bin/cmd/cov.ts rename {src => test/fixtures/egg-bin}/cmd/debug.ts (54%) rename {src => test/fixtures/egg-bin}/cmd/dev.ts (62%) create mode 100644 test/fixtures/egg-bin/cmd/main.ts create mode 100644 test/fixtures/egg-bin/cmd/test.ts rename {src => test/fixtures/egg-bin}/config/framework.ts (100%) create mode 100644 test/fixtures/egg-bin/config/plugin.ts create mode 100644 test/fixtures/egg-bin/index.ts create mode 100644 test/fixtures/egg-bin/package.json create mode 100644 test/tsconfig.json diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..9cf9495 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false \ No newline at end of file diff --git a/package.json b/package.json index 9aa962b..ee70058 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "prepack": "npm run tsc" }, "dependencies": { - "@artus-cli/artus-cli": "beta" + "@artus-cli/artus-cli": "latest" }, "devDependencies": { "@artus/eslint-config-artus": "^0.0.1", diff --git a/src/cmd/main.ts b/src/cmd/main.ts deleted file mode 100644 index ca24424..0000000 --- a/src/cmd/main.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { DefineCommand, Command, Option, DefineOption } from '@artus-cli/artus-cli'; - -interface MainOption extends Option { - params?: string[]; -} - -@DefineCommand({ - command: '$0 [params...]', -}) -export class MainCommand extends Command { - @DefineOption() - args: MainOption; - - async run() { - console.info('main', this.args.params.join(',')); - } -} diff --git a/src/config/plugin.ts b/src/config/plugin.ts deleted file mode 100644 index 832e840..0000000 --- a/src/config/plugin.ts +++ /dev/null @@ -1,6 +0,0 @@ -export default { - // checkUpdate: { - // enable: true, - // package: 'plugin-check-update', - // }, -}; diff --git a/src/help.ts b/src/help.ts new file mode 100644 index 0000000..abf89e8 --- /dev/null +++ b/src/help.ts @@ -0,0 +1,79 @@ +import { Option, DefineCommand, Command, DefineOption, Inject, CommandContext, Program } from '@artus-cli/artus-cli'; +import commandLineUsage from 'command-line-usage'; + +interface HelpOption extends Option { + command: string; +} + +@DefineCommand({ + command: 'help [command]', + description: 'show help infomation for command', + alias: 'h', +}) +export class HelpCommand extends Command { + @Inject() + ctx: CommandContext; + + @Inject() + program: Program; + + @DefineOption() + option: HelpOption; + + async run() { + const ctx = this.ctx; + const { binName: bin } = this.program; + const command = this.option.command || bin; + const commandUid = command.startsWith(bin) ? command : `${bin} ${command}`; + const helpCommand = ctx.commands.get(commandUid) || ctx.rootCommand; + + // display help informations + const displayTexts: string[] = []; + const commandLineUsageList: any[] = []; + const optionKeys = helpCommand.options ? Object.keys(helpCommand.options) : []; + + // usage info in first line + displayTexts.push(`Usage: ${helpCommand.command.startsWith(bin) ? '' : `${bin} `}${helpCommand.command}`); + if (helpCommand.description) { + displayTexts.push('', helpCommand.description); + } + + // available commands, display all subcommands if match the root command + const availableCommands = ( + helpCommand.isRoot + ? Array.from(new Set(ctx.commands.values())) + : [ helpCommand ].concat(helpCommand.childs || []) + ).filter(c => !c.isRoot && c.isRunable); + + if (availableCommands.length) { + commandLineUsageList.push({ + header: 'Available Commands', + content: availableCommands.map(command => ({ + name: command.command, + summary: command.description, + })), + }); + } + + // options list, like -h, --help / -v, --version ... + commandLineUsageList.push({ + header: 'Options', + optionList: optionKeys + .map(flag => { + const option = helpCommand.options[flag]; + const showFlag = flag[0].toLowerCase() + flag.substring(1).replace(/[A-Z]/g, '-$&').toLowerCase(); + return { + name: showFlag, + type: { name: option.type }, + description: option.description, + alias: option.alias, + defaultValue: option.default, + }; + }), + }); + + // use command-line-usage to format help informations. + displayTexts.push(commandLineUsage(commandLineUsageList)); + console.info(displayTexts.join('\n')); + } +} diff --git a/src/index.ts b/src/index.ts index c0cc167..b658848 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1 @@ -export * from './cmd/dev'; -export * from './cmd/debug'; +export * from './help'; diff --git a/src/lifecycle.ts b/src/lifecycle.ts new file mode 100644 index 0000000..e55ac51 --- /dev/null +++ b/src/lifecycle.ts @@ -0,0 +1,37 @@ +import { Inject, ApplicationLifecycle, LifecycleHook, LifecycleHookUnit, Program, CommandContext, Utils } from '@artus-cli/artus-cli'; + +@LifecycleHookUnit() +export default class UsageLifecycle implements ApplicationLifecycle { + @Inject() + private readonly program: Program; + + @LifecycleHook() + async configDidLoad() { + // add global options + this.program.option({ + help: { + type: 'boolean', + description: 'Show Help', + alias: 'h', + }, + }); + + this.program.use(async (ctx: CommandContext, next) => { + const { binName: bin } = this.program; + const { fuzzyMatched, matched, args, raw } = ctx; + if (!fuzzyMatched || !args.help) { + if (!matched) { + // can not match any command + console.error(`\n Command not found: '${bin} ${raw.join(' ')}', try '${fuzzyMatched?.cmds.join(' ') || bin} --help' for more information.\n`); + process.exit(1); + } + + return await next(); + } + + // redirect to help command + const utils = ctx.container.get(Utils); + await utils.redirect([ 'help', fuzzyMatched.uid ]); + }); + } +} diff --git a/src/meta.json b/src/meta.json new file mode 100644 index 0000000..084a60b --- /dev/null +++ b/src/meta.json @@ -0,0 +1,3 @@ +{ + "name": "help" +} diff --git a/test/cmd/debug.test.ts b/test/cmd/debug.test.ts deleted file mode 100644 index a684221..0000000 --- a/test/cmd/debug.test.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { run } from '../test-utils'; - -describe('test/debug.test.ts', () => { - it('debug', async () => { - await run('debug --port=80 --flags=1') - // .debug() - .expect('stdout', /inspect true/) - .expect('stdout', /port 80/) - .expect('stdout', /flags 1 number/) - .end(); - }); - - it('inspect alias', async () => { - await run('inspect') - // .debug() - .expect('stdout', /inspect true/) - .expect('stdout', /flags 0 number/) - .end(); - }); -}); diff --git a/test/cmd/dev.test.ts b/test/cmd/dev.test.ts deleted file mode 100644 index 72193a7..0000000 --- a/test/cmd/dev.test.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { run } from '../test-utils'; - -describe('test/dev.test.ts', () => { - it('dev', async () => { - await run('dev --port=80 --nodeFlags=--experimental-vm-modules') - // .debug() - .expect('stdout', /port 80/) - .expect('stdout', /inspect false boolean/) - .expect('stdout', /nodeFlags --experimental-vm-module/) - .end(); - }); - - it('dev with baseDir', async () => { - await run('dev ./src -p=80') - // .debug() - .expect('stdout', /port 80/) - .expect('stdout', /baseDir .\/src/) - .end(); - }); -}); diff --git a/test/cmd/main.test.ts b/test/cmd/main.test.ts deleted file mode 100644 index 3a17ac2..0000000 --- a/test/cmd/main.test.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { run } from '../test-utils'; - -describe('test/main.test.ts', () => { - it('should fallback', async () => { - await run('a b c') - // .debug() - .expect('stdout', /main a,b,c/) - .end(); - }); -}); diff --git a/test/fixtures/README.md b/test/fixtures/README.md deleted file mode 100644 index e69de29..0000000 diff --git a/src/bin/cli.ts b/test/fixtures/egg-bin/bin/cli.ts similarity index 98% rename from src/bin/cli.ts rename to test/fixtures/egg-bin/bin/cli.ts index 8b89046..f72d41b 100644 --- a/src/bin/cli.ts +++ b/test/fixtures/egg-bin/bin/cli.ts @@ -1,5 +1,4 @@ #!/usr/bin/env node import { start } from '@artus-cli/artus-cli'; - start(); diff --git a/test/fixtures/egg-bin/cmd/cov.ts b/test/fixtures/egg-bin/cmd/cov.ts new file mode 100644 index 0000000..37f67a1 --- /dev/null +++ b/test/fixtures/egg-bin/cmd/cov.ts @@ -0,0 +1,23 @@ +import { DefineCommand, DefineOption, Command, Inject } from '@artus-cli/artus-cli'; +import { TestCommand, TestOption } from './test'; + +interface CovOption extends TestOption { + c8?: boolean; +} + +@DefineCommand({ + command: 'cov [file...]', + description: 'Run the coverage', +}) +export class CovCommand extends TestCommand { + @DefineOption({ + c8: { + type: 'boolean', + default: true, + }, + }) + args: CovOption; + + async run() { + } +} diff --git a/src/cmd/debug.ts b/test/fixtures/egg-bin/cmd/debug.ts similarity index 54% rename from src/cmd/debug.ts rename to test/fixtures/egg-bin/cmd/debug.ts index 2c88342..a06e316 100644 --- a/src/cmd/debug.ts +++ b/test/fixtures/egg-bin/cmd/debug.ts @@ -7,16 +7,10 @@ interface DebugOption extends DevOption { @DefineCommand({ command: 'debug [baseDir]', - alias: [ 'inspect' ], description: 'Run the development server at debug mode', }) export class DebugCommand extends DevCommand { @DefineOption({ - inspect: { - type: 'boolean', - default: true, - description: 'Debug with node-inspector', - }, flags: { type: 'number', alias: 'f', @@ -26,13 +20,5 @@ export class DebugCommand extends DevCommand { args: DebugOption; async run() { - console.info('port', this.args.port); - console.info('inspect', this.args.inspect); - console.info('flags', this.args.flags, typeof this.args.flags); - console.info('baseDir', this.args.baseDir); - return { - command: 'debug', - args: this.args, - }; } } diff --git a/src/cmd/dev.ts b/test/fixtures/egg-bin/cmd/dev.ts similarity index 62% rename from src/cmd/dev.ts rename to test/fixtures/egg-bin/cmd/dev.ts index 0d764fd..37010c4 100644 --- a/src/cmd/dev.ts +++ b/test/fixtures/egg-bin/cmd/dev.ts @@ -1,4 +1,4 @@ -import { DefineCommand, DefineOption, Command, Option } from '@artus-cli/artus-cli'; +import { DefineCommand, DefineOption, Command, Middleware, Option } from '@artus-cli/artus-cli'; export interface DevOption extends Option { port?: number; @@ -10,6 +10,7 @@ export interface DevOption extends Option { @DefineCommand({ command: 'dev [baseDir]', description: 'Run the development server', + alias: [ 'd' ], }) export class DevCommand extends Command { @DefineOption({ @@ -33,13 +34,5 @@ export class DevCommand extends Command { args: DevOption; async run() { - console.info('port', this.args.port); - console.info('inspect', this.args.inspect, typeof this.args.inspect); - console.info('nodeFlags', this.args.nodeFlags); - console.info('baseDir', this.args.baseDir); - return { - command: 'dev', - args: this.args, - }; } } diff --git a/test/fixtures/egg-bin/cmd/main.ts b/test/fixtures/egg-bin/cmd/main.ts new file mode 100644 index 0000000..561d892 --- /dev/null +++ b/test/fixtures/egg-bin/cmd/main.ts @@ -0,0 +1,8 @@ +import { DefineCommand, Command } from '@artus-cli/artus-cli'; + +@DefineCommand() +export class MainCommand extends Command { + async run() { + console.info('main'); + } +} diff --git a/test/fixtures/egg-bin/cmd/test.ts b/test/fixtures/egg-bin/cmd/test.ts new file mode 100644 index 0000000..1c1baf6 --- /dev/null +++ b/test/fixtures/egg-bin/cmd/test.ts @@ -0,0 +1,21 @@ +import { DefineCommand, DefineOption, Command, Middleware, Option } from '@artus-cli/artus-cli'; + +export interface TestOption extends Option { + baseDir: string; + file: string[] +} + +@DefineCommand({ + command: 'test [file...]', + description: 'Run the unitest', + alias: [ 't' ], +}) +export class TestCommand extends Command { + @DefineOption() + options: TestOption; + + async run() { + console.info('test baseDir', this.options.baseDir); + console.info('test files', this.options.file); + } +} diff --git a/src/config/framework.ts b/test/fixtures/egg-bin/config/framework.ts similarity index 100% rename from src/config/framework.ts rename to test/fixtures/egg-bin/config/framework.ts diff --git a/test/fixtures/egg-bin/config/plugin.ts b/test/fixtures/egg-bin/config/plugin.ts new file mode 100644 index 0000000..3bef3ce --- /dev/null +++ b/test/fixtures/egg-bin/config/plugin.ts @@ -0,0 +1,8 @@ +import path from 'node:path'; + +export default { + help: { + enable: true, + path: path.resolve(__dirname, '../../../../src'), + }, +}; diff --git a/test/fixtures/egg-bin/index.ts b/test/fixtures/egg-bin/index.ts new file mode 100644 index 0000000..fe8e9cc --- /dev/null +++ b/test/fixtures/egg-bin/index.ts @@ -0,0 +1,3 @@ +export * from './cmd/dev'; +export * from './cmd/debug'; +export * from './cmd/main'; diff --git a/test/fixtures/egg-bin/package.json b/test/fixtures/egg-bin/package.json new file mode 100644 index 0000000..495e74d --- /dev/null +++ b/test/fixtures/egg-bin/package.json @@ -0,0 +1,6 @@ +{ + "name": "egg-bin", + "version": "1.0.0", + "type": "commonjs", + "bin": "./bin/cli.js" +} diff --git a/test/index.test.ts b/test/index.test.ts index 63f658e..7891857 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -2,9 +2,72 @@ import { run } from './test-utils'; describe('test/index.test.ts', () => { it('should --help', async () => { - await run('--help') + await run('egg-bin', '--help') // .debug() .expect('stdout', /Available Commands/) + .expect('stdout', /help \[command\]\s+show help infomation for command/) + .expect('stdout', /test \ \[file\.\.\.\]\s+Run the unitest/) + .expect('stdout', /cov \ \[file\.\.\.\]\s+Run the coverage/) + .expect('stdout', /dev \[baseDir\]\s+Run the development server/) + .expect('stdout', /debug \[baseDir\]\s+Run the development server at debug mode/) + .expect('stdout', /Options/) + .expect('stdout', /-h, --help\s+Show Help/) + .end(); + }); + + it('should subcommand --help', async () => { + await run('egg-bin', 'dev -h') + .debug() + .expect('stdout', /Available Commands/) + .notExpect('stdout', /help \[command\]\s+show help infomation for command/) + .notExpect('stdout', /test \ \[file\.\.\.\]\s+Run the unitest/) + .notExpect('stdout', /cov \ \[file\.\.\.\]\s+Run the coverage/) + .expect('stdout', /dev \[baseDir\]\s+Run the development server/) + .expect('stdout', /Options/) + .expect('stdout', /--inspect\s+Debug with node-inspector/) + .expect('stdout', /--node-flags string/) + .expect('stdout', /-h, --help\s+Show Help/) + .end(); + }); + + it('should use help command', async () => { + await run('egg-bin', 'help') + // .debug() + .expect('stdout', /Available Commands/) + .expect('stdout', /help \[command\]\s+show help infomation for command/) + .expect('stdout', /test \ \[file\.\.\.\]\s+Run the unitest/) + .expect('stdout', /cov \ \[file\.\.\.\]\s+Run the coverage/) + .expect('stdout', /dev \[baseDir\]\s+Run the development server/) + .expect('stdout', /debug \[baseDir\]\s+Run the development server at debug mode/) + .expect('stdout', /Options/) + .expect('stdout', /-h, --help\s+Show Help/) + .end(); + + await run('egg-bin', 'help dev') + // .debug() + .expect('stdout', /Available Commands/) + .notExpect('stdout', /help \[command\]\s+show help infomation for command/) + .notExpect('stdout', /test \ \[file\.\.\.\]\s+Run the unitest/) + .notExpect('stdout', /cov \ \[file\.\.\.\]\s+Run the coverage/) + .expect('stdout', /dev \[baseDir\]\s+Run the development server/) + .expect('stdout', /Options/) + .expect('stdout', /--inspect\s+Debug with node-inspector/) + .expect('stdout', /--node-flags string/) + .expect('stdout', /-h, --help\s+Show Help/) + .end(); + }); + + it('should show command not found', async () => { + await run('egg-bin', 'notexistscommand -h') + // .debug() + .expect('stderr', /Command not found: 'egg-bin notexistscommand -h'/) + .expect('stderr', /try 'egg-bin --help' for more information/) + .end(); + + await run('egg-bin', 'dev abc bbc') + // .debug() + .expect('stderr', /Command not found: 'egg-bin dev abc bbc'/) + .expect('stderr', /try 'egg-bin dev --help' for more information/) .end(); }); }); diff --git a/test/test-utils.ts b/test/test-utils.ts index 8b2c377..eba3c1d 100644 --- a/test/test-utils.ts +++ b/test/test-utils.ts @@ -2,12 +2,10 @@ import path from 'path'; import coffee from 'coffee'; import { ForkOptions } from 'child_process'; -export function run(args: string | string[], options: ForkOptions = {}) { - const bin = path.join(__dirname, '../src/bin/cli.ts'); - const cwd = path.join(__dirname, 'fixtures'); +export function run(binName: string, args: string | string[], options: ForkOptions = {}) { + const bin = path.resolve(__dirname, './fixtures', binName, 'bin/cli.ts'); if (typeof args === 'string') args = args.split(' '); return coffee.fork(bin, args, { - cwd, execArgv: [ '-r', 'ts-node/register' ].concat(options.execArgv || []), ...options, }); diff --git a/test/tsconfig.json b/test/tsconfig.json new file mode 100644 index 0000000..4d08d15 --- /dev/null +++ b/test/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../tsconfig", + "include": [ + "**/*.ts" + ] +} From fe0d1802e82c33be79aae00e175dc14a13be440b Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 21 Dec 2022 18:11:13 +0800 Subject: [PATCH 02/13] feat: first impl --- test/fixtures/egg-bin/index.ts | 3 -- test/fixtures/{egg-bin => my-bin}/bin/cli.ts | 0 test/fixtures/{egg-bin => my-bin}/cmd/cov.ts | 0 .../fixtures/{egg-bin => my-bin}/cmd/debug.ts | 0 test/fixtures/{egg-bin => my-bin}/cmd/dev.ts | 0 test/fixtures/{egg-bin => my-bin}/cmd/main.ts | 0 test/fixtures/{egg-bin => my-bin}/cmd/test.ts | 0 .../{egg-bin => my-bin}/config/framework.ts | 0 .../{egg-bin => my-bin}/config/plugin.ts | 0 .../fixtures/{egg-bin => my-bin}/package.json | 2 +- test/fixtures/other-bin/bin/cli.ts | 4 +++ test/fixtures/other-bin/config/framework.ts | 5 +++ test/fixtures/other-bin/package.json | 6 ++++ test/index.test.ts | 36 +++++++++++++------ 14 files changed, 42 insertions(+), 14 deletions(-) delete mode 100644 test/fixtures/egg-bin/index.ts rename test/fixtures/{egg-bin => my-bin}/bin/cli.ts (100%) rename test/fixtures/{egg-bin => my-bin}/cmd/cov.ts (100%) rename test/fixtures/{egg-bin => my-bin}/cmd/debug.ts (100%) rename test/fixtures/{egg-bin => my-bin}/cmd/dev.ts (100%) rename test/fixtures/{egg-bin => my-bin}/cmd/main.ts (100%) rename test/fixtures/{egg-bin => my-bin}/cmd/test.ts (100%) rename test/fixtures/{egg-bin => my-bin}/config/framework.ts (100%) rename test/fixtures/{egg-bin => my-bin}/config/plugin.ts (100%) rename test/fixtures/{egg-bin => my-bin}/package.json (77%) create mode 100644 test/fixtures/other-bin/bin/cli.ts create mode 100644 test/fixtures/other-bin/config/framework.ts create mode 100644 test/fixtures/other-bin/package.json diff --git a/test/fixtures/egg-bin/index.ts b/test/fixtures/egg-bin/index.ts deleted file mode 100644 index fe8e9cc..0000000 --- a/test/fixtures/egg-bin/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './cmd/dev'; -export * from './cmd/debug'; -export * from './cmd/main'; diff --git a/test/fixtures/egg-bin/bin/cli.ts b/test/fixtures/my-bin/bin/cli.ts similarity index 100% rename from test/fixtures/egg-bin/bin/cli.ts rename to test/fixtures/my-bin/bin/cli.ts diff --git a/test/fixtures/egg-bin/cmd/cov.ts b/test/fixtures/my-bin/cmd/cov.ts similarity index 100% rename from test/fixtures/egg-bin/cmd/cov.ts rename to test/fixtures/my-bin/cmd/cov.ts diff --git a/test/fixtures/egg-bin/cmd/debug.ts b/test/fixtures/my-bin/cmd/debug.ts similarity index 100% rename from test/fixtures/egg-bin/cmd/debug.ts rename to test/fixtures/my-bin/cmd/debug.ts diff --git a/test/fixtures/egg-bin/cmd/dev.ts b/test/fixtures/my-bin/cmd/dev.ts similarity index 100% rename from test/fixtures/egg-bin/cmd/dev.ts rename to test/fixtures/my-bin/cmd/dev.ts diff --git a/test/fixtures/egg-bin/cmd/main.ts b/test/fixtures/my-bin/cmd/main.ts similarity index 100% rename from test/fixtures/egg-bin/cmd/main.ts rename to test/fixtures/my-bin/cmd/main.ts diff --git a/test/fixtures/egg-bin/cmd/test.ts b/test/fixtures/my-bin/cmd/test.ts similarity index 100% rename from test/fixtures/egg-bin/cmd/test.ts rename to test/fixtures/my-bin/cmd/test.ts diff --git a/test/fixtures/egg-bin/config/framework.ts b/test/fixtures/my-bin/config/framework.ts similarity index 100% rename from test/fixtures/egg-bin/config/framework.ts rename to test/fixtures/my-bin/config/framework.ts diff --git a/test/fixtures/egg-bin/config/plugin.ts b/test/fixtures/my-bin/config/plugin.ts similarity index 100% rename from test/fixtures/egg-bin/config/plugin.ts rename to test/fixtures/my-bin/config/plugin.ts diff --git a/test/fixtures/egg-bin/package.json b/test/fixtures/my-bin/package.json similarity index 77% rename from test/fixtures/egg-bin/package.json rename to test/fixtures/my-bin/package.json index 495e74d..aab8077 100644 --- a/test/fixtures/egg-bin/package.json +++ b/test/fixtures/my-bin/package.json @@ -1,5 +1,5 @@ { - "name": "egg-bin", + "name": "my-bin", "version": "1.0.0", "type": "commonjs", "bin": "./bin/cli.js" diff --git a/test/fixtures/other-bin/bin/cli.ts b/test/fixtures/other-bin/bin/cli.ts new file mode 100644 index 0000000..f72d41b --- /dev/null +++ b/test/fixtures/other-bin/bin/cli.ts @@ -0,0 +1,4 @@ +#!/usr/bin/env node + +import { start } from '@artus-cli/artus-cli'; +start(); diff --git a/test/fixtures/other-bin/config/framework.ts b/test/fixtures/other-bin/config/framework.ts new file mode 100644 index 0000000..2e2085e --- /dev/null +++ b/test/fixtures/other-bin/config/framework.ts @@ -0,0 +1,5 @@ +import path from 'path'; + +export default { + path: path.resolve(__dirname, '../../my-bin'), +}; diff --git a/test/fixtures/other-bin/package.json b/test/fixtures/other-bin/package.json new file mode 100644 index 0000000..d7efc66 --- /dev/null +++ b/test/fixtures/other-bin/package.json @@ -0,0 +1,6 @@ +{ + "name": "other-bin", + "version": "1.0.0", + "type": "commonjs", + "bin": "./bin/cli.js" +} diff --git a/test/index.test.ts b/test/index.test.ts index 7891857..604a639 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -2,8 +2,9 @@ import { run } from './test-utils'; describe('test/index.test.ts', () => { it('should --help', async () => { - await run('egg-bin', '--help') + await run('my-bin', '--help') // .debug() + .expect('stdout', /Usage: my-bin/) .expect('stdout', /Available Commands/) .expect('stdout', /help \[command\]\s+show help infomation for command/) .expect('stdout', /test \ \[file\.\.\.\]\s+Run the unitest/) @@ -16,7 +17,7 @@ describe('test/index.test.ts', () => { }); it('should subcommand --help', async () => { - await run('egg-bin', 'dev -h') + await run('my-bin', 'dev -h') .debug() .expect('stdout', /Available Commands/) .notExpect('stdout', /help \[command\]\s+show help infomation for command/) @@ -31,7 +32,7 @@ describe('test/index.test.ts', () => { }); it('should use help command', async () => { - await run('egg-bin', 'help') + await run('my-bin', 'help') // .debug() .expect('stdout', /Available Commands/) .expect('stdout', /help \[command\]\s+show help infomation for command/) @@ -43,7 +44,7 @@ describe('test/index.test.ts', () => { .expect('stdout', /-h, --help\s+Show Help/) .end(); - await run('egg-bin', 'help dev') + await run('my-bin', 'help dev') // .debug() .expect('stdout', /Available Commands/) .notExpect('stdout', /help \[command\]\s+show help infomation for command/) @@ -58,16 +59,31 @@ describe('test/index.test.ts', () => { }); it('should show command not found', async () => { - await run('egg-bin', 'notexistscommand -h') + await run('my-bin', 'notexistscommand -h') // .debug() - .expect('stderr', /Command not found: 'egg-bin notexistscommand -h'/) - .expect('stderr', /try 'egg-bin --help' for more information/) + .expect('stderr', /Command not found: 'my-bin notexistscommand -h'/) + .expect('stderr', /try 'my-bin --help' for more information/) .end(); - await run('egg-bin', 'dev abc bbc') + await run('my-bin', 'dev abc bbc') // .debug() - .expect('stderr', /Command not found: 'egg-bin dev abc bbc'/) - .expect('stderr', /try 'egg-bin dev --help' for more information/) + .expect('stderr', /Command not found: 'my-bin dev abc bbc'/) + .expect('stderr', /try 'my-bin dev --help' for more information/) + .end(); + }); + + it('should show help in extends command without error', async () => { + await run('other-bin', '-h') + // .debug() + .expect('stdout', /Usage: other-bin/) + .expect('stdout', /Available Commands/) + .expect('stdout', /help \[command\]\s+show help infomation for command/) + .expect('stdout', /test \ \[file\.\.\.\]\s+Run the unitest/) + .expect('stdout', /cov \ \[file\.\.\.\]\s+Run the coverage/) + .expect('stdout', /dev \[baseDir\]\s+Run the development server/) + .expect('stdout', /debug \[baseDir\]\s+Run the development server at debug mode/) + .expect('stdout', /Options/) + .expect('stdout', /-h, --help\s+Show Help/) .end(); }); }); From 937e3c37816213b0e44d2fc8fcbd1d602e644f9d Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 21 Dec 2022 19:19:41 +0800 Subject: [PATCH 03/13] feat: update name --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index ee70058..6967f93 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "@artus-cli/template", - "version": "0.0.0", - "description": "artus-cli template", + "name": "@artus-cli/plugin-help", + "version": "0.0.1", + "description": "show help information for artus cli", "homepage": "", "author": "", "main": "dist/index.js", From 787e388467d7c11f188fb2dcb1e228c16d554f70 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 21 Dec 2022 20:07:09 +0800 Subject: [PATCH 04/13] chore: update readme --- README.md | 42 ++++++++++++++++++++---------------------- package.json | 2 +- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index e6706ef..b418b45 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,32 @@ -# artus-cli/template +# artus-cli/plugin-help -template repository for artus-cli +Built-in plugin for showing help information in artus-cli - +[![NPM version](https://img.shields.io/npm/v/@artus-cli/plugin-help.svg?style=flat-square)](https://npmjs.org/package/@artus-cli/plugin-help) +[![NPM quality](https://img.shields.io/npms-io/final-score/@artus-cli/plugin-help.svg?style=flat-square)](https://npmjs.org/package/@artus-cli/plugin-help) +[![NPM download](https://img.shields.io/npm/dm/@artus-cli/plugin-help.svg?style=flat-square)](https://npmjs.org/package/@artus-cli/plugin-help) +[![Continuous Integration](https://github.com/artus-cli/plugin-help/actions/workflows/ci.yml/badge.svg)](https://github.com/artus-cli/plugin-help/actions/workflows/ci.yml) +[![Test coverage](https://img.shields.io/codecov/c/github/artus-cli/plugin-help.svg?style=flat-square)](https://codecov.io/gh/artus-cli/plugin-help) +[![Oss Insight Analytics](https://img.shields.io/badge/OssInsight-artus--cli%2Fartus--cli-blue.svg?style=flat-square)](https://ossinsight.io/analyze/artus-cli/plugin-help) -[![NPM version](https://img.shields.io/npm/v/@artus-cli/artus-cli.svg?style=flat-square)](https://npmjs.org/package/@artus-cli/artus-cli) -[![NPM quality](https://img.shields.io/npms-io/final-score/@artus-cli/artus-cli.svg?style=flat-square)](https://npmjs.org/package/@artus-cli/artus-cli) -[![NPM download](https://img.shields.io/npm/dm/@artus-cli/artus-cli.svg?style=flat-square)](https://npmjs.org/package/@artus-cli/artus-cli) -[![Continuous Integration](https://github.com/artus-cli/artus-cli/actions/workflows/ci.yml/badge.svg)](https://github.com/artus-cli/artus-cli/actions/workflows/ci.yml) -[![Test coverage](https://img.shields.io/codecov/c/github/artus-cli/artus-cli.svg?style=flat-square)](https://codecov.io/gh/artus-cli/artus-cli) -[![Oss Insight Analytics](https://img.shields.io/badge/OssInsight-artus--cli%2Fartus--cli-blue.svg?style=flat-square)](https://ossinsight.io/analyze/artus-cli/artus-cli) - - -## Usage +## Install ```sh -# print help -$ my-bin --help - -# run dev -$ my-bin dev --port=8080 +$ npm i @artus-cli/plugin-help ``` -## Commands - -### dev - +## Usage -### debug +```typescript +// plugin.ts +export default { + help: { + enable: true, + package: '@artus-cli/plugin-help', + }, +}; +``` ## Contributing diff --git a/package.json b/package.json index 6967f93..c7948d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@artus-cli/plugin-help", - "version": "0.0.1", + "version": "0.0.1-beta.1", "description": "show help information for artus cli", "homepage": "", "author": "", From 835d00a3a64cc5a2932f4a7604a4b17484f71308 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 21 Dec 2022 20:11:09 +0800 Subject: [PATCH 05/13] chore: update readme --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index b418b45..133c0b3 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,4 @@ export default { ```sh $ npm test $ npm run cov - -$ npx ts-node src/bin/cli.ts dev --port=8080 ``` From c2d3d556a3c8f8c7ce41cb0a87b8f5782bb2dc1c Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 21 Dec 2022 20:16:53 +0800 Subject: [PATCH 06/13] feat: add release yml --- .github/workflows/release.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..44082b6 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,18 @@ +name: Release +on: + # 合并后自动发布 + # push: + # branches: [ master, main, next, beta, '*.x' ] + + # 手动发布 + workflow_dispatch: {} + +jobs: + release: + name: Node.js + uses: artusjs/github-actions/.github/workflows/node-release.yml@v1 + secrets: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + # with: + # checkTest: false + # dryRun: true \ No newline at end of file From edcd52b45d36b0b1ac17216d0a24654e7ed25001 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 21 Dec 2022 21:03:14 +0800 Subject: [PATCH 07/13] chore: update pkg --- package.json | 7 ++++--- tsconfig.json | 2 -- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index c7948d8..d0a959c 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "prepack": "npm run tsc" }, "dependencies": { - "@artus-cli/artus-cli": "latest" + "tslib": "^2.4.0" }, "devDependencies": { "@artus/eslint-config-artus": "^0.0.1", @@ -38,13 +38,13 @@ "mocha": "^10.0.0", "ts-mocha": "^10.0.0", "ts-node": "^10.9.1", + "@artus-cli/artus-cli": "latest", "tsconfig-paths": "^4.1.1", - "tslib": "^2.4.0", "typescript": "^4.8.2" }, "repository": { "type": "git", - "url": "" + "url": "https://github.com/artus-cli/plugin-help" }, "files": [ "dist" @@ -55,6 +55,7 @@ "keywords": [ "artus", "artus-cli", + "artus-cli-plugin", "command-line-app", "cli", "commander" diff --git a/tsconfig.json b/tsconfig.json index aab23a6..e400a73 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,8 +2,6 @@ "extends": "@artus/tsconfig", "compilerOptions": { "baseUrl": ".", - "noUnusedParameters": false, - "noUnusedLocals":false, "resolveJsonModule": true, "outDir": "dist" }, From c1397c338a105b8f02381847f74489bef1de9acf Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 21 Dec 2022 21:04:58 +0800 Subject: [PATCH 08/13] chore: update release.yml --- .github/workflows/release.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 44082b6..7185109 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -13,6 +13,7 @@ jobs: uses: artusjs/github-actions/.github/workflows/node-release.yml@v1 secrets: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + GIT_TOKEN: ${{ secrets.GIT_TOKEN }} # with: # checkTest: false # dryRun: true \ No newline at end of file From 59623b21dbbef6f082f379cb1ae4e6341cad2fe7 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 21 Dec 2022 21:06:16 +0800 Subject: [PATCH 09/13] fix: lint --- test/fixtures/my-bin/cmd/cov.ts | 2 +- test/fixtures/my-bin/cmd/dev.ts | 2 +- test/fixtures/my-bin/cmd/test.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/fixtures/my-bin/cmd/cov.ts b/test/fixtures/my-bin/cmd/cov.ts index 37f67a1..7437b28 100644 --- a/test/fixtures/my-bin/cmd/cov.ts +++ b/test/fixtures/my-bin/cmd/cov.ts @@ -1,4 +1,4 @@ -import { DefineCommand, DefineOption, Command, Inject } from '@artus-cli/artus-cli'; +import { DefineCommand, DefineOption } from '@artus-cli/artus-cli'; import { TestCommand, TestOption } from './test'; interface CovOption extends TestOption { diff --git a/test/fixtures/my-bin/cmd/dev.ts b/test/fixtures/my-bin/cmd/dev.ts index 37010c4..455af29 100644 --- a/test/fixtures/my-bin/cmd/dev.ts +++ b/test/fixtures/my-bin/cmd/dev.ts @@ -1,4 +1,4 @@ -import { DefineCommand, DefineOption, Command, Middleware, Option } from '@artus-cli/artus-cli'; +import { DefineCommand, DefineOption, Command, Option } from '@artus-cli/artus-cli'; export interface DevOption extends Option { port?: number; diff --git a/test/fixtures/my-bin/cmd/test.ts b/test/fixtures/my-bin/cmd/test.ts index 1c1baf6..963b091 100644 --- a/test/fixtures/my-bin/cmd/test.ts +++ b/test/fixtures/my-bin/cmd/test.ts @@ -1,4 +1,4 @@ -import { DefineCommand, DefineOption, Command, Middleware, Option } from '@artus-cli/artus-cli'; +import { DefineCommand, DefineOption, Command, Option } from '@artus-cli/artus-cli'; export interface TestOption extends Option { baseDir: string; From a3e48378caacbf498d276eba45377d9c45f1b834 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 21 Dec 2022 21:09:30 +0800 Subject: [PATCH 10/13] fix: lint --- .eslintrc | 12 ------------ src/lifecycle.ts | 2 +- test/fixtures/my-bin/cmd/cov.ts | 1 + test/fixtures/my-bin/cmd/debug.ts | 1 + test/fixtures/my-bin/cmd/dev.ts | 1 + 5 files changed, 4 insertions(+), 13 deletions(-) diff --git a/.eslintrc b/.eslintrc index 370fb68..7c05da9 100644 --- a/.eslintrc +++ b/.eslintrc @@ -3,17 +3,5 @@ "parserOptions": { "project": "./tsconfig.json", "createDefaultProgram": true - }, - "rules": { - "prefer-spread": "off", - "no-return-assign": "off", - "no-case-declarations": "off", - "prefer-const": "off", - "no-regex-spaces": "off", - "no-return-await": "off", - "@typescript-eslint/no-unused-vars": "off", - "@typescript-eslint/no-use-before-define": "off", - "@typescript-eslint/no-empty-function": "off", - "@typescript-eslint/no-var-requires": "off" } } diff --git a/src/lifecycle.ts b/src/lifecycle.ts index e55ac51..8ad7b4e 100644 --- a/src/lifecycle.ts +++ b/src/lifecycle.ts @@ -26,7 +26,7 @@ export default class UsageLifecycle implements ApplicationLifecycle { process.exit(1); } - return await next(); + return next(); } // redirect to help command diff --git a/test/fixtures/my-bin/cmd/cov.ts b/test/fixtures/my-bin/cmd/cov.ts index 7437b28..b1e14b4 100644 --- a/test/fixtures/my-bin/cmd/cov.ts +++ b/test/fixtures/my-bin/cmd/cov.ts @@ -19,5 +19,6 @@ export class CovCommand extends TestCommand { args: CovOption; async run() { + // nothing } } diff --git a/test/fixtures/my-bin/cmd/debug.ts b/test/fixtures/my-bin/cmd/debug.ts index a06e316..bd9739b 100644 --- a/test/fixtures/my-bin/cmd/debug.ts +++ b/test/fixtures/my-bin/cmd/debug.ts @@ -20,5 +20,6 @@ export class DebugCommand extends DevCommand { args: DebugOption; async run() { + // nothing } } diff --git a/test/fixtures/my-bin/cmd/dev.ts b/test/fixtures/my-bin/cmd/dev.ts index 455af29..bfaa9cc 100644 --- a/test/fixtures/my-bin/cmd/dev.ts +++ b/test/fixtures/my-bin/cmd/dev.ts @@ -34,5 +34,6 @@ export class DevCommand extends Command { args: DevOption; async run() { + // nothing } } From 3f94824f10909df4e69c38da7fbc17aa959d0432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=96=E7=8C=A9?= Date: Wed, 21 Dec 2022 21:18:32 +0800 Subject: [PATCH 11/13] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: TZ | 天猪 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 133c0b3..eda64f1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# artus-cli/plugin-help +# @artus-cli/plugin-help Built-in plugin for showing help information in artus-cli From cce21a9b8acf75636a8ebc081661df3e9e75efa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=96=E7=8C=A9?= Date: Wed, 21 Dec 2022 21:19:14 +0800 Subject: [PATCH 12/13] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: TZ | 天猪 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eda64f1..d2621f9 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ $ npm i @artus-cli/plugin-help ## Usage -```typescript +```ts // plugin.ts export default { From 2cf3ab797c9b11e67c314eebea6a8b1467a55cdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=96=E7=8C=A9?= Date: Wed, 21 Dec 2022 21:19:22 +0800 Subject: [PATCH 13/13] Update package.json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: TZ | 天猪 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d0a959c..37321f9 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@artus-cli/plugin-help", "version": "0.0.1-beta.1", - "description": "show help information for artus cli", + "description": "show help information for @artus-cli", "homepage": "", "author": "", "main": "dist/index.js",