Skip to content

Commit

Permalink
refactor dev
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 26, 2024
1 parent 7255dbd commit 996f96a
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 31 deletions.
5 changes: 2 additions & 3 deletions bin/dev.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env -S node --loader ts-node/esm --disable-warning=ExperimentalWarning

// eslint-disable-next-line n/shebang
import {execute} from '@oclif/core'
import { execute } from '@oclif/core';

await execute({development: true, dir: import.meta.url})
await execute({ development: true, dir: import.meta.url });
4 changes: 2 additions & 2 deletions bin/run.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node

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

await execute({dir: import.meta.url})
await execute({ dir: import.meta.url });
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"lint": "eslint --cache src test --ext .ts",
"pretest": "npm run clean && npm run lint -- --fix && npm run prepublishOnly",
"test": "npm run test-local",
"test-local": "node bin/dev.js test",
"test-local": "node bin/run.js test",
"preci": "npm run clean && npm run lint && npm run prepublishOnly",
"cov": "c8 -r lcov -r text-summary -x 'test/**' npm run test-local -- --timeout 120000",
"ci": "npm run cov",
Expand Down
6 changes: 3 additions & 3 deletions src/baseCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { debuglog } from 'node:util';
import { pathToFileURL } from 'node:url';
import path from 'node:path';
import { fork, ForkOptions, ChildProcess } from 'node:child_process';
import { Command, Flags, Interfaces } from '@oclif/core';
import { importResolve } from '@eggjs/utils';
Expand All @@ -9,7 +10,6 @@ import {
getSourceDirname,
readPackageJSON, hasTsConfig,
} from './utils.js';
import path from 'node:path';
import { PackageEgg } from './types.js';

const debug = debuglog('@eggjs/bin/baseCommand');
Expand Down Expand Up @@ -135,7 +135,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {

public async init(): Promise<void> {
await super.init();
debug('raw args: %o', this.argv);
debug('[init] raw args: %o, NODE_ENV: %o', this.argv, this.env.NODE_ENV);
const { args, flags } = await this.parse({
flags: this.ctor.flags,
baseFlags: (super.ctor as typeof BaseCommand).baseFlags,
Expand Down Expand Up @@ -295,7 +295,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {

debug('set NODE_OPTIONS: %o', this.env.NODE_OPTIONS);
debug('after: args: %o, flags: %o', args, flags);
debug('enter real command');
debug('enter real command: %o', this.id);
}

protected async catch(err: Error & {exitCode?: number}): Promise<any> {
Expand Down
107 changes: 92 additions & 15 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,107 @@
import { Args, Command, Flags } from '@oclif/core';
import { debuglog } from 'node:util';
import { Flags } from '@oclif/core';
import { pathToFileURL } from 'node:url';
import { getConfig, getFrameworkPath } from '@eggjs/utils';
import { detect } from 'detect-port';
import { getSourceFilename } from '../utils.js';
import { BaseCommand } from '../baseCommand.js';

export default class Dev extends Command {
static override args = {
file: Args.string({ description: 'file to read' }),
};
const debug = debuglog('@eggjs/bin/commands/dev');

static override description = 'describe the command here';
export default class Dev<T extends typeof Dev> extends BaseCommand<T> {
static override description = 'Start server at local dev mode';

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

static override flags = {
// flag with no value (-f, --force)
force: Flags.boolean({ char: 'f' }),
// flag with a value (-n, --name=VALUE)
name: Flags.string({ char: 'n', description: 'name to print' }),
port: Flags.integer({
description: 'listening port, default to 7001',
char: 'p',
}),
workers: Flags.integer({
char: 'c',
aliases: [ 'cluster' ],
description: 'numbers of app workers',
default: 1,
}),
framework: Flags.string({
description: 'specify framework that can be absolute path or npm package, default is "egg"',
}),
sticky: Flags.boolean({
description: 'start a sticky cluster server',
}),
env: Flags.string({
description: 'specify the NODE_ENV',
}),
};

public async run(): Promise<void> {
const { args, flags } = await this.parse(Dev);
debug('NODE_ENV: %o', this.env);
this.env.NODE_ENV = this.flags.env ?? this.env.NODE_ENV ?? 'development';
this.env.EGG_MASTER_CLOSE_TIMEOUT = '1000';
const serverBin = getSourceFilename('../scripts/start-cluster.mjs');
const eggStartOptions = await this.formatEggStartOptions();
const args = [ JSON.stringify(eggStartOptions) ];
const requires = await this.formatRequires();
const execArgv: string[] = [];
for (const r of requires) {
if (this.isESM) {
execArgv.push('--import');
execArgv.push(pathToFileURL(r).href);
} else {
execArgv.push('--require');
execArgv.push(r);
}
}
await this.forkNode(serverBin, args, { execArgv });
}

const name = flags.name ?? 'world';
this.log(`hello ${name} from /Users/fengmk2/git/github.com/eggjs/bin/src/commands/dev.ts`);
if (args.file && flags.force) {
this.log(`you input --force and --file: ${args.file}`);
protected async formatEggStartOptions() {
const { flags } = this;
flags.framework = getFrameworkPath({
framework: flags.framework,
baseDir: flags.base,
});

if (!flags.port) {
let configuredPort: number | undefined;
try {
const configuration = await getConfig({
framework: flags.framework,
baseDir: flags.base,
env: 'local',
});
configuredPort = configuration?.cluster?.listen?.port;
} catch (err) {
/** skip when failing to read the configuration */
debug('getConfig error: %s, framework: %o, baseDir: %o, env: local',
err, flags.framework, flags.base);
}
if (configuredPort) {
flags.port = configuredPort;
debug(`use port ${flags.port} from configuration file`);
} else {
const defaultPort = parseInt(process.env.EGG_BIN_DEFAULT_PORT ?? '7001');
debug('detect available port');
flags.port = await detect(defaultPort);
if (flags.port !== defaultPort) {
console.warn('[@eggjs/bin] server port %o is unavailable, now using port %o',
defaultPort, flags.port);
}
debug(`use available port ${flags.port}`);
}
}

return {
baseDir: flags.base,
workers: flags.workers,
port: flags.port,
framework: flags.framework,
typescript: flags.typescript,
tscompiler: flags.tscompiler,
sticky: flags.sticky,
};
}
}
2 changes: 1 addition & 1 deletion test/cmd/cov.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getFixtures, getRootDirname } from '../helper.js';
const version = Number(process.version.substring(1, 3));

describe('test/cmd/cov.test.ts', () => {
const eggBin = path.join(getRootDirname(), 'bin/dev.js');
const eggBin = path.join(getRootDirname(), 'bin/run.js');
const cwd = getFixtures('test-files');

async function assertCoverage(baseDir: string) {
Expand Down
2 changes: 1 addition & 1 deletion test/cmd/debug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import coffee from '../coffee.js';
import { getFixtures, getRootDirname } from '../helper.js';

describe('test/cmd/debug.test.ts', () => {
const eggBin = path.join(getRootDirname(), 'bin/dev.js');
const eggBin = path.join(getRootDirname(), 'bin/run.js');
const cwd = getFixtures('demo-app');

it('should startCluster success', () => {
Expand Down
17 changes: 14 additions & 3 deletions test/cmd/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getRootDirname, getFixtures } from '../helper.js';
const version = Number(process.version.substring(1, 3));

describe('test/cmd/dev.test.ts', () => {
const eggBin = path.join(getRootDirname(), 'bin/dev.js');
const eggBin = path.join(getRootDirname(), 'bin/run.js');
const cwd = getFixtures('demo-app');

it('should startCluster success', () => {
Expand All @@ -25,9 +25,20 @@ describe('test/cmd/dev.test.ts', () => {
.end();
});

it('should dev start with custom NODE_ENV', () => {
it.only('should dev start with custom NODE_ENV', () => {

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18.19.0)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18.19.0)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 20)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 20)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 22)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 22)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18.19.0)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18.19.0)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 23)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 23)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 23)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 23)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 20)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 20)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18.19.0)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18.19.0)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 22)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 22)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 23)

it.only not permitted

Check warning on line 28 in test/cmd/dev.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 23)

