{"id":22070005,"url":"https://github.com/tashuo/nest-command","last_synced_at":"2025-07-24T08:35:32.478Z","repository":{"id":179099644,"uuid":"662950225","full_name":"tashuo/nest-command","owner":"tashuo","description":"NestJS command tool","archived":false,"fork":false,"pushed_at":"2023-07-10T16:08:49.000Z","size":196,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-21T23:27:48.651Z","etag":null,"topics":["command","console","nestjs","yargs"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/nest-commands","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tashuo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-06T08:30:41.000Z","updated_at":"2023-07-09T06:44:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"9a49c3ab-1137-42e9-ab32-0d6980007179","html_url":"https://github.com/tashuo/nest-command","commit_stats":null,"previous_names":["tashuo/nestjs-cli","tashuo/nest-command"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tashuo%2Fnest-command","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tashuo%2Fnest-command/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tashuo%2Fnest-command/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tashuo%2Fnest-command/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tashuo","download_url":"https://codeload.github.com/tashuo/nest-command/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227422095,"owners_count":17775014,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["command","console","nestjs","yargs"],"created_at":"2024-11-30T20:14:23.456Z","updated_at":"2024-11-30T20:14:24.191Z","avatar_url":"https://github.com/tashuo.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Description\n\nNestJS Command tool, based on [yargs](https://github.com/yargs/yargs).\n\n## Dependency\n* version 1.* for nestjs 7\n* version 2.* for nestjs 8\n* version 3.* for nestjs 9\n* version 4.* for nestjs 10\n\n## Installation\n\n```bash\n$ npm install --save nest-commands\n\n# with typescript\n$ npm install --save-dev @types/yargs\n```\n\n## Quick Start\n1. register `CommandModule` to `AppModule`\n\n    ```typescript\n    import { Module } from '@nestjs/common';\n    import { CommandModule } from 'nest-commands';\n\n    @Module({\n        imports: [CommandModule]\n    })\n    export class AppModule {}\n    ```\n2. create a cli entrypoint\n\n    ```typescript\n    # src/console.ts\n    import { NestFactory } from '@nestjs/core';\n    import { AppModule } from './app.module';\n    import { CommandModule, CommandService } from 'nestjs-commands';\n    \n    async function bootstrap() {\n        try {\n            const app = await NestFactory.createApplicationContext(AppModule, {\n                // logger: ['debug'],\n                logger: false,\n            });\n            await app.select(CommandModule).get(CommandService).init().exec();\n        } catch (error) {\n            console.error(error);\n        }\n        process.exit(1);\n    }\n    \n    bootstrap();\n    \n    ```\n3. cli config\n    \n    add the two line to `package.json`\n\n    ```json\n    {\n        \"console:dev\": \"ts-node -r tsconfig-paths/register src/console.ts\",\n        \"console\": \"node dist/console.js\"\n   }\n    ```\n    \n    and now you can run `npm run console:dev --help` to see all the commands and helps\n\n4. custom command example\n\n     1. create a command provider\n\n        ```typescript\n        # ./src/simple.command.ts\n        import { Injectable } from '@nestjs/common';\n        import { Command } from 'nestjs-commands';\n        \n        @Injectable()\n        export class SimpleCommand {\n            @Command({\n                command: 'simple:test1 \u003cp1\u003e',\n                describe: 'simple:test1',\n                params: [\n                    {\n                        name: 'p1',\n                        type: 'positional',\n                        value: {\n                            type: 'string'\n                        }\n                    }\n                ]\n            })\n            test1(p1: string) {\n                console.log(`test1:${p1}`);\n            }\n        \n            @Command()\n            async test2(p1: number, p2 = 'abc') {\n                console.log(`test2:${p1}:${p2}`);\n            }\n        }\n\n        ```\n\n        如上所示，假如`@Command`无配置params参数，会自动获取方法的参数并注册至yargs，但由于无法获取运行时的参数名所以自动注册的参数名无任何逻辑意义，直接看下面help输出中的*test2*就懂啥意思了，但也算为了偷懒少写代码提供了一条不归路：\n        ```shell\n        $ npm run console:dev --help\n        Commands:\n            cli simple:test1 \u003cp1\u003e           simple:test1\n            cli test2 \u003cnumber1\u003e [unknown2]  test2(auto generated)\n        ```\n    \n    2. register the command as a provider\n\n        ```typescript\n        # app.module.ts\n        import { Module } from '@nestjs/common';\n        import { CommandModule } from 'nest-commands';\n        import { SimpleCommand } from './simple.command';\n    \n        @Module({\n            imports: [CommandModule],\n            providers: [SimpleCommand],\n        })\n        export class AppModule {}\n        ```\n\n    3. run the command\n\n        ```shell\n        $ npm run console:dev simple:test1 abc // console.log('test1:abc');\n    \n        $ npm run console:dev test2 123 // console.log('test2:123:abc');\n        ```\n\n5. more usage examples\n\n    1. multiple commands\n        \n        ```typescript\n        # ./test/multiple.command.ts\n        import { Injectable } from '@nestjs/common';\n        import { Command, Commands } from '../src/decorators';\n\n        @Injectable()\n        @Commands()\n        export class MultipleCommand {\n            async test3() {\n                return new Promise\u003cstring\u003e((resolve) =\u003e setTimeout(() =\u003e resolve('test3'), 0));\n            }\n        \n            test4() {\n                return 'test4';\n            }\n        \n            @Command({\n                command: 'multiple:test5'\n            })\n            test5() {\n                return 'test5';\n            }\n        }\n\n        ```\n\n        使用`@Commands`装饰的类会自动将所有的方法注册为独立的command，**便于批量注册**，但由于typescript装饰器只有编译时才会封装`design:*`等metadata信息，动态装饰器无法获取方法的参数，所以**此种方式只适合不需参数的方法**\n\n    \n    2. original yargs command\n\n        具体示例可查看[origin.command.ts](https://github.com/tashuo/nest-command/blob/master/test/original.command.ts)\n\n        使用`@OriginYargsCommand`装饰的类也可以直接注册为command，**便于复用已有的yargs command**，所以必须保证该类继承了`yargs.CommandModule`，是一个标准的yargs command\n\n        为啥会有这样一个feature呢，初衷是想重载`typeorm`的一些内置command，后面发现这些command没有export...先留着吧\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftashuo%2Fnest-command","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftashuo%2Fnest-command","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftashuo%2Fnest-command/lists"}