From f26d53fbd43fedf0080b5a215d891c45f509c0db Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 26 Dec 2024 23:24:40 +0800 Subject: [PATCH] add my-egg-bin demo --- README.md | 15 +++------ package.json | 1 + src/baseCommand.ts | 8 ++--- src/index.ts | 10 ++++-- .../node_modules/egg/index.js | 4 +-- test/fixtures/my-egg-bin/bin/my-egg-bin.ts | 15 --------- test/fixtures/my-egg-bin/bin/run.cmd | 3 ++ test/fixtures/my-egg-bin/bin/run.js | 5 +++ test/fixtures/my-egg-bin/cmd/cov.ts | 3 ++ test/fixtures/my-egg-bin/cmd/dev.ts | 19 ++++++----- test/fixtures/my-egg-bin/cmd/nsp.ts | 30 +++++++++++------ test/fixtures/my-egg-bin/cmd/test.ts | 3 ++ test/fixtures/my-egg-bin/package.json | 13 ++++++-- test/my-egg-bin.test.ts | 33 +++++++++++-------- test/ts.test.ts | 27 +++++++-------- 15 files changed, 107 insertions(+), 82 deletions(-) delete mode 100644 test/fixtures/my-egg-bin/bin/my-egg-bin.ts create mode 100644 test/fixtures/my-egg-bin/bin/run.cmd create mode 100644 test/fixtures/my-egg-bin/bin/run.js create mode 100644 test/fixtures/my-egg-bin/cmd/cov.ts create mode 100644 test/fixtures/my-egg-bin/cmd/test.ts diff --git a/README.md b/README.md index 982b5568..be79bed7 100644 --- a/README.md +++ b/README.md @@ -21,14 +21,14 @@ [node-version-image]: https://img.shields.io/node/v/@eggjs/bin.svg?style=flat-square [node-version-url]: https://nodejs.org/en/download/ -egg developer tool, extends [@artus-cli/artus-cli]. +egg developer tool, base on [oclif](https://oclif.io/). --- ## Install ```bash -npm i egg-bin --save-dev +npm i @eggjs/bin --save-dev ``` ## Usage @@ -78,7 +78,7 @@ egg-bin dev #### dev options - `--framework` egg web framework root path. -- `--port` server port. If not specified, the port is obtained in the following order: [_egg.js_ configuration](https://www.eggjs.org/basics/config) `config/config.*.js` > `process.env.EGG_BIN_DEFAULT_PORT` > 7001 > other available ports. +- `--port` server port. If not specified, the port is obtained in the following order: [_egg.js_ configuration](https://eggjs.org/basics/config) `config/config.*.js` > `process.env.EGG_BIN_DEFAULT_PORT` > 7001 > other available ports. - `--workers` worker process number, default to `1` worker at local mode. - `--sticky` start a sticky cluster server, default to `false`. @@ -103,8 +103,6 @@ Create `.vscode/launch.json` file: ], "console": "integratedTerminal", "restart": true, - "protocol": "auto", - "port": 9229, "autoAttachChildProcesses": true }, { @@ -118,8 +116,6 @@ Create `.vscode/launch.json` file: "--", "--inspect-brk" ], - "protocol": "auto", - "port": 9229, "autoAttachChildProcesses": true } ] @@ -192,7 +188,7 @@ You can pass any mocha argv. - `-x` add dir ignore coverage, support multiple argv - `--prerequire` prerequire files for coverage instrument, you can use this options if load files slowly when call `mm.app` or `mm.cluster` -- `--typescript` / `--ts` enable typescript support. If true, will auto add `.ts` extension and ignore `typings` and `d.ts`. +- `--typescript` enable typescript support. If `true`, will auto add `.ts` extension and ignore `typings` and `d.ts`. - `--c8` c8 instruments passthrough. you can use this to overwrite egg-bin's default c8 instruments and add additional ones. > > - egg-bin have some default instruments passed to c8 like `-r` and `--temp-directory` @@ -210,7 +206,7 @@ COV_EXCLUDES="app/plugins/c*,app/autocreate/**" egg-bin cov ## Custom egg-bin for your team -See +See ## License @@ -224,4 +220,3 @@ Made with [contributors-img](https://contrib.rocks). [mocha]: https://mochajs.org [glob]: https://github.com/isaacs/node-glob -[@artus-cli/artus-cli]: https://github.com/artus-cli/artus-cli diff --git a/package.json b/package.json index 0c8d4b44..f470c690 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "esbuild-register": "^3.4.2", "eslint": "8", "eslint-config-egg": "14", + "npminstall": "^7.12.0", "rimraf": "6", "tshy": "3", "tshy-after": "1", diff --git a/src/baseCommand.ts b/src/baseCommand.ts index 240ed384..4e13f898 100644 --- a/src/baseCommand.ts +++ b/src/baseCommand.ts @@ -53,8 +53,8 @@ export interface ForkNodeOptions extends ForkOptions { dryRun?: boolean; } -export type Flags = Interfaces.InferredFlags; -export type Args = Interfaces.InferredArgs; +type Flags = Interfaces.InferredFlags; +type Args = Interfaces.InferredArgs; export abstract class BaseCommand extends Command { // add the --json flag @@ -89,12 +89,12 @@ export abstract class BaseCommand extends Command { tscompiler: Flags.string({ helpGroup: 'GLOBAL', summary: 'TypeScript compiler, like ts-node/register', + aliases: [ 'tsc' ], }), // flag with no value (--typescript) typescript: Flags.boolean({ helpGroup: 'GLOBAL', description: '[default: true] use TypeScript to run the test', - aliases: [ 'ts' ], allowNo: true, }), ts: Flags.string({ @@ -160,7 +160,7 @@ export abstract class BaseCommand extends Command { this.pkgEgg = pkg.egg ?? {}; flags.tscompiler = flags.tscompiler ?? this.env.TS_COMPILER ?? this.pkgEgg.tscompiler; - let typescript: boolean = args.typescript; + let typescript: boolean = flags.typescript; // keep compatible with old ts flag: `--ts=true` or `--ts=false` if (flags.ts === 'true') { typescript = true; diff --git a/src/index.ts b/src/index.ts index 46d8f993..577e9db1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,9 @@ -export * from './commands/dev.js'; -export * from './commands/test.js'; -export * from './commands/cov.js'; +import Test from './commands/test.js'; +import Cov from './commands/cov.js'; +import Dev from './commands/dev.js'; + +export { Test, Cov, Dev }; + export * from './baseCommand.js'; export * from './types.js'; +export * from '@oclif/core'; diff --git a/test/fixtures/example-ts-cluster/node_modules/egg/index.js b/test/fixtures/example-ts-cluster/node_modules/egg/index.js index defb6ec1..82007546 100644 --- a/test/fixtures/example-ts-cluster/node_modules/egg/index.js +++ b/test/fixtures/example-ts-cluster/node_modules/egg/index.js @@ -1,8 +1,8 @@ -'use strict'; - module.exports = require('../../../../../node_modules/egg'); setTimeout(() => { + // console.log(process.execArgv) + // console.log(process.env); console.log('exit by master test end'); process.exit(0); }, require('os').platform() === 'win32' ? 14000 : 10000); diff --git a/test/fixtures/my-egg-bin/bin/my-egg-bin.ts b/test/fixtures/my-egg-bin/bin/my-egg-bin.ts deleted file mode 100644 index 80072d7c..00000000 --- a/test/fixtures/my-egg-bin/bin/my-egg-bin.ts +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env node - -import path from 'node:path'; -import { fileURLToPath } from 'node:url'; -import { start } from '@artus-cli/artus-cli'; - -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); -const baseDir = path.join(__dirname, '..'); - -console.error(baseDir); - -start({ - baseDir, -}); diff --git a/test/fixtures/my-egg-bin/bin/run.cmd b/test/fixtures/my-egg-bin/bin/run.cmd new file mode 100644 index 00000000..968fc307 --- /dev/null +++ b/test/fixtures/my-egg-bin/bin/run.cmd @@ -0,0 +1,3 @@ +@echo off + +node "%~dp0\run" %* diff --git a/test/fixtures/my-egg-bin/bin/run.js b/test/fixtures/my-egg-bin/bin/run.js new file mode 100644 index 00000000..176d2af5 --- /dev/null +++ b/test/fixtures/my-egg-bin/bin/run.js @@ -0,0 +1,5 @@ +#!/usr/bin/env node + +import { execute } from '@oclif/core'; + +await execute({ dir: import.meta.url }); diff --git a/test/fixtures/my-egg-bin/cmd/cov.ts b/test/fixtures/my-egg-bin/cmd/cov.ts new file mode 100644 index 00000000..8affc616 --- /dev/null +++ b/test/fixtures/my-egg-bin/cmd/cov.ts @@ -0,0 +1,3 @@ +import { Cov } from '../../../../dist/esm/index.js'; + +export default Cov; diff --git a/test/fixtures/my-egg-bin/cmd/dev.ts b/test/fixtures/my-egg-bin/cmd/dev.ts index 9dff18aa..510fd9b3 100644 --- a/test/fixtures/my-egg-bin/cmd/dev.ts +++ b/test/fixtures/my-egg-bin/cmd/dev.ts @@ -1,13 +1,14 @@ -import { DefineCommand } from '@artus-cli/artus-cli'; -import { DevCommand as BaseDevCommand } from '../../../../'; +import { Dev } from '../../../../dist/esm/index.js'; -@DefineCommand({ - command: 'dev', - description: 'Run the development server with my-egg-bin', -}) -export class DevCommand extends BaseDevCommand { - async run() { +export default class MyDev extends Dev { + static override description = 'Run the development server with my-egg-bin'; + + static override examples = [ + '<%= config.bin %> <%= command.id %>', + ]; + + public async run(): Promise { super.run(); - console.info('this is my-egg-bin dev, baseDir: %s', this.base); + console.info('this is my-egg-bin dev, baseDir: %s', this.flags.base); } } diff --git a/test/fixtures/my-egg-bin/cmd/nsp.ts b/test/fixtures/my-egg-bin/cmd/nsp.ts index fb9cec64..905560b0 100644 --- a/test/fixtures/my-egg-bin/cmd/nsp.ts +++ b/test/fixtures/my-egg-bin/cmd/nsp.ts @@ -1,12 +1,22 @@ -import { DefineCommand } from '@artus-cli/artus-cli'; -import { BaseCommand } from '../../../../src/index'; - -@DefineCommand({ - command: 'nsp', - description: 'nsp check', -}) -export class NspCommand extends BaseCommand { - async run() { - console.log('run nsp check at baseDir: %s, with %o', this.base, this.ctx.args); +import { BaseCommand, Flags } from '../../../../dist/esm/index.js'; + +export default class Nsp extends BaseCommand { + static override description = 'nsp check'; + + static override examples = [ + '<%= config.bin %> <%= command.id %>', + ]; + + static override flags = { + foo: Flags.boolean({ + description: 'foo bar', + }), + }; + + public async run(): Promise { + console.log('run nsp check at baseDir: %s, with %o', this.flags.base, this.args); + if (this.flags.foo) { + console.log('foo is true'); + } } } diff --git a/test/fixtures/my-egg-bin/cmd/test.ts b/test/fixtures/my-egg-bin/cmd/test.ts new file mode 100644 index 00000000..d1bb394a --- /dev/null +++ b/test/fixtures/my-egg-bin/cmd/test.ts @@ -0,0 +1,3 @@ +import { Test } from '../../../../dist/esm/index.js'; + +export default Test; diff --git a/test/fixtures/my-egg-bin/package.json b/test/fixtures/my-egg-bin/package.json index 88b99948..06020130 100644 --- a/test/fixtures/my-egg-bin/package.json +++ b/test/fixtures/my-egg-bin/package.json @@ -2,7 +2,16 @@ "name": "my-egg-bin", "version": "2.3.4", "bin": { - "my-egg-bin": "bin/my-egg-bin.js" + "my-egg-bin": "bin/run.js" }, - "type": "module" + "type": "module", + "oclif": { + "bin": "my-egg-bin", + "commands": "./cmd", + "dirname": "my-egg-bin", + "topicSeparator": " ", + "additionalHelpFlags": [ + "-h" + ] + } } diff --git a/test/my-egg-bin.test.ts b/test/my-egg-bin.test.ts index 8ffe0804..e77f8566 100644 --- a/test/my-egg-bin.test.ts +++ b/test/my-egg-bin.test.ts @@ -1,13 +1,13 @@ import coffee from './coffee.js'; import { getFixtures } from './helper.js'; -describe.skip('test/my-egg-bin.test.ts', () => { - const eggBin = getFixtures('my-egg-bin/bin/my-egg-bin.ts'); +describe('test/my-egg-bin.test.ts', () => { + const eggBin = getFixtures('my-egg-bin/bin/run.js'); const cwd = getFixtures('test-files'); it('should my-egg-bin test success', () => { return coffee.fork(eggBin, [ 'test' ], { cwd, env: { TESTS: 'test/**/*.test.js' } }) - .debug() + // .debug() .expect('stdout', /should success/) .expect('stdout', /a.test.js/) .expect('stdout', /b\/b.test.js/) @@ -19,7 +19,7 @@ describe.skip('test/my-egg-bin.test.ts', () => { it('should my-egg-bin nsp success', async () => { await coffee.fork(eggBin, [ 'nsp', '-h' ], { cwd }) // .debug() - .expect('stdout', /-baseDir, --base string/) + .expect('stdout', /nsp check/) .expect('code', 0) .end(); @@ -28,25 +28,30 @@ describe.skip('test/my-egg-bin.test.ts', () => { .expect('stdout', /run nsp check at baseDir: .+test\-files, with/) .expect('code', 0) .end(); + + await coffee.fork(eggBin, [ 'nsp', '--foo' ], { cwd }) + // .debug() + .expect('stdout', /run nsp check at baseDir: .+test\-files, with/) + .expect('stdout', /foo is true/) + .expect('code', 0) + .end(); }); it('should show help', async () => { await coffee.fork(eggBin, [ '--help' ], { cwd }) // .debug() - .expect('stdout', /Usage: my-egg-bin/) - .expect('stdout', /Available Commands/) - .expect('stdout', /test \[files\.\.\.]\s+Run the test/) - .expect('stdout', /-ts, --typescript\s+whether enable typescript support/) - .expect('stdout', /nsp\s+nsp check/) + .expect('stdout', /\$ my-egg-bin \[COMMAND]/) + .expect('stdout', /COMMANDS/) + .expect('stdout', /test {2}Run the test/) + .expect('stdout', /nsp {3}nsp check/) .expect('code', 0) .end(); await coffee.fork(eggBin, [ 'dev', '-h' ], { cwd }) // .debug() - .expect('stdout', /Usage: my-egg-bin/) - .expect('stdout', /dev\s+Run the development server with my-egg-bin/) - .expect('stdout', /-p, --port number/) - .expect('stdout', /-ts, --typescript\s+whether enable typescript support/) + .expect('stdout', /Run the development server with my-egg-bin/) + .expect('stdout', /listening port, default to 7001/) + .expect('stdout', /TypeScript compiler, like ts-node\/register/) .expect('code', 0) .end(); }); @@ -64,7 +69,7 @@ describe.skip('test/my-egg-bin.test.ts', () => { it('should show version 2.3.4', () => { return coffee.fork(eggBin, [ '--version' ], { cwd }) // .debug() - .expect('stdout', '2.3.4\n') + .expect('stdout', /my-egg-bin\/2\.3\.4 /) .expect('code', 0) .end(); }); diff --git a/test/ts.test.ts b/test/ts.test.ts index d54764e0..68ebd13b 100644 --- a/test/ts.test.ts +++ b/test/ts.test.ts @@ -197,8 +197,8 @@ describe('test/ts.test.ts', () => { .end(); }); - it('should fail start app with --no-ts', () => { - return coffee.fork(eggBin, [ 'dev', '--no-ts' ], { cwd }) + it('should fail start app with --no-typescript', () => { + return coffee.fork(eggBin, [ 'dev', '--no-typescript' ], { cwd }) // .debug() .expect('stdout', /agent.options.typescript = false/) .expect('stdout', /started/) @@ -229,9 +229,9 @@ describe('test/ts.test.ts', () => { await fs.rm(path.join(cwd, 'node_modules'), { force: true, recursive: true }); if (process.env.CI) { // don't use npmmirror.com on CI - await runScript('npx npminstall', { cwd }); + await runScript('npminstall', { cwd }); } else { - await runScript('npx npminstall -c', { cwd }); + await runScript('npminstall -c', { cwd }); } // copy egg to node_modules @@ -243,11 +243,12 @@ describe('test/ts.test.ts', () => { const { stderr, code } = await coffee.fork(eggBin, [ 'dev', '--tsc', 'ts-node/register' ], { cwd, env: { - NODE_DEBUG: 'egg-bin*', + NODE_DEBUG: '@eggjs/bin*', }, }) // .debug() .end(); + // @EGGJS/BIN/BASECOMMAND 15959: set NODE_OPTIONS: '--require /Users/fengmk2/git/github.com/eggjs/bin/node_modules/.store/ts-node@10.9.2/node_modules/ts-node/register/index.js' assert.match(stderr, /ts-node@10\.\d+\.\d+/); assert.equal(code, 0); }); @@ -260,9 +261,9 @@ describe('test/ts.test.ts', () => { await fs.rm(path.join(cwd, 'node_modules'), { force: true, recursive: true }); if (process.env.CI) { // don't use npmmirror.com on CI - await runScript('npx npminstall ts-node@10.9.2 --no-save', { cwd }); + await runScript('npminstall ts-node@10.9.2 --no-save', { cwd }); } else { - await runScript('npx npminstall -c ts-node@10.9.2 --no-save', { cwd }); + await runScript('npminstall -c ts-node@10.9.2 --no-save', { cwd }); } // copy egg to node_modules @@ -272,11 +273,11 @@ describe('test/ts.test.ts', () => { ); const { stderr, code } = await coffee.fork(eggBin, [ - 'dev', '--ts', '--tscompiler=ts-node/register', + 'dev', '--tscompiler=ts-node/register', ], { cwd, env: { - NODE_DEBUG: 'egg-bin*', + NODE_DEBUG: '@eggjs/bin*', }, }) // .debug() @@ -388,10 +389,10 @@ describe('test/ts.test.ts', () => { // install custom ts-node await fs.rm(path.join(cwd, 'node_modules'), { force: true, recursive: true }); if (process.env.CI) { - // dont use npmmirror.com on CI - await runScript('npx npminstall', { cwd }); + // don't use npmmirror.com on CI + await runScript('npminstall', { cwd }); } else { - await runScript('npx npminstall -c', { cwd }); + await runScript('npminstall -c', { cwd }); } // copy egg to node_modules @@ -403,7 +404,7 @@ describe('test/ts.test.ts', () => { const { stdout, code } = await coffee.fork(eggBin, [ 'test', '--tsc', 'ts-node/register' ], { cwd, env: { - NODE_DEBUG: 'egg-bin*', + NODE_DEBUG: '@eggjs/bin*', }, }) // .debug()