From d7ad24c06ba4c74e1b5c7876eba19d13fc28402d Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 3 Nov 2016 09:57:17 +0800 Subject: [PATCH] feat: try to use --inspect first (#19) * feat: try to use --inspect first closes https://github.com/eggjs/egg/issues/131 * test: run test on node v7 --- .autod.conf | 1 + .travis.yml | 1 + appveyor.yml | 1 + lib/debug_command.js | 57 ++++++++++++++++++++++++++---------------- lib/start-cluster | 16 +++++++++--- package.json | 7 +++--- test/egg-bin.test.js | 4 +-- test/egg-debug.test.js | 2 +- test/egg-dev.test.js | 2 +- 9 files changed, 58 insertions(+), 33 deletions(-) diff --git a/.autod.conf b/.autod.conf index 853e1830..67308e20 100644 --- a/.autod.conf +++ b/.autod.conf @@ -17,4 +17,5 @@ module.exports = { ], semver: [ ], + registry: 'https://r.cnpmjs.org', }; diff --git a/.travis.yml b/.travis.yml index 03736ab2..5ec14674 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: node_js node_js: - '4' - '6' + - '7' install: - npm i npminstall && npminstall script: diff --git a/appveyor.yml b/appveyor.yml index e4893768..2efd0fad 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -2,6 +2,7 @@ environment: matrix: - nodejs_version: '4' - nodejs_version: '6' + - nodejs_version: '7' install: - ps: Install-Product node $env:nodejs_version diff --git a/lib/debug_command.js b/lib/debug_command.js index 1b76def6..98c9af35 100644 --- a/lib/debug_command.js +++ b/lib/debug_command.js @@ -3,6 +3,7 @@ const path = require('path'); const debug = require('debug')('egg-bin:debug'); const childprocess = require('childprocess'); +const semver = require('semver'); const Command = require('./command'); class DebugCommand extends Command { @@ -24,37 +25,49 @@ class DebugCommand extends Command { } const options = { - env: process.env, + env: Object.assign({}, process.env), }; options.env.NODE_ENV = options.env.NODE_ENV || 'development'; options.env.EGG_DEBUG = 'true'; - debug('%s %s %j', this.helper.serverBin, args.join(' '), options.env.NODE_ENV); yield this.helper.checkDeps(); - // auto download iron-node at the first time - yield this.helper.getIronNodeBin(this.npmCli, cwd); + // https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V6.md#2016-07-06-version-630-current-fishrock123 + const hasInspector = semver.satisfies(process.versions.node, '>=6.3.0'); + debug('%s %s, hasInspector:%s, NODE_ENV:%s, cwd:%s', + this.helper.serverBin, args.join(' '), hasInspector, options.env.NODE_ENV, process.cwd()); - childprocess.inject(function(modulePath, args, opt) { - // this function will be toString() and save to tmp file - const cluster = require('cluster'); - const originSetupMaster = cluster.setupMaster; - /* istanbul ignore next */ - cluster.setupMaster = function(settings) { - if (!settings) return; - const args = settings.args || []; - args.unshift(settings.exec); - settings.args = args; - settings.exec = process.env.IRON_NODE_PATH; - originSetupMaster.call(cluster, settings); - }; - return [ modulePath, args, opt ]; - }); + if (hasInspector) { + options.execArgv = [ + '--inspect', + ]; + } else { + // try to use iron-node@3 instead + // auto download iron-node at the first time + yield this.helper.getIronNodeBin(this.npmCli, cwd); + + childprocess.inject(function(modulePath, args, opt) { + // this function will be toString() and save to tmp file + const cluster = require('cluster'); + const originSetupMaster = cluster.setupMaster; + /* istanbul ignore next */ + cluster.setupMaster = function(settings) { + if (!settings) return; + const args = settings.args || []; + args.unshift(settings.exec); + settings.args = args; + settings.exec = process.env.IRON_NODE_PATH; + originSetupMaster.call(cluster, settings); + }; + return [ modulePath, args, opt ]; + }); + + // iron-node should be installed in cwd, + // resolve after iron-node installed + options.env.IRON_NODE_PATH = require.resolve(path.join(process.cwd(), 'node_modules/iron-node/bin/run.js')); + } - // iron-node should be installed in cwd, - // resolve after iron-node installed - options.env.IRON_NODE_PATH = require.resolve(path.join(process.cwd(), 'node_modules/iron-node/bin/run.js')); this.helper.forkNode(this.helper.serverBin, args, options); } diff --git a/lib/start-cluster b/lib/start-cluster index 90e57ca9..fb96cda1 100755 --- a/lib/start-cluster +++ b/lib/start-cluster @@ -2,6 +2,8 @@ 'use strict'; +const debug = require('debug')('egg-bin:start-cluster'); +const assert = require('assert'); const commander = require('commander'); commander @@ -14,11 +16,17 @@ commander const baseDir = commander.baseDir; const workers = commander.cluster ? Number(commander.cluster) : 1; +const port = commander.port; +const eggPath = commander.eggPath; + +assert(eggPath, 'eggPath required, missing any egg frameworks?'); const options = { - baseDir: baseDir, - port: commander.port, - workers: workers, + baseDir, + workers, + port, }; -require(commander.eggPath).startCluster(options); +debug('eggPath:%s options:%j', eggPath, options); + +require(eggPath).startCluster(options); diff --git a/package.json b/package.json index ccde06f0..125acf8d 100644 --- a/package.json +++ b/package.json @@ -18,11 +18,12 @@ "mkdirp": "^0.5.1", "mocha": "^3.0.0", "rimraf": "^2.5.4", + "semver": "^5.3.0", "thunk-mocha": "^1.0.3" }, "devDependencies": { "autod": "^2.7.1", - "coffee": "^3.2.5", + "coffee": "^3.3.0", "egg-ci": "1", "eslint": "3", "eslint-config-egg": "3", @@ -37,7 +38,7 @@ "homepage": "https://github.com/eggjs/egg-bin", "author": "fengmk2 (https://fengmk2.com)", "scripts": { - "lint": "eslint --fix bin lib test *.js", + "lint": "eslint bin lib test *.js", "test": "TEST_TIMEOUT=3600000 TESTS=test/*.test.js bin/egg-bin.js test -r intelli-espower-loader", "cov": "TEST_TIMEOUT=3600000 TESTS=test/*.test.js bin/egg-bin.js cov -r intelli-espower-loader", "ci": "npm run lint && npm run cov", @@ -52,6 +53,6 @@ "index.js" ], "ci": { - "version": "4, 6" + "version": "4, 6, 7" } } diff --git a/test/egg-bin.test.js b/test/egg-bin.test.js index b2ea9b91..0dab610c 100644 --- a/test/egg-bin.test.js +++ b/test/egg-bin.test.js @@ -22,7 +22,7 @@ describe('egg-bin --version, --help', () => { cwd: appdir, }) // .debug() - .expect('stdout', /Usage: .*egg-bin.* \[command\] \[options\]/) + .expect('stdout', /Usage: .*egg-bin.* \[command] \[options]/) .expect('code', 0) .end(done); }); @@ -32,7 +32,7 @@ describe('egg-bin --version, --help', () => { cwd: appdir, }) // .debug() - .expect('stdout', /cov \- Run test with coverage/) + .expect('stdout', /cov - Run test with coverage/) .expect('code', 0) .end(done); }); diff --git a/test/egg-debug.test.js b/test/egg-debug.test.js index 2c53eb47..e8fb3b8e 100644 --- a/test/egg-debug.test.js +++ b/test/egg-debug.test.js @@ -30,7 +30,7 @@ describe('egg-bin debug', () => { cwd: appdir, }) .debug() - .expect('stdout', `{"baseDir":"${appdir}","port":"6001","workers":1}\n`) + .expect('stdout', `{"baseDir":"${appdir}","workers":1,"port":"6001"}\n`) .expect('code', 0) .end(done); }); diff --git a/test/egg-dev.test.js b/test/egg-dev.test.js index e1f51556..671f6e21 100644 --- a/test/egg-dev.test.js +++ b/test/egg-dev.test.js @@ -22,7 +22,7 @@ describe('egg-bin dev', () => { cwd: appdir, }) // .debug() - .expect('stdout', `{"baseDir":"${appdir}","port":"6001","workers":1}\n`) + .expect('stdout', `{"baseDir":"${appdir}","workers":1,"port":"6001"}\n`) .expect('code', 0) .end(done); });