Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

- name: update npm
run: npm i -g npm

- name: Install Dependencies
run: npm i -g npminstall && npminstall

Expand Down
22 changes: 19 additions & 3 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class Command extends BaseCommand {
type: 'boolean',
alias: [ 'ts', 'typescript' ],
},

require: {
description: 'inject to execArgv --require',
type: 'array',
alias: 'r',
},
};

this.logger = new Logger({
Expand All @@ -38,18 +44,28 @@ class Command extends BaseCommand {
const context = super.context;
const { argv, execArgvObj, cwd } = context;

// read `egg.typescript` from package.json
let baseDir = argv._[0] || cwd;
if (!path.isAbsolute(baseDir)) baseDir = path.join(cwd, baseDir);
const pkgFile = path.join(baseDir, 'package.json');
if (fs.existsSync(pkgFile)) {
const pkgInfo = require(pkgFile);
if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript) {
const eggInfo = pkgInfo.egg;

// read `egg.typescript` from package.json
if (eggInfo && eggInfo.typescript) {
argv.sourcemap = true;
}

// read `egg.require` from package.json
if (eggInfo && eggInfo.require && Array.isArray(eggInfo.require)) {
execArgvObj.require = execArgvObj.require || [];
eggInfo.require.forEach(injectScript => {
execArgvObj.require.push(path.resolve(baseDir, injectScript));
Copy link
Member Author

@atian25 atian25 Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不应该 resolve 的,因为一般的用法是:

{
  "name": "example",
  "version": "1.0.0",
  "egg": {
    "require": [
      "babel/register.js"
    ]
  }
}

加了 resolve 后,就得写成 ./node_modules/babel/register.js

这样和 egg-cluster 的 --require 表现不一致。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实 egg-bin 和 egg-scripts 这段逻辑,直接下沉到 egg-cluster 可能更适合。不过先修复这个再说

});
}

// read argv from eggScriptsConfig in package.json
if (pkgInfo && pkgInfo.eggScriptsConfig && typeof pkgInfo.eggScriptsConfig === 'object') {
if (pkgInfo.eggScriptsConfig && typeof pkgInfo.eggScriptsConfig === 'object') {
for (const key in pkgInfo.eggScriptsConfig) {
if (argv[key] == null) argv[key] = pkgInfo.eggScriptsConfig[key];
}
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/pkg-config/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

exports.keys = '123456';

exports.logger = {
level: 'WARN',
consoleLevel: 'WARN',
};
3 changes: 3 additions & 0 deletions test/fixtures/pkg-config/inject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

console.log('@@@ inject by pkgInfo');
9 changes: 9 additions & 0 deletions test/fixtures/pkg-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "example",
"version": "1.0.0",
"egg": {
"require": [
"./inject.js"
]
}
}
32 changes: 31 additions & 1 deletion test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,32 @@ describe('test/start.test.js', () => {
});
});

describe('read pkgInfo', () => {
let app;
let fixturePath;

before(function* () {
fixturePath = path.join(__dirname, 'fixtures/pkg-config');
yield utils.cleanup(fixturePath);
});

after(function* () {
app.proc.kill('SIGTERM');
yield utils.cleanup(fixturePath);
});

it('should --require', function* () {
app = coffee.fork(eggBin, [ 'start', '--workers=1', fixturePath ]);
app.debug();
app.expect('code', 0);

yield sleep(waitTime);

assert(app.stderr === '');
assert(app.stdout.match(/@@@ inject by pkgInfo/));
});
});

describe('subDir as baseDir', () => {
let app;
const rootDir = path.join(__dirname, '..');
Expand Down Expand Up @@ -551,7 +577,11 @@ describe('test/start.test.js', () => {
const exitEvent = awaitEvent(app.proc, 'exit');
app.proc.kill('SIGTERM');
const code = yield exitEvent;
assert(code === 0);
if (isWin) {
assert(code === null);
} else {
assert(code === 0);
}
});
});
});
Expand Down