it.only not permitted
return coffee.fork(eggBin, [ 'dev' ], { cwd, env: { NODE_ENV: 'prod' } })
// .debug()
.debug()
.expect('stdout', /"workers":1/)
.expect('stdout', /"baseDir":".*?demo-app"/)
.expect('stdout', /"framework":".*?aliyun-egg"/)
.expect('stdout', /NODE_ENV: prod/)
.expect('code', 0)
.end();
});

it('should dev start with --env prod', () => {
return coffee.fork(eggBin, [ 'dev', '--env', 'prod' ], { cwd })
.debug()
.expect('stdout', /"workers":1/)
.expect('stdout', /"baseDir":".*?demo-app"/)
.expect('stdout', /"framework":".*?aliyun-egg"/)
Expand Down
2 changes: 1 addition & 1 deletion test/cmd/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getFixtures, getRootDirname } from '../helper.js';
const version = Number(process.version.substring(1, 3));

describe('test/cmd/test.test.ts', () => {
const eggBin = path.join(getRootDirname(), 'bin/dev.js');
const eggBin = path.join(getRootDirname(), 'bin/run.js');
const cwd = getFixtures('test-files');

describe('egg-bin test', () => {
Expand Down
1 change: 1 addition & 0 deletions test/coffee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default {
PATH: process.env.PATH,
...options.env,
};
// console.error('fork env: %o', options.env);
return coffee.fork(modulePath, args, options);
},
};
2 changes: 1 addition & 1 deletion test/egg-bin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import coffee from './coffee.js';
import { getRootDirname, getFixtures } from './helper.js';

describe('test/egg-bin.test.ts', () => {
const eggBin = path.join(getRootDirname(), 'bin/dev.js');
const eggBin = path.join(getRootDirname(), 'bin/run.js');
const cwd = getFixtures('test-files');

describe('global options', () => {
Expand Down

0 comments on commit 996f96a

Please sign in to comment.