generated from artus-cli/template
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: first impl #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5b5b45b
feat: first impl
whxaxes fe0d180
feat: first impl
whxaxes 937e3c3
feat: update name
whxaxes 787e388
chore: update readme
whxaxes 835d00a
chore: update readme
whxaxes c2d3d55
feat: add release yml
whxaxes edcd52b
chore: update pkg
whxaxes c1397c3
chore: update release.yml
whxaxes 59623b2
fix: lint
whxaxes a3e4837
fix: lint
whxaxes 3f94824
Update README.md
whxaxes cce21a9
Update README.md
whxaxes 2cf3ab7
Update package.json
whxaxes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| 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 }} | ||
| GIT_TOKEN: ${{ secrets.GIT_TOKEN }} | ||
| # with: | ||
| # checkTest: false | ||
| # dryRun: true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| package-lock=false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,36 @@ | ||
| # artus-cli/template | ||
| # @artus-cli/plugin-help | ||
|
|
||
| template repository for artus-cli | ||
| Built-in plugin for showing help information in artus-cli | ||
|
|
||
| <!-- Badge,自行替换掉下面的 `artus-cli/artus-cli` 占位符--> | ||
| [](https://npmjs.org/package/@artus-cli/plugin-help) | ||
| [](https://npmjs.org/package/@artus-cli/plugin-help) | ||
| [](https://npmjs.org/package/@artus-cli/plugin-help) | ||
| [](https://github.com/artus-cli/plugin-help/actions/workflows/ci.yml) | ||
| [](https://codecov.io/gh/artus-cli/plugin-help) | ||
| [](https://ossinsight.io/analyze/artus-cli/plugin-help) | ||
|
|
||
| [](https://npmjs.org/package/@artus-cli/artus-cli) | ||
| [](https://npmjs.org/package/@artus-cli/artus-cli) | ||
| [](https://npmjs.org/package/@artus-cli/artus-cli) | ||
| [](https://github.com/artus-cli/artus-cli/actions/workflows/ci.yml) | ||
| [](https://codecov.io/gh/artus-cli/artus-cli) | ||
| [](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 | ||
| ```ts | ||
| // plugin.ts | ||
|
|
||
| export default { | ||
| help: { | ||
| enable: true, | ||
| package: '@artus-cli/plugin-help', | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| ## Contributing | ||
|
|
||
| ```sh | ||
| $ npm test | ||
| $ npm run cov | ||
|
|
||
| $ npx ts-node src/bin/cli.ts dev --port=8080 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 { | ||
whxaxes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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[] = []; | ||
whxaxes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const optionKeys = helpCommand.options ? Object.keys(helpCommand.options) : []; | ||
|
|
||
| // usage info in first line | ||
| displayTexts.push(`Usage: ${helpCommand.command.startsWith(bin) ? '' : `${bin} `}${helpCommand.command}`); | ||
atian25 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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')); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1 @@ | ||
| export * from './cmd/dev'; | ||
| export * from './cmd/debug'; | ||
| export * from './help'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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`); | ||
whxaxes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| process.exit(1); | ||
| } | ||
|
|
||
| return next(); | ||
| } | ||
|
|
||
| // redirect to help command | ||
| const utils = ctx.container.get(Utils); | ||
| await utils.redirect([ 'help', fuzzyMatched.uid ]); | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "name": "help" | ||
| } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { start } from '@artus-cli/artus-cli'; | ||
|
|
||
| start(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { DefineCommand, DefineOption } from '@artus-cli/artus-cli'; | ||
| import { TestCommand, TestOption } from './test'; | ||
|
|
||
| interface CovOption extends TestOption { | ||
| c8?: boolean; | ||
| } | ||
|
|
||
| @DefineCommand({ | ||
| command: 'cov <baseDir> [file...]', | ||
| description: 'Run the coverage', | ||
| }) | ||
| export class CovCommand extends TestCommand { | ||
| @DefineOption<CovOption>({ | ||
| c8: { | ||
| type: 'boolean', | ||
| default: true, | ||
| }, | ||
| }) | ||
| args: CovOption; | ||
|
|
||
| async run() { | ||
| // nothing | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.