From ae0db57172c0769c0715943e4cff654df1565ac8 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Fri, 27 Dec 2024 13:44:07 +0800 Subject: [PATCH] f --- scripts/start-cluster.cjs | 15 ++++++ src/baseCommand.ts | 48 ++++++++++++------- src/commands/dev.ts | 3 +- src/types.ts | 1 + test/commands/dev.test.ts | 12 ++++- test/fixtures/egg-require/package.json | 2 +- .../{require-script.js => require-script.cjs} | 0 test/fixtures/require-script.mjs | 1 + test/ts.test.ts | 2 +- 9 files changed, 62 insertions(+), 22 deletions(-) create mode 100755 scripts/start-cluster.cjs rename test/fixtures/{require-script.js => require-script.cjs} (100%) create mode 100644 test/fixtures/require-script.mjs diff --git a/scripts/start-cluster.cjs b/scripts/start-cluster.cjs new file mode 100755 index 00000000..42c542b7 --- /dev/null +++ b/scripts/start-cluster.cjs @@ -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(); diff --git a/src/baseCommand.ts b/src/baseCommand.ts index 21205a1a..a69dce5e 100644 --- a/src/baseCommand.ts +++ b/src/baseCommand.ts @@ -80,6 +80,11 @@ export abstract class BaseCommand 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', @@ -154,7 +159,6 @@ export abstract class BaseCommand 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 ?? {}; @@ -287,6 +291,7 @@ export abstract class BaseCommand 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); @@ -305,15 +310,21 @@ export abstract class BaseCommand extends Command { protected async formatRequires(): Promise { 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) { @@ -338,15 +349,18 @@ export abstract class BaseCommand 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', @@ -356,11 +370,9 @@ export abstract class BaseCommand 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((resolve, reject) => { diff --git a/src/commands/dev.ts b/src/commands/dev.ts index a7f1a261..cc687e1b 100644 --- a/src/commands/dev.ts +++ b/src/commands/dev.ts @@ -37,7 +37,8 @@ export default class Dev extends BaseCommand { 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(); diff --git a/src/types.ts b/src/types.ts index b1717ef7..b785f838 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,4 +5,5 @@ export interface PackageEgg { declarations?: boolean; revert?: string | string[]; require?: string | string[]; + import?: string | string[]; } diff --git a/test/commands/dev.test.ts b/test/commands/dev.test.ts index 9632e667..9512ca21 100644 --- a/test/commands/dev.test.ts +++ b/test/commands/dev.test.ts @@ -152,7 +152,7 @@ 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/) @@ -160,6 +160,16 @@ describe('test/commands/dev.test.ts', () => { .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'), diff --git a/test/fixtures/egg-require/package.json b/test/fixtures/egg-require/package.json index feec7eca..e44c21a7 100644 --- a/test/fixtures/egg-require/package.json +++ b/test/fixtures/egg-require/package.json @@ -3,7 +3,7 @@ "egg": { "framework": "aliyun-egg", "require": [ - "../require-script" + "../require-script.cjs" ] } } diff --git a/test/fixtures/require-script.js b/test/fixtures/require-script.cjs similarity index 100% rename from test/fixtures/require-script.js rename to test/fixtures/require-script.cjs diff --git a/test/fixtures/require-script.mjs b/test/fixtures/require-script.mjs new file mode 100644 index 00000000..e843d5f2 --- /dev/null +++ b/test/fixtures/require-script.mjs @@ -0,0 +1 @@ +console.log('hey, you require me by --import'); diff --git a/test/ts.test.ts b/test/ts.test.ts index 68ebd13b..96ce3a7b 100644 --- a/test/ts.test.ts +++ b/test/ts.test.ts @@ -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);