diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 05760f6..ba86876 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -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 diff --git a/lib/command.js b/lib/command.js index d57c7ff..b5df59d 100644 --- a/lib/command.js +++ b/lib/command.js @@ -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({ @@ -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)); + }); + } + // 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]; } diff --git a/test/fixtures/pkg-config/config/config.default.js b/test/fixtures/pkg-config/config/config.default.js new file mode 100644 index 0000000..98de4f0 --- /dev/null +++ b/test/fixtures/pkg-config/config/config.default.js @@ -0,0 +1,8 @@ +'use strict'; + +exports.keys = '123456'; + +exports.logger = { + level: 'WARN', + consoleLevel: 'WARN', +}; diff --git a/test/fixtures/pkg-config/inject.js b/test/fixtures/pkg-config/inject.js new file mode 100644 index 0000000..fd04a2f --- /dev/null +++ b/test/fixtures/pkg-config/inject.js @@ -0,0 +1,3 @@ +'use strict'; + +console.log('@@@ inject by pkgInfo'); diff --git a/test/fixtures/pkg-config/package.json b/test/fixtures/pkg-config/package.json new file mode 100644 index 0000000..0999626 --- /dev/null +++ b/test/fixtures/pkg-config/package.json @@ -0,0 +1,9 @@ +{ + "name": "example", + "version": "1.0.0", + "egg": { + "require": [ + "./inject.js" + ] + } +} diff --git a/test/start.test.js b/test/start.test.js index a9ed9a4..7333c4d 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -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, '..'); @@ -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); + } }); }); });