Skip to content

Commit

Permalink
feat: try to use --inspect first (#19)
Browse files Browse the repository at this point in the history
* feat: try to use --inspect first

closes eggjs/egg#131

* test: run test on node v7
  • Loading branch information
fengmk2 authored and atian25 committed Nov 3, 2016
1 parent 8441162 commit d7ad24c
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 33 deletions.
1 change: 1 addition & 0 deletions .autod.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ module.exports = {
],
semver: [
],
registry: 'https://r.cnpmjs.org',
};
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: node_js
node_js:
- '4'
- '6'
- '7'
install:
- npm i npminstall && npminstall
script:
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'
- nodejs_version: '7'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
57 changes: 35 additions & 22 deletions lib/debug_command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down
16 changes: 12 additions & 4 deletions lib/start-cluster
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

'use strict';

const debug = require('debug')('egg-bin:start-cluster');
const assert = require('assert');
const commander = require('commander');

commander
Expand All @@ -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);
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -37,7 +38,7 @@
"homepage": "https://github.com/eggjs/egg-bin",
"author": "fengmk2 <[email protected]> (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",
Expand All @@ -52,6 +53,6 @@
"index.js"
],
"ci": {
"version": "4, 6"
"version": "4, 6, 7"
}
}
4 changes: 2 additions & 2 deletions test/egg-bin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion test/egg-debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion test/egg-dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit d7ad24c

Please sign in to comment.