Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 27, 2024
1 parent b2d53a8 commit ae0db57
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 22 deletions.
15 changes: 15 additions & 0 deletions scripts/start-cluster.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { debuglog } = require('node:util');
const { importModule } = require('@eggjs/utils');

const debug = debuglog('@eggjs/bin/scripts/start-cluster');

async function main() {
debug('argv: %o', process.argv);
const options = JSON.parse(process.argv[2]);
debug('start cluster options: %o', options);
const { startCluster } = await importModule(options.framework);
await startCluster(options);
}

main();
48 changes: 30 additions & 18 deletions src/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
char: 'r',
multiple: true,
}),
import: Flags.string({
helpGroup: 'GLOBAL',
summary: 'import the given module, only work on ESM',
multiple: true,
}),
base: Flags.string({
helpGroup: 'GLOBAL',
summary: 'directory of application',
Expand Down Expand Up @@ -154,7 +159,6 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
if (!path.isAbsolute(flags.base)) {
flags.base = path.join(process.cwd(), flags.base);
}
debug('baseDir: %o', flags.base);
const pkg = await readPackageJSON(flags.base);
this.pkg = pkg;
this.pkgEgg = pkg.egg ?? {};
Expand Down Expand Up @@ -287,6 +291,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
debug('set timeout = false when process.env.JB_DEBUG_FILE=%o', this.env.JB_DEBUG_FILE);
}

debug('baseDir: %o, isESM: %o', flags.base, this.isESM);
debug('set NODE_OPTIONS: %o', this.env.NODE_OPTIONS);
debug('after: args: %o, flags: %o', args, flags);
debug('enter real command: %o', this.id);
Expand All @@ -305,15 +310,21 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {

protected async formatRequires(): Promise<string[]> {
const requires = this.flags.require ?? [];
const eggRequire = this.pkgEgg.require;
if (Array.isArray(eggRequire)) {
for (const r of eggRequire) {
requires.push(r);
}
} else if (typeof eggRequire === 'string' && eggRequire) {
requires.push(eggRequire);
const imports = this.flags.import ?? [];
let eggRequires = this.pkgEgg.require as string[] ?? [];
if (typeof eggRequires === 'string') {
eggRequires = [ eggRequires ];
}
return requires;
let eggImports = this.pkgEgg.import as string[] ?? [];
if (typeof eggImports === 'string') {
eggImports = [ eggImports ];
}
return [
...requires,
...imports,
...eggRequires,
...eggImports,
];
}

protected formatImportModule(modulePath: string) {
Expand All @@ -338,15 +349,18 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
...this.env,
...options.env,
};
const NODE_OPTIONS = env.NODE_OPTIONS ? `NODE_OPTIONS='${env.NODE_OPTIONS}' ` : '';
if (options.dryRun) {
console.log('dry run: $ %o', `${NODE_OPTIONS}${process.execPath} ${modulePath} ${forkArgs.join(' ')}`);
return;
}
const forkExecArgv = [
...this.globalExecArgv,
...options.execArgv || [],
];
const NODE_OPTIONS = env.NODE_OPTIONS ? `NODE_OPTIONS='${env.NODE_OPTIONS}' ` : '';
const forkExecArgvString = forkExecArgv.length ? ' ' + forkExecArgv.join(' ') + ' ' : ' ';
const forkArgsString = forkArgs.map(a => `'${a}'`).join(' ');
const fullCommand = `${NODE_OPTIONS}${process.execPath}${forkExecArgvString}${modulePath} ${forkArgsString}`;
if (options.dryRun) {
console.log('dry run: $ %s', fullCommand);
return;
}

options = {
stdio: 'inherit',
Expand All @@ -356,11 +370,9 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
execArgv: forkExecArgv,
};
const proc = fork(modulePath, forkArgs, options);
debug('Run fork pid: %o\n\n$ %s%s %s %s\n\n',
debug('Run fork pid: %o\n\n$ %s\n\n',
proc.pid,
NODE_OPTIONS,
process.execPath,
modulePath, forkArgs.map(a => `'${a}'`).join(' '));
fullCommand);
graceful(proc);

return new Promise<void>((resolve, reject) => {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export default class Dev<T extends typeof Dev> extends BaseCommand<T> {
debug('NODE_ENV: %o', this.env);
this.env.NODE_ENV = this.env.NODE_ENV ?? 'development';
this.env.EGG_MASTER_CLOSE_TIMEOUT = '1000';
const serverBin = getSourceFilename('../scripts/start-cluster.mjs');
const ext = this.isESM ? 'mjs' : 'cjs';
const serverBin = getSourceFilename(`../scripts/start-cluster.${ext}`);
const eggStartOptions = await this.formatEggStartOptions();
const args = [ JSON.stringify(eggStartOptions) ];
const requires = await this.formatRequires();
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface PackageEgg {
declarations?: boolean;
revert?: string | string[];
require?: string | string[];
import?: string | string[];
}
12 changes: 11 additions & 1 deletion test/commands/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,24 @@ describe('test/commands/dev.test.ts', () => {
});

it('should support --require', () => {
const script = getFixtures('require-script');
const script = getFixtures('require-script.cjs');
return coffee.fork(eggBin, [ 'dev', '--require', script ], { cwd })
.debug()
.expect('stdout', /hey, you require me by --require/)
.expect('code', 0)
.end();
});

it('should support --import', () => {
const cwd = getFixtures('demo-app-esm');
const script = getFixtures('require-script.mjs');
return coffee.fork(eggBin, [ 'dev', '--import', script ], { cwd })
.debug()
.expect('stdout', /hey, you require me by --import/)
.expect('code', 0)
.end();
});

it('should support egg.require', () => {
return coffee.fork(eggBin, [ 'dev' ], {
cwd: getFixtures('egg-require'),
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/egg-require/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"egg": {
"framework": "aliyun-egg",
"require": [
"../require-script"
"../require-script.cjs"
]
}
}
File renamed without changes.
1 change: 1 addition & 0 deletions test/fixtures/require-script.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hey, you require me by --import');
2 changes: 1 addition & 1 deletion test/ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ describe('test/ts.test.ts', () => {
NODE_DEBUG: '@eggjs/bin*',
},
})
// .debug()
.debug()
.end();
assert.match(stdout, /ts-node@10\.\d+\.\d+/);
assert.equal(code, 0);
Expand Down

0 comments on commit ae0db57

Please sign in to comment.