Skip to content

Commit

Permalink
fix: make sure dev command eggPath can be override (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Dec 16, 2016
1 parent 9f2689d commit 22510d7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
7 changes: 6 additions & 1 deletion lib/debug_command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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';
}
Expand Down
7 changes: 6 additions & 1 deletion lib/dev_command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -24,6 +25,10 @@ class DevCommand extends Command {
help() {
return 'local env start';
}

getFrameworkOrEggPath(cwd) {
return this.utils.getFrameworkOrEggPath(cwd);
}
}

module.exports = DevCommand;
10 changes: 4 additions & 6 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
Expand All @@ -45,4 +44,3 @@ exports.formatArgs = function* (cwd, args) {
}
return args;
};

11 changes: 11 additions & 0 deletions test/fixtures/my-egg-bin/lib/dev_command.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions test/fixtures/my-egg-bin/lib/my_program.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/my-egg-bin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 22510d7

Please sign in to comment.