Skip to content

Commit

Permalink
add my-egg-bin demo
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 26, 2024
1 parent d7b3540 commit f26d53f
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 82 deletions.
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`.

Expand All @@ -103,8 +103,6 @@ Create `.vscode/launch.json` file:
],
"console": "integratedTerminal",
"restart": true,
"protocol": "auto",
"port": 9229,
"autoAttachChildProcesses": true
},
{
Expand All @@ -118,8 +116,6 @@ Create `.vscode/launch.json` file:
"--",
"--inspect-brk"
],
"protocol": "auto",
"port": 9229,
"autoAttachChildProcesses": true
}
]
Expand Down Expand Up @@ -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`
Expand All @@ -210,7 +206,7 @@ COV_EXCLUDES="app/plugins/c*,app/autocreate/**" egg-bin cov

## Custom egg-bin for your team

See <https://artus-cli.github.io>
See <https://oclif.io/docs/configuring_your_cli/>

## License

Expand All @@ -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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions src/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export interface ForkNodeOptions extends ForkOptions {
dryRun?: boolean;
}

export type Flags<T extends typeof Command> = Interfaces.InferredFlags<typeof BaseCommand['baseFlags'] & T['flags']>;
export type Args<T extends typeof Command> = Interfaces.InferredArgs<T['args']>;
type Flags<T extends typeof Command> = Interfaces.InferredFlags<typeof BaseCommand['baseFlags'] & T['flags']>;
type Args<T extends typeof Command> = Interfaces.InferredArgs<T['args']>;

export abstract class BaseCommand<T extends typeof Command> extends Command {
// add the --json flag
Expand Down Expand Up @@ -89,12 +89,12 @@ export abstract class BaseCommand<T extends typeof Command> 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({
Expand Down Expand Up @@ -160,7 +160,7 @@ export abstract class BaseCommand<T extends typeof Command> 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;
Expand Down
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
4 changes: 2 additions & 2 deletions test/fixtures/example-ts-cluster/node_modules/egg/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions test/fixtures/my-egg-bin/bin/my-egg-bin.ts

This file was deleted.

3 changes: 3 additions & 0 deletions test/fixtures/my-egg-bin/bin/run.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off

node "%~dp0\run" %*
5 changes: 5 additions & 0 deletions test/fixtures/my-egg-bin/bin/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

import { execute } from '@oclif/core';

await execute({ dir: import.meta.url });
3 changes: 3 additions & 0 deletions test/fixtures/my-egg-bin/cmd/cov.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Cov } from '../../../../dist/esm/index.js';

export default Cov;
19 changes: 10 additions & 9 deletions test/fixtures/my-egg-bin/cmd/dev.ts
Original file line number Diff line number Diff line change
@@ -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<T extends typeof MyDev> extends Dev<T> {
static override description = 'Run the development server with my-egg-bin';

static override examples = [
'<%= config.bin %> <%= command.id %>',
];

public async run(): Promise<void> {
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);
}
}
30 changes: 20 additions & 10 deletions test/fixtures/my-egg-bin/cmd/nsp.ts
Original file line number Diff line number Diff line change
@@ -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<T extends typeof Nsp> extends BaseCommand<T> {
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<void> {
console.log('run nsp check at baseDir: %s, with %o', this.flags.base, this.args);
if (this.flags.foo) {
console.log('foo is true');
}
}
}
3 changes: 3 additions & 0 deletions test/fixtures/my-egg-bin/cmd/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Test } from '../../../../dist/esm/index.js';

export default Test;
13 changes: 11 additions & 2 deletions test/fixtures/my-egg-bin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}
33 changes: 19 additions & 14 deletions test/my-egg-bin.test.ts
Original file line number Diff line number Diff line change
@@ -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/)
Expand All @@ -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();

Expand All @@ -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();
});
Expand All @@ -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();
});
Expand Down
Loading

0 comments on commit f26d53f

Please sign in to comment.