diff --git a/.gitignore b/.gitignore index 3c329524..69e56212 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ test/fixtures/ts/node_modules/aliyun-egg/ !test/fixtures/test-files-stack/node_modules/ !test/fixtures/example/node_modules/ !test/fixtures/example-ts-error-stack/node_modules/ +!test/fixtures/egg-require/node_modules/ **/run/*.json diff --git a/README.md b/README.md index 7d3b2be1..307b1db6 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ $ egg-bin dev - `--cluster` worker process number, skip this argvs will start only `1` worker, provide this without value will start `cpu` count worker. - `--sticky` start a sticky cluster server, default to `false`. - `--typescript` / `--ts` enable typescript support, default to `false`. Also support read from `package.json`'s `egg.typescript`. -- `--require` will add to `execArgv`, support multiple. +- `--require` will add to `execArgv`, support multiple. Also support read from `package.json`'s `egg.require` ### debug diff --git a/lib/cmd/dev.js b/lib/cmd/dev.js index 3ae25083..dc7a146c 100644 --- a/lib/cmd/dev.js +++ b/lib/cmd/dev.js @@ -80,7 +80,7 @@ class DevCommand extends Command { * formatArgs(context) { const { cwd, argv } = context; /* istanbul ignore next */ - argv.baseDir = argv._[0] || argv.baseDir || cwd; + argv.baseDir = argv.baseDir || cwd; /* istanbul ignore next */ if (!path.isAbsolute(argv.baseDir)) argv.baseDir = path.join(cwd, argv.baseDir); diff --git a/lib/command.js b/lib/command.js index 0b750ee0..7d05ddd3 100644 --- a/lib/command.js +++ b/lib/command.js @@ -34,22 +34,26 @@ class Command extends BaseCommand { // remove unuse args argv.$0 = undefined; + // read package.json + let baseDir = argv.baseDir || cwd; + if (!path.isAbsolute(baseDir)) baseDir = path.join(cwd, baseDir); + const pkgFile = path.join(baseDir, 'package.json'); + const pkgInfo = fs.existsSync(pkgFile) ? require(pkgFile) : null; + const eggInfo = pkgInfo && pkgInfo.egg; + execArgvObj.require = execArgvObj.require || []; + // read `egg.typescript` from package.json if not pass argv - if (argv.typescript === undefined) { - let baseDir = argv._[0] || argv.baseDir || 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 === true) { - argv.typescript = true; - } - } + if (argv.typescript === undefined && eggInfo && eggInfo.typescript === true) { + argv.typescript = true; + } + + // read `egg.require` from package.json + if (eggInfo && eggInfo.require && Array.isArray(eggInfo.require)) { + execArgvObj.require = execArgvObj.require.concat(eggInfo.require); } // execArgv if (argv.typescript) { - execArgvObj.require = execArgvObj.require || []; execArgvObj.require.push(require.resolve('ts-node/register')); // tell egg loader to load ts file diff --git a/test/fixtures/egg-require/node_modules/aliyun-egg/index.js b/test/fixtures/egg-require/node_modules/aliyun-egg/index.js new file mode 100644 index 00000000..dc41c309 --- /dev/null +++ b/test/fixtures/egg-require/node_modules/aliyun-egg/index.js @@ -0,0 +1,15 @@ +'use strict'; + +exports.startCluster = options => { + console.log('options: %j', options); + if (process.execArgv.length) { + console.log('process.execArgv:', process.execArgv); + } + console.log('NODE_ENV: %s', process.env.NODE_ENV); + + // make sure exit + setTimeout(function() { + console.log('exist by setTimeout'); + process.exit(0); + }, 3000); +}; diff --git a/test/fixtures/egg-require/node_modules/aliyun-egg/package.json b/test/fixtures/egg-require/node_modules/aliyun-egg/package.json new file mode 100644 index 00000000..4e36e661 --- /dev/null +++ b/test/fixtures/egg-require/node_modules/aliyun-egg/package.json @@ -0,0 +1,6 @@ +{ + "name": "aliyun-egg", + "dependencies": { + "egg": "*" + } +} diff --git a/test/fixtures/egg-require/package.json b/test/fixtures/egg-require/package.json new file mode 100644 index 00000000..feec7eca --- /dev/null +++ b/test/fixtures/egg-require/package.json @@ -0,0 +1,9 @@ +{ + "name": "demo-app", + "egg": { + "framework": "aliyun-egg", + "require": [ + "../require-script" + ] + } +} diff --git a/test/lib/cmd/dev.test.js b/test/lib/cmd/dev.test.js index 230d5dca..d976467b 100644 --- a/test/lib/cmd/dev.test.js +++ b/test/lib/cmd/dev.test.js @@ -147,4 +147,15 @@ describe('test/lib/cmd/dev.test.js', () => { .expect('code', 0) .end(); }); + + it('should support egg.require', () => { + mm(process.env, 'NODE_ENV', 'development'); + return coffee.fork(eggBin, [ 'dev' ], { + cwd: path.join(__dirname, '../../fixtures/egg-require'), + }) + // .debug() + .expect('stdout', /hey, you require me by --require/) + .expect('code', 0) + .end(); + }); }); diff --git a/test/my-egg-bin.test.js b/test/my-egg-bin.test.js index 4c574eab..6ad1d3cc 100644 --- a/test/my-egg-bin.test.js +++ b/test/my-egg-bin.test.js @@ -68,7 +68,7 @@ describe('test/my-egg-bin.test.js', () => { .expect('stdout', /"baseDir":".\/dist"/) .expect('stdout', /debugPort: 6666/) .notExpect('stdout', /"argv: {.*debugBrk":true/) - .expect('debugOptions:', /{"debug":true,"debug-brk":5555,"inspect":6666,"inspect-brk":true}/) + .expect('stdout', /{"debug":true,"debug-brk":5555,"inspect":6666,"inspect-brk":true}/) .expect('stdout', /execArgv: \["--debug","--debug-brk=5555","--expose_debug_as=v8debug","--inspect=6666","--inspect-brk","--es_staging","--harmony","--harmony_default_parameters"]/) .expect('code', 0) .end(done); diff --git a/test/ts.test.js b/test/ts.test.js index 12f24993..25a1627a 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -141,16 +141,6 @@ describe('test/ts.test.js', () => { .end(); }); - it('should start app with relative path', () => { - return coffee.fork(eggBin, [ 'dev', './example-ts-pkg' ], { cwd: path.dirname(cwd) }) - // .debug() - .expect('stdout', /hi, egg, 12345/) - .expect('stdout', /ts env: true/) - .expect('stdout', /started/) - .expect('code', 0) - .end(); - }); - it('should test app', () => { return coffee.fork(eggBin, [ 'test' ], { cwd }) // .debug()