diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index e08a511..0000000 --- a/.travis.yml +++ /dev/null @@ -1,24 +0,0 @@ -sudo: false -language: node_js -node_js: - - '6' - - '8' - - '10' -env: - - EGG_VERSION=1 - - EGG_VERSION=2 -matrix: - exclude: - - node_js: '6' - env: EGG_VERSION=2 -before_install: - - npm install -g npminstall -install: - - npminstall - - sed -i.bak '/"egg":/d' package.json - - npminstall -d -script: - - eval "npminstall -d egg@$EGG_VERSION" - - npm run ci -after_script: - - npminstall codecov && codecov diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 981e82b..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,15 +0,0 @@ -environment: - matrix: - - nodejs_version: '8' - - nodejs_version: '10' - -install: - - ps: Install-Product node $env:nodejs_version - - npm i npminstall && node_modules\.bin\npminstall - -test_script: - - node --version - - npm --version - - npm run test - -build: off diff --git a/lib/command.js b/lib/command.js index d57c7ff..f006bf5 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,22 +44,45 @@ 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 `eggScriptsConfig.require` from package.json + const eggScriptsConfig = pkgInfo.eggScriptsConfig; + let requireFiles = Array.isArray(argv.require) ? argv.require : []; + if (eggScriptsConfig && Array.isArray(eggScriptsConfig.require)) { + requireFiles = requireFiles.concat(eggScriptsConfig.require); + } + execArgvObj.require = execArgvObj.require || []; + requireFiles + .filter(injectScript => injectScript) + .forEach(injectScript => { + let requirePath = ''; + if (path.isAbsolute(injectScript) || injectScript.startsWith(`.${path.sep}`)) { + requirePath = path.resolve(baseDir, injectScript); + } else { + requirePath = injectScript; + } + execArgvObj.require.push(requirePath); + }); + // read argv from eggScriptsConfig in package.json - if (pkgInfo && pkgInfo.eggScriptsConfig && typeof pkgInfo.eggScriptsConfig === 'object') { + if (eggScriptsConfig && typeof eggScriptsConfig === 'object') { for (const key in pkgInfo.eggScriptsConfig) { if (argv[key] == null) argv[key] = pkgInfo.eggScriptsConfig[key]; } } + + delete argv.require; } // execArgv 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/config/plugin.js b/test/fixtures/pkg-config/config/plugin.js new file mode 100644 index 0000000..18b9986 --- /dev/null +++ b/test/fixtures/pkg-config/config/plugin.js @@ -0,0 +1,148 @@ +'use strict'; + +module.exports = { + // enable plugins + + /** + * app global Error Handling + * @member {Object} Plugin#onerror + * @property {Boolean} enable - `true` by default + */ + onerror: { + enable: false, + package: 'egg-onerror', + path: 'xxxxx', + }, + + /** + * session + * @member {Object} Plugin#session + * @property {Boolean} enable - `true` by default + * @since 1.0.0 + */ + session: { + enable: false, + package: 'egg-session', + path: 'xxxxx', + }, + + /** + * i18n + * @member {Object} Plugin#i18n + * @property {Boolean} enable - `true` by default + * @since 1.0.0 + */ + i18n: { + enable: false, + package: 'egg-i18n', + path: 'xxxxx', + }, + + /** + * file and dir watcher + * @member {Object} Plugin#watcher + * @property {Boolean} enable - `true` by default + * @since 1.0.0 + */ + watcher: { + enable: false, + package: 'egg-watcher', + path: 'xxxxx', + }, + + /** + * multipart + * @member {Object} Plugin#multipart + * @property {Boolean} enable - `true` by default + * @since 1.0.0 + */ + multipart: { + enable: false, + package: 'egg-multipart', + path: 'xxxxx', + }, + + /** + * security middlewares and extends + * @member {Object} Plugin#security + * @property {Boolean} enable - `true` by default + * @since 1.0.0 + */ + security: { + enable: false, + package: 'egg-security', + path: 'xxxxx', + }, + + /** + * local development helper + * @member {Object} Plugin#development + * @property {Boolean} enable - `true` by default + * @since 1.0.0 + */ + development: { + enable: false, + package: 'egg-development', + path: 'xxxxx', + }, + + /** + * logger file rotator + * @member {Object} Plugin#logrotator + * @property {Boolean} enable - `true` by default + * @since 1.0.0 + */ + logrotator: { + enable: false, + package: 'egg-logrotator', + path: 'xxxxx', + }, + + /** + * schedule tasks + * @member {Object} Plugin#schedule + * @property {Boolean} enable - `true` by default + * @since 2.7.0 + */ + schedule: { + enable: false, + package: 'egg-schedule', + path: 'xxxxx', + }, + + /** + * `app/public` dir static serve + * @member {Object} Plugin#static + * @property {Boolean} enable - `true` by default + * @since 1.0.0 + */ + static: { + enable: false, + package: 'egg-static', + path: 'xxxxx', + }, + + /** + * jsonp support for egg + * @member {Function} Plugin#jsonp + * @property {Boolean} enable - `true` by default + * @since 1.0.0 + */ + jsonp: { + enable: false, + package: 'egg-jsonp', + path: 'xxxxx', + }, + + /** + * view plugin + * @member {Function} Plugin#view + * @property {Boolean} enable - `true` by default + * @since 1.0.0 + */ + view: { + enable: false, + package: 'egg-view', + path: 'xxxxx', + }, +}; diff --git a/test/fixtures/pkg-config/inject.js b/test/fixtures/pkg-config/inject.js new file mode 100644 index 0000000..2ead88c --- /dev/null +++ b/test/fixtures/pkg-config/inject.js @@ -0,0 +1,3 @@ +'use strict'; + +console.log('@@@ inject relative js by pkgInfo'); diff --git a/test/fixtures/pkg-config/inject2.js b/test/fixtures/pkg-config/inject2.js new file mode 100644 index 0000000..4fa540d --- /dev/null +++ b/test/fixtures/pkg-config/inject2.js @@ -0,0 +1,3 @@ +'use strict'; + +console.log('@@@ inject by cli'); diff --git a/test/fixtures/pkg-config/node_modules/custom-framework/index.js b/test/fixtures/pkg-config/node_modules/custom-framework/index.js new file mode 100644 index 0000000..499901d --- /dev/null +++ b/test/fixtures/pkg-config/node_modules/custom-framework/index.js @@ -0,0 +1,13 @@ +'use strict'; + +const egg = require('../../../../../node_modules/egg'); + +const EGG_PATH = Symbol.for('egg#eggPath'); + +class Application extends egg.Application { + get [EGG_PATH]() { + return __dirname; + } +} + +module.exports = Object.assign(egg, { Application }); diff --git a/test/fixtures/pkg-config/node_modules/custom-framework/package.json b/test/fixtures/pkg-config/node_modules/custom-framework/package.json new file mode 100644 index 0000000..073be53 --- /dev/null +++ b/test/fixtures/pkg-config/node_modules/custom-framework/package.json @@ -0,0 +1,7 @@ +{ + "name": "custom-framework", + "version": "1.0.0", + "dependencies": { + "egg": "*" + } +} diff --git a/test/fixtures/pkg-config/node_modules/inject/index.js b/test/fixtures/pkg-config/node_modules/inject/index.js new file mode 100644 index 0000000..55d5a75 --- /dev/null +++ b/test/fixtures/pkg-config/node_modules/inject/index.js @@ -0,0 +1,3 @@ +'use strict'; + +console.log('@@@ inject node_modules by pkgInfo'); diff --git a/test/fixtures/pkg-config/package.json b/test/fixtures/pkg-config/package.json new file mode 100644 index 0000000..7956e68 --- /dev/null +++ b/test/fixtures/pkg-config/package.json @@ -0,0 +1,13 @@ +{ + "name": "example", + "version": "1.0.0", + "egg": { + "framework": "custom-framework" + }, + "eggScriptsConfig": { + "require": [ + "./inject.js", + "inject" + ] + } +} diff --git a/test/fixtures/ts-pkg/app/controller/home.ts b/test/fixtures/ts-pkg/app/controller/home.ts index f6a07cd..bfb11be 100644 --- a/test/fixtures/ts-pkg/app/controller/home.ts +++ b/test/fixtures/ts-pkg/app/controller/home.ts @@ -4,7 +4,7 @@ export default class AppController extends Controller { public index() { try { throw new Error('some err'); - } catch (err) { + } catch (err: any) { this.ctx.logger.error(err); this.ctx.body = { msg: err.message, diff --git a/test/fixtures/ts/app/controller/home.ts b/test/fixtures/ts/app/controller/home.ts index f6a07cd..bfb11be 100644 --- a/test/fixtures/ts/app/controller/home.ts +++ b/test/fixtures/ts/app/controller/home.ts @@ -4,7 +4,7 @@ export default class AppController extends Controller { public index() { try { throw new Error('some err'); - } catch (err) { + } catch (err: any) { this.ctx.logger.error(err); this.ctx.body = { msg: err.message, diff --git a/test/start.test.js b/test/start.test.js index a9ed9a4..26a3f37 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -30,6 +30,34 @@ describe('test/start.test.js', () => { afterEach(mm.restore); describe('start without daemon', () => { + 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', '--require=./inject2' ], { cwd: fixturePath }); + app.debug(); + app.expect('code', 0); + + yield sleep(waitTime); + + assert(app.stderr === ''); + assert(app.stdout.match(/@@@ inject relative js by pkgInfo/)); + assert(app.stdout.match(/@@@ inject node_modules by pkgInfo/)); + assert(app.stdout.match(/@@@ inject by cli/)); + }); + }); + describe('full path', () => { let app; @@ -551,7 +579,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); + } }); }); }); diff --git a/test/stop.test.js b/test/stop.test.js index 16cd38e..95fe485 100644 --- a/test/stop.test.js +++ b/test/stop.test.js @@ -18,7 +18,7 @@ describe('test/stop.test.js', () => { const timeoutPath = path.join(__dirname, 'fixtures/stop-timeout'); const homePath = path.join(__dirname, 'fixtures/home'); const logDir = path.join(homePath, 'logs'); - const waitTime = '10s'; + const waitTime = '15s'; before(function* () { yield mkdirp(homePath); @@ -271,7 +271,7 @@ describe('test/stop.test.js', () => { describe('stop all with timeout', function() { let app; let killer; - this.timeout(12000); + this.timeout(17000); beforeEach(function* () { yield utils.cleanup(timeoutPath); app = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=stop-timeout', timeoutPath ]);