Skip to content

Commit

Permalink
refactor: use framework (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and popomore committed Mar 2, 2017
1 parent a939187 commit 4542dee
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 84 deletions.
8 changes: 4 additions & 4 deletions lib/debug_command.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class DebugCommand extends Command {
}

* run(cwd, args) {
const eggPath = this.getFrameworkOrEggPath(cwd);
args = yield this.helper.formatArgs(cwd, args, { eggPath });
const framework = this.getFrameworkOrEggPath(cwd);
args = yield this.helper.formatArgs(cwd, args, { framework });

const options = {
env: Object.assign({}, process.env),
Expand All @@ -30,8 +30,8 @@ class DebugCommand extends Command {
this.helper.forkNode(this.helper.serverBin, args, options);
}

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

}

help() {
Expand Down
12 changes: 6 additions & 6 deletions lib/dev_command.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const Command = require('./command');

class DevCommand extends Command {
* run(cwd, args = []) {
const eggPath = this.getFrameworkOrEggPath(cwd);
const devArgs = yield this.helper.formatArgs(cwd, args, { eggPath });
const framework = this.getFrameworkOrEggPath(cwd);
const devArgs = yield this.helper.formatArgs(cwd, args, { framework });

const options = {
env: Object.assign({}, process.env),
Expand All @@ -20,12 +20,12 @@ class DevCommand extends Command {
yield this.helper.forkNode(this.helper.serverBin, devArgs, options);
}

help() {
return 'local env start';
getFrameworkOrEggPath() {

}

getFrameworkOrEggPath(cwd) {
return this.utils.getFrameworkOrEggPath(cwd);
help() {
return 'local env start';
}
}

Expand Down
30 changes: 16 additions & 14 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const path = require('path');
const glob = require('glob');
const detect = require('detect-port');
const debug = require('debug')('egg-bin');
const utils = require('egg-utils');
const yargs = require('yargs');

exports.defaultPort = 7001;
exports.serverBin = path.join(__dirname, 'start-cluster');
Expand Down Expand Up @@ -62,31 +64,31 @@ exports.checkDeps = function* () {

exports.formatArgs = function* (cwd, args, options) {
options = options || {};
if (!args.filter(arg => arg.startsWith('--baseDir')).length) {
args.push('--baseDir');
args.push(cwd);
}

if (!args.filter(arg => arg.startsWith('--cluster')).length) {
args.push('--cluster');
args.push('1');
}
const argv = yargs.parse(args);
argv.baseDir = argv.baseDir || cwd;
argv.workers = argv.cluster || 1;
argv.framework = argv.framework || options.framework;
argv.framework = utils.getFrameworkPath(argv);
argv.port = argv.port || argv.p;

if (options.eggPath) {
args.push(`--eggPath=${options.eggPath}`);
}
// remove unused properties
argv.cluster = undefined;
argv.p = undefined;
argv._ = undefined;
argv.$0 = undefined;

// auto detect available port
if (args.indexOf('-p') === -1 && args.indexOf('--port') === -1) {
if (!argv.port) {
debug('detect available port');
const port = yield detect(exports.defaultPort);
if (port !== exports.defaultPort) {
args.push('-p', port);
argv.port = port;
console.warn(`[egg-bin] server port ${exports.defaultPort} is in use, now using port ${port}\n`);
}
debug(`use available port ${port}`);
}
return args;
return [ JSON.stringify(argv) ];
};

exports.formatExecArgv = function(args) {
Expand Down
33 changes: 3 additions & 30 deletions lib/start-cluster
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,7 @@
'use strict';

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

commander
.option('--eggPath [eggPath]')
.option('--baseDir [baseDir]')
.option('-p, --port [port]')
.option('--cluster [workers]')
.option('--sticky')
.allowUnknownOption(true)
.parse(process.argv);

const baseDir = commander.baseDir;
const workers = commander.cluster ? Number(commander.cluster) : 1;
const port = commander.port;
const customEgg = commander.eggPath;
const sticky = commander.sticky;

assert(customEgg, 'eggPath required, missing any egg frameworks?');

const options = {
baseDir,
workers,
port,
customEgg,
sticky,
};

debug('eggPath:%s options:%j', customEgg, options);

require(customEgg).startCluster(options);
const options = JSON.parse(process.argv[2]);
debug('start cluster options: %j', options);
require(options.framework).startCluster(options);
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
},
"dependencies": {
"co-mocha": "^1.2.0",
"commander": "^2.9.0",
"common-bin": "^1.0.0",
"debug": "^2.6.0",
"detect-port": "^1.0.7",
"egg-utils": "^1.1.0",
"common-bin": "^1.0.1",
"debug": "^2.6.1",
"detect-port": "^1.1.0",
"egg-utils": "^2.0.0",
"glob": "^7.1.1",
"intelli-espower-loader": "^1.0.1",
"istanbul": "^0.4.5",
"mkdirp": "^0.5.1",
"mocha": "^3.2.0",
"power-assert": "^1.4.2",
"rimraf": "^2.5.4"
"rimraf": "^2.6.1",
"yargs": "^6.6.0"
},
"devDependencies": {
"autod": "^2.7.1",
Expand All @@ -29,12 +29,12 @@
"babel-register": "^6.4.3",
"coffee": "^3.3.0",
"cross-env": "^3.1.3",
"egg-ci": "^1.1.0",
"egg-ci": "^1.3.0",
"enzyme": "^2.0.0",
"eslint": "^3.13.1",
"eslint": "^3.16.1",
"eslint-config-egg": "^3.2.0",
"jsdom": "^8.0.1",
"mm": "^2.0.0",
"mm": "^2.1.0",
"react": "^0.14.7",
"react-addons-test-utils": "^0.14.7",
"react-dom": "^0.14.7"
Expand Down
4 changes: 2 additions & 2 deletions test/egg-debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const net = require('net');
describe('egg-bin debug', () => {
const eggBin = require.resolve('../bin/egg-bin.js');
const appdir = path.join(__dirname, 'fixtures/demo-app');
const customEgg = path.join(appdir, 'node_modules/aliyun-egg');
const framework = path.join(appdir, 'node_modules/aliyun-egg');

before(() => {
rimraf.sync(path.join(appdir, 'node_modules/iron-node'));
Expand All @@ -28,7 +28,7 @@ describe('egg-bin debug', () => {
it('should startCluster with port', done => {
coffee.fork(eggBin, [ 'debug', '--port', '6001' ], { cwd: appdir })
// .debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"port":"6001","customEgg":"${customEgg}"}\nprocess.execArgv: [ '--inspect' ]\n`)
.expect('stdout', `{"port":6001,"baseDir":"${appdir}","workers":1,"framework":"${framework}"}\nprocess.execArgv: [ '--inspect' ]\n`)
.expect('code', 0)
.end(done);
});
Expand Down
36 changes: 18 additions & 18 deletions test/egg-dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,85 @@ const net = require('net');
describe('egg-bin dev', () => {
const eggBin = require.resolve('../bin/egg-bin.js');
const appdir = path.join(__dirname, 'fixtures/demo-app');
const customEgg = path.join(appdir, 'node_modules/aliyun-egg');
const framework = path.join(appdir, 'node_modules/aliyun-egg');

it('should startCluster success', done => {
coffee.fork(eggBin, [ 'dev' ], { cwd: appdir })
// .debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"customEgg":"${customEgg}"}\n`)
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"framework":"${framework}"}\n`)
.expect('code', 0)
.end(done);
});

it('should startCluster with --harmony success', done => {
coffee.fork(eggBin, [ 'dev', '--harmony' ], { cwd: appdir })
// .debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"customEgg":"${customEgg}"}\nprocess.execArgv: [ '--harmony' ]\n`)
.expect('stdout', `{"harmony":true,"baseDir":"${appdir}","workers":1,"framework":"${framework}"}\nprocess.execArgv: [ '--harmony' ]\n`)
.expect('code', 0)
.end(done);
});

it('should startCluster with --port', done => {
coffee.fork(eggBin, [ 'dev', '--port', '6001' ], { cwd: appdir })
// .debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"port":"6001","customEgg":"${customEgg}"}\n`)
.expect('stdout', `{"port":6001,"baseDir":"${appdir}","workers":1,"framework":"${framework}"}\n`)
.expect('code', 0)
.end(done);
});

it('should startCluster with --sticky', done => {
coffee.fork(eggBin, [ 'dev', '--port', '6001', '--sticky' ], { cwd: appdir })
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"port":"6001","customEgg":"${customEgg}","sticky":true}\n`)
.expect('stdout', `{"port":6001,"sticky":true,"baseDir":"${appdir}","workers":1,"framework":"${framework}"}\n`)
.expect('code', 0)
.end(done);
});

it('should startCluster with -p', done => {
coffee.fork(eggBin, [ 'dev', '-p', '6001' ], { cwd: appdir })
// .debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"port":"6001","customEgg":"${customEgg}"}\n`)
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"framework":"${framework}","port":6001}\n`)
.expect('code', 0)
.end(done);
});

it('should startCluster with --cluster 2', done => {
coffee.fork(eggBin, [ 'dev', '--cluster', '2' ], { cwd: appdir })
// .debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":2,"customEgg":"${customEgg}"}\n`)
.expect('stdout', `{"baseDir":"${appdir}","workers":2,"framework":"${framework}"}\n`)
.expect('code', 0)
.end(done);
});

it('should startCluster with --cluster=2', done => {
coffee.fork(eggBin, [ 'dev', '--cluster=2' ], { cwd: appdir })
// .debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":2,"customEgg":"${customEgg}"}\n`)
.expect('stdout', `{"baseDir":"${appdir}","workers":2,"framework":"${framework}"}\n`)
.expect('code', 0)
.end(done);
});

it('should startCluster with --baseDir base', done => {
coffee.fork(eggBin, [ 'dev', '--baseDir', 'base' ], { cwd: appdir })
it('should startCluster with --baseDir root', done => {
coffee.fork(eggBin, [ 'dev', '--baseDir', appdir ])
// .debug()
.expect('stdout', `{"baseDir":"base","workers":1,"customEgg":"${customEgg}"}\n`)
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"framework":"${framework}"}\n`)
.expect('code', 0)
.end(done);
});

it('should startCluster with --baseDir=base', done => {
coffee.fork(eggBin, [ 'dev', '--baseDir=base' ], { cwd: appdir })
it('should startCluster with --baseDir=root', done => {
coffee.fork(eggBin, [ 'dev', `--baseDir=${appdir}` ])
// .debug()
.expect('stdout', `{"baseDir":"base","workers":1,"customEgg":"${customEgg}"}\n`)
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"framework":"${framework}"}\n`)
.expect('code', 0)
.end(done);
});

it('should startCluster with custom yadan framework', done => {
const baseDir = path.join(__dirname, 'fixtures/custom-framework-app');
const customEgg = path.join(baseDir, 'node_modules', 'yadan');
const framework = path.join(baseDir, 'node_modules', 'yadan');
coffee.fork(eggBin, [ 'dev' ], { cwd: baseDir })
// .debug()
.expect('stdout', `yadan start: {"baseDir":"${baseDir}","workers":1,"customEgg":"${customEgg}"}\n`)
.expect('stdout', `yadan start: {"baseDir":"${baseDir}","workers":1,"framework":"${framework}"}\n`)
.expect('code', 0)
.end(done);
});
Expand All @@ -111,7 +111,7 @@ describe('egg-bin dev', () => {
it.skip('should startCluster with execArgv --debug', done => {
coffee.fork(eggBin, [ 'dev', '--debug=7000' ], { cwd: appdir })
// .debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"customEgg":"${customEgg}"}\n`)
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"framework":"${framework}"}\n`)
.expect('stderr', /Debugger listening on .*7000/)
.expect('code', 0)
.end(done);
Expand All @@ -120,7 +120,7 @@ describe('egg-bin dev', () => {
it.skip('should startCluster with execArgv --inspect', done => {
coffee.fork(eggBin, [ 'dev', '--inspect=7000' ], { cwd: appdir })
// .debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"customEgg":"${customEgg}"}\n`)
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"framework":"${framework}"}\n`)
.expect('stderr', /Debugger listening on .*7000/)
.expect('code', 0)
.end(done);
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/demo-app/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"name": "demo-app"
"name": "demo-app",
"egg": {
"framework": "aliyun-egg"
}
}

0 comments on commit 4542dee

Please sign in to comment.