diff --git a/lib/debug_command.js b/lib/debug_command.js index 7c7703cf..ee9ed9d0 100644 --- a/lib/debug_command.js +++ b/lib/debug_command.js @@ -14,7 +14,8 @@ class DebugCommand extends Command { } * run(cwd, args) { - args = yield this.helper.formatArgs(cwd, args); + const eggPath = this.getFrameworkOrEggPath(cwd); + args = yield this.helper.formatArgs(cwd, args, { eggPath }); const options = { env: Object.assign({}, process.env), @@ -63,6 +64,10 @@ class DebugCommand extends Command { this.helper.forkNode(this.helper.serverBin, args, options); } + getFrameworkOrEggPath(cwd) { + return this.utils.getFrameworkOrEggPath(cwd); + } + help() { return 'Debug mode start'; } diff --git a/lib/dev_command.js b/lib/dev_command.js index 342347b6..e6aede56 100644 --- a/lib/dev_command.js +++ b/lib/dev_command.js @@ -7,7 +7,8 @@ class DevCommand extends Command { * run(cwd, args) { const execArgv = args ? args.filter(str => str.indexOf('--debug') === 0 || str.indexOf('--inspect') === 0) : []; - args = yield this.helper.formatArgs(cwd, args); + const eggPath = this.getFrameworkOrEggPath(cwd); + args = yield this.helper.formatArgs(cwd, args, { eggPath }); const options = { env: Object.assign({}, process.env), @@ -24,6 +25,10 @@ class DevCommand extends Command { help() { return 'local env start'; } + + getFrameworkOrEggPath(cwd) { + return this.utils.getFrameworkOrEggPath(cwd); + } } module.exports = DevCommand; diff --git a/lib/helper.js b/lib/helper.js index 67483796..c2d1f62a 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -3,7 +3,6 @@ const path = require('path'); const glob = require('glob'); const detect = require('detect-port'); -const utils = require('egg-utils'); exports.defaultPort = 7001; exports.serverBin = path.join(__dirname, 'start-cluster'); @@ -24,15 +23,15 @@ exports.checkDeps = function* () { return true; }; -exports.formatArgs = function* (cwd, args) { +exports.formatArgs = function* (cwd, args, options) { + options = options || {}; args.push('--baseDir'); args.push(cwd); args.push('--cluster'); args.push('1'); - const eggPath = utils.getFrameworkOrEggPath(cwd); - if (eggPath) { - args.push(`--eggPath=${eggPath}`); + if (options.eggPath) { + args.push(`--eggPath=${options.eggPath}`); } // auto detect available port @@ -45,4 +44,3 @@ exports.formatArgs = function* (cwd, args) { } return args; }; - diff --git a/test/fixtures/my-egg-bin/lib/dev_command.js b/test/fixtures/my-egg-bin/lib/dev_command.js new file mode 100644 index 00000000..5e286e7f --- /dev/null +++ b/test/fixtures/my-egg-bin/lib/dev_command.js @@ -0,0 +1,11 @@ +'use strict'; + +const DevCommand = require('../../../..').DevCommand; + +class MyDevCommand extends DevCommand { + * run(cwd) { + console.log('run dev with eggPath: %s', this.getFrameworkOrEggPath(cwd) || 'empty'); + } +} + +module.exports = MyDevCommand; diff --git a/test/fixtures/my-egg-bin/lib/my_program.js b/test/fixtures/my-egg-bin/lib/my_program.js index e76c8090..f7555f8c 100644 --- a/test/fixtures/my-egg-bin/lib/my_program.js +++ b/test/fixtures/my-egg-bin/lib/my_program.js @@ -9,6 +9,7 @@ class MyProgram extends Program { this.version = require('../package.json').version; this.addCommand('nsp', path.join(__dirname, 'nsp_command.js')); + this.addCommand('dev', path.join(__dirname, 'dev_command.js')); } } diff --git a/test/my-egg-bin.test.js b/test/my-egg-bin.test.js index 29beec1a..76d7dcfe 100644 --- a/test/my-egg-bin.test.js +++ b/test/my-egg-bin.test.js @@ -37,6 +37,16 @@ describe('custom egg-bin: my-egg-bin', () => { .end(done); }); + it('should my-egg-bin dev success', done => { + coffee.fork(eggBin, [ 'dev' ], { + cwd: path.join(__dirname, 'fixtures/test-files'), + }) + // .debug() + .expect('stdout', /run dev with eggPath: empty/) + .expect('code', 0) + .end(done); + }); + it('should show help message', done => { coffee.fork(eggBin, [ '-h' ], { cwd: path.join(__dirname, 'fixtures/test-files'),