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 029a9d0 commit b2d53a8
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
node_modules/
node_modules
coverage/
test/fixtures/custom-framework-app/node_modules/

test/fixtures/demo-app/node_modules/aliyun-egg/
!test/fixtures/demo-app/node_modules/aliyun-egg/node_modules/
!test/fixtures/demo-app-esm/node_modules/

test/fixtures/ts/node_modules/aliyun-egg/
!test/fixtures/ts/node_modules/aliyun-egg/node_modules/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@eggjs/utils": "^4.1.2",
"@oclif/core": "^4.2.0",
"@types/mocha": "^10.0.10",
"@types/supertest": "^6.0.2",
"c8": "^10.0.0",
"detect-port": "^2.0.0",
"egg-ts-helper": "^2.1.0",
Expand Down
16 changes: 9 additions & 7 deletions src/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,15 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
findPaths.unshift(flags.base);
}
flags.tscompiler = flags.tscompiler ?? 'ts-node/register';
let tsNodeRegister = importResolve(flags.tscompiler, {
const tsNodeRegister = importResolve(flags.tscompiler, {
paths: findPaths,
});
flags.tscompiler = tsNodeRegister;
// should require tsNodeRegister on current process, let it can require *.ts files
// e.g.: dev command will execute egg loader to find configs and plugins
// await importModule(tsNodeRegister);
// let child process auto require ts-node too
if (this.isESM) {
tsNodeRegister = pathToFileURL(tsNodeRegister).href;
this.addNodeOptions(`--import ${tsNodeRegister}`);
} else {
this.addNodeOptions(`--require ${tsNodeRegister}`);
}
this.addNodeOptions(this.formatImportModule(tsNodeRegister));
// tell egg loader to load ts file
// see https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js#L443
this.env.EGG_TYPESCRIPT = 'true';
Expand Down Expand Up @@ -321,6 +316,13 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
return requires;
}

protected formatImportModule(modulePath: string) {
if (this.isESM) {
return `--import ${pathToFileURL(modulePath).href}`;
}
return `--require ${modulePath}`;
}

protected addNodeOptions(options: string) {
if (this.env.NODE_OPTIONS) {
if (!this.env.NODE_OPTIONS.includes(options)) {
Expand Down
10 changes: 2 additions & 8 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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';
Expand Down Expand Up @@ -44,13 +43,8 @@ export default class Dev<T extends typeof Dev> extends BaseCommand<T> {
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);
}
const imports = this.formatImportModule(r).split(' ');
execArgv.push(...imports);
}
await this.forkNode(serverBin, args, { execArgv });
}
Expand Down
23 changes: 20 additions & 3 deletions test/commands/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('test/commands/dev.test.ts', () => {
const eggBin = path.join(getRootDirname(), 'bin/run.js');
const cwd = getFixtures('demo-app');

it('should startCluster success', () => {
it('should startCluster success on CommonJS', () => {
return coffee.fork(eggBin, [ 'dev' ], {
cwd,
// env: { NODE_DEBUG: 'egg-bin*' },
Expand All @@ -25,6 +25,23 @@ describe('test/commands/dev.test.ts', () => {
.end();
});

it('should startCluster success on ESM', () => {
const cwd = getFixtures('demo-app-esm');
const hook = path.join(cwd, 'hook.js');
return coffee.fork(eggBin, [ 'dev', '-r', hook ], {
cwd,
})
// .debug()
.expect('stdout', /start hook success/)
.expect('stdout', /'--import'/)
.expect('stdout', /"workers":1/)
.expect('stdout', /"baseDir":".*?demo-app-esm"/)
.expect('stdout', /"framework":".*?aliyun-egg"/)
.expect('stdout', /NODE_ENV: development/)
.expect('code', 0)
.end();
});

it('should dev start with custom NODE_ENV', () => {
return coffee.fork(eggBin, [ 'dev' ], { cwd, env: { NODE_ENV: 'prod' } })
.debug()
Expand Down Expand Up @@ -137,7 +154,7 @@ describe('test/commands/dev.test.ts', () => {
it('should support --require', () => {
const script = getFixtures('require-script');
return coffee.fork(eggBin, [ 'dev', '--require', script ], { cwd })
// .debug()
.debug()
.expect('stdout', /hey, you require me by --require/)
.expect('code', 0)
.end();
Expand All @@ -147,7 +164,7 @@ describe('test/commands/dev.test.ts', () => {
return coffee.fork(eggBin, [ 'dev' ], {
cwd: getFixtures('egg-require'),
})
// .debug()
.debug()
.expect('stdout', /hey, you require me by --require/)
.expect('code', 0)
.end();
Expand Down
18 changes: 17 additions & 1 deletion test/commands/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('test/commands/test.test.ts', () => {
describe('egg-bin test', () => {
it('should success js', () => {
return coffee.fork(eggBin, [ 'test' ], { cwd })
// .debug()
.debug()
.expect('stdout', /should success/)
.expect('stdout', /a\.test\.js/)
.expect('stdout', /b\/b\.test\.js/)
Expand All @@ -20,6 +20,22 @@ describe('test/commands/test.test.ts', () => {
.end();
});

it('should success when no changed files', () => {
return coffee.fork(eggBin, [ 'test', '-c' ], { cwd })
// .debug()
.expect('stdout', /No changed test files/)
.expect('code', 0)
.end();
});

it('should fail when baseDir not exists', () => {
return coffee.fork(eggBin, [ 'test', '--base', path.join(cwd, 'not-exists') ], { cwd })
// .debug()
.expect('stderr', /baseDir: .+ not exists/)
.expect('code', 1)
.end();
});

it('should success on ts', async () => {
const cwd = getFixtures('example-ts');
await coffee.fork(eggBin, [ 'test' ], { cwd })
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/demo-app-esm/hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('start hook success');

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

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

3 changes: 2 additions & 1 deletion test/fixtures/demo-app-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"name": "demo-app",
"egg": {
"framework": "aliyun-egg"
}
},
"type": "module"
}

0 comments on commit b2d53a8

Please sign in to comment.