From 944050314824d88684afc5f1f5545d4aa8675ec7 Mon Sep 17 00:00:00 2001 From: dead-horse Date: Mon, 16 Dec 2019 14:30:33 +0800 Subject: [PATCH 1/2] feat: support eggScriptsConfig in package.json --- README.md | 14 +++++++++++ lib/cmd/start.js | 2 -- lib/command.js | 7 ++++++ test/fixtures/egg-scripts-config/app.js | 13 ++++++++++ .../config/config.default.js | 8 +++++++ test/fixtures/egg-scripts-config/package.json | 15 ++++++++++++ test/start.test.js | 24 +++++++++++++++++-- 7 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/egg-scripts-config/app.js create mode 100644 test/fixtures/egg-scripts-config/config/config.default.js create mode 100644 test/fixtures/egg-scripts-config/package.json diff --git a/README.md b/README.md index 6bba9c9..a02f984 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,20 @@ $ eggctl stop [--title=example] - **Options** - `title` - process title description, use for kill grep. +## Options in `package.json` + +In addition to the command line specification, options can also be specified in `package.json`. However, the command line designation takes precedence. + +```json +{ + "eggScriptsConfig": { + "port": 1234, + "ignore-stderr": true + } +} +``` + + ## Questions & Suggestions Please open an issue [here](https://github.com/eggjs/egg/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc). diff --git a/lib/cmd/start.js b/lib/cmd/start.js index f6e5a99..ec47a2f 100644 --- a/lib/cmd/start.js +++ b/lib/cmd/start.js @@ -78,7 +78,6 @@ class StartCommand extends Command { * run(context) { const { argv, env, cwd, execArgv } = context; - const HOME = homedir(); const logDir = path.join(HOME, 'logs'); @@ -243,7 +242,6 @@ class StartCommand extends Command { } catch (err) { this.logger.error('ignore tail error: %s', err); } - isSuccess = ignoreStdErr; this.logger.error('Start got error, see %s', stderr); this.logger.error('Or use `--ignore-stderr` to ignore stderr at startup.'); diff --git a/lib/command.js b/lib/command.js index bd53436..d57c7ff 100644 --- a/lib/command.js +++ b/lib/command.js @@ -47,6 +47,13 @@ class Command extends BaseCommand { if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript) { argv.sourcemap = true; } + + // read argv from eggScriptsConfig in package.json + if (pkgInfo && pkgInfo.eggScriptsConfig && typeof pkgInfo.eggScriptsConfig === 'object') { + for (const key in pkgInfo.eggScriptsConfig) { + if (argv[key] == null) argv[key] = pkgInfo.eggScriptsConfig[key]; + } + } } // execArgv diff --git a/test/fixtures/egg-scripts-config/app.js b/test/fixtures/egg-scripts-config/app.js new file mode 100644 index 0000000..7ba4259 --- /dev/null +++ b/test/fixtures/egg-scripts-config/app.js @@ -0,0 +1,13 @@ +'use strict'; + +const sleep = require('mz-modules/sleep'); + +module.exports = app => { + if (process.env.ERROR) { + app.logger.error(new Error(process.env.ERROR)); + } + + app.beforeStart(function* () { + yield sleep(process.env.WAIT_TIME); + }); +}; diff --git a/test/fixtures/egg-scripts-config/config/config.default.js b/test/fixtures/egg-scripts-config/config/config.default.js new file mode 100644 index 0000000..98de4f0 --- /dev/null +++ b/test/fixtures/egg-scripts-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/egg-scripts-config/package.json b/test/fixtures/egg-scripts-config/package.json new file mode 100644 index 0000000..36c1f05 --- /dev/null +++ b/test/fixtures/egg-scripts-config/package.json @@ -0,0 +1,15 @@ +{ + "name": "example", + "version": "1.0.0", + "dependencies": { + "egg": "^1.0.0" + }, + "egg": { + "framework": "custom-framework" + }, + "eggScriptsConfig": { + "ignore-stderr": true, + "daemon": true, + "workers": 1 + } +} diff --git a/test/start.test.js b/test/start.test.js index 34f2880..384ff52 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -607,11 +607,14 @@ describe('test/start.test.js', () => { }); describe('check status', () => { - const cwd = path.join(__dirname, 'fixtures/status'); + let cwd; + beforeEach(() => { + cwd = path.join(__dirname, 'fixtures/status'); + }); after(function* () { yield coffee.fork(eggBin, [ 'stop', cwd ]) - // .debug() + // .debug() .end(); yield utils.cleanup(cwd); }); @@ -642,6 +645,23 @@ describe('test/start.test.js', () => { .end(); }); + it('should status check fail `--ignore-stderr` in package.json, exit with 0', function* () { + cwd = path.join(__dirname, 'fixtures/egg-scripts-config'); + mm(process.env, 'WAIT_TIME', 5000); + mm(process.env, 'ERROR', 'error message'); + + let stderr = path.join(homePath, 'logs/master-stderr.log'); + if (isWin) stderr = stderr.replace(/\\/g, '\\\\'); + + const app = coffee.fork(eggBin, [ 'start' ], { cwd }); + // app.debug(); + // TODO: find a windows replacement for tail command + if (!isWin) app.expect('stderr', /nodejs.Error: error message/); + yield app.expect('stderr', new RegExp(`Start got error, see ${stderr}`)) + .expect('code', 0) + .end(); + }); + it('should status check fail, exit with 1', function* () { mm(process.env, 'WAIT_TIME', 5000); mm(process.env, 'ERROR', 'error message'); From 1c77f2cde88ac2ee7cb1c5193bfd2d81979c4981 Mon Sep 17 00:00:00 2001 From: dead-horse Date: Mon, 16 Dec 2019 15:16:40 +0800 Subject: [PATCH 2/2] f --- .gitignore | 3 ++- .../node_modules/custom-framework/index.js | 21 +++++++++++++++++++ .../custom-framework/package.json | 7 +++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/egg-scripts-config/node_modules/custom-framework/index.js create mode 100644 test/fixtures/egg-scripts-config/node_modules/custom-framework/package.json diff --git a/.gitignore b/.gitignore index 125fe6e..0c5a36f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ run/ .DS_Store *.swp test/fixtures/ts/app/controller/home.js -test/fixtures/ts-pkg/app/controller/home.js \ No newline at end of file +test/fixtures/ts-pkg/app/controller/home.js +!test/fixtures/**/node_modules diff --git a/test/fixtures/egg-scripts-config/node_modules/custom-framework/index.js b/test/fixtures/egg-scripts-config/node_modules/custom-framework/index.js new file mode 100644 index 0000000..071acea --- /dev/null +++ b/test/fixtures/egg-scripts-config/node_modules/custom-framework/index.js @@ -0,0 +1,21 @@ +'use strict'; + +const egg = require('../../../../../node_modules/egg'); + +const originStartCluster = egg.startCluster; + +module.exports = Object.assign(egg, { + Application: class CustomApplication extends egg.Application { + get [Symbol.for('egg#eggPath')]() { + return __dirname; + } + }, + startCluster(...args) { + if (process.env.CUSTOM_ENV && !process.env.EGG_SERVER_ENV) { + console.log('## EGG_SERVER_ENV is not pass'); + console.log('## CUSTOM_ENV:', process.env.CUSTOM_ENV); + process.env.EGG_SERVER_ENV = process.env.CUSTOM_ENV; + } + return originStartCluster(...args); + }, +}); diff --git a/test/fixtures/egg-scripts-config/node_modules/custom-framework/package.json b/test/fixtures/egg-scripts-config/node_modules/custom-framework/package.json new file mode 100644 index 0000000..a9328f7 --- /dev/null +++ b/test/fixtures/egg-scripts-config/node_modules/custom-framework/package.json @@ -0,0 +1,7 @@ +{ + "name": "custom-framework", + "version": "1.0.0", + "dependencies": { + "egg": "*" + } +} \ No newline at end of file