Skip to content

Commit

Permalink
fix: fix template and modify ts register method
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Aug 14, 2018
1 parent 79cdfb1 commit 1857c08
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
"description": "{{description}}",
"private": true,
"dependencies": {
"midway": "*",
"tslib": "^1.8.1",
"typescript": "^2.8.0"
"midway": "*"
},
"devDependencies": {
"@types/node": "^10.5.5",
"egg-ci": "^1.8.0",
"midway-bin": "*",
"midway-mock": "*",
"tslib": "^1.8.1",
"ts-node": "^7.0.0",
"tslint": "^5.9.1",
"typescript": "^2.8.0",
"webstorm-disable-index": "^1.2.0"
},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ module.exports = pandora => {
* default is fork mode
*/
pandora
.fork('{{name}}', require('midway/server')({
typescript: true,
}));
.fork('{{name}}', require.resolve('midway/server'));

/**
* you can use cluster mode to start application
Expand Down
2 changes: 1 addition & 1 deletion packages/midway/cluster/agent_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

const utils = require('./utils');
const options = JSON.parse(process.argv[2]);
utils.isNeedCompile(options);
utils.registerTypescriptEnvironment(options);
require('egg-cluster/lib/agent_worker');
2 changes: 1 addition & 1 deletion packages/midway/cluster/app_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

const utils = require('./utils');
const options = JSON.parse(process.argv[2]);
utils.isNeedCompile(options);
utils.registerTypescriptEnvironment(options);
require('egg-cluster/lib/app_worker');
7 changes: 2 additions & 5 deletions packages/midway/cluster/master.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
const EggMaster = require('egg-cluster/lib/master');
const path = require('path');
const isTypeScriptEnvironment = require('./utils').isTypeScriptEnvironment;
const formatOptions = require('./utils').formatOptions;

class Master extends EggMaster {

constructor(options) {
if(isTypeScriptEnvironment()) {
options.isTsEnv = true;
}

options = formatOptions(options);
super(options);
this.log('[master] egg version %s, egg-core version %s',
require('egg/package').version,
Expand Down
37 changes: 30 additions & 7 deletions packages/midway/cluster/utils.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
'use strict';

/**
* 判断是否为本地测试或者开发环境,ugly...
* register ts env
* @param {Object} options {env: NODE_ENV, typescript: true}
* @return {Boolean} true or false
*/
exports.isNeedCompile = options => {
let need = options.typescript && options.isTsEnv;
if (need && !exports.isTypeScriptEnvironment()) {
exports.registerTypescriptEnvironment = options => {
let tsFlag = options.typescript;
// 只有是 ts 应用,并且在本地环境才判断是否加载 ts-node
if (tsFlag && !exports.isTypeScriptEnvironment() && exports.isDev()) {
try {
require('ts-node/register');
} catch (e) {
throw new Error('Need ts-node(https://github.com/TypeStrong/ts-node) be installed!');
}
}

return need;
};

exports.isDev = env => {
Expand All @@ -24,3 +23,27 @@ exports.isDev = env => {
exports.isTypeScriptEnvironment = () => {
return !!require.extensions['.ts'];
};

/**
* add typescript and baseDir
* @param options
* @returns {*}
*/
exports.formatOptions = (options) => {
if(!options.baseDir) {
options.baseDir = process.cwd();
}

if(options.typescript === undefined) {
if(exports.isTypeScriptEnvironment()) {
options.typescript = true;
} else {
const pkg = require(path.join(options.baseDir, 'package.json'));
if(pkg['devDependencies'] && pkg['devDependencies']['typescript']) {
options.typescript = true;
}
}
}

return options;
};
4 changes: 0 additions & 4 deletions packages/midway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@
"supertest": "^3.1.0"
},
"dependencies": {
"cfork": "^1.7.1",
"debug": "^3.1.0",
"egg-cluster": "^1.17.0",
"egg-logger": "^1.6.2",
"graceful-process": "^1.2.0",
"injection": "^0.2.7",
"midway-core": "^0.2.7",
"midway-web": "^0.2.7"
Expand Down
9 changes: 2 additions & 7 deletions packages/midway/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@
* Midway 服务器入口文件
* @exports midway/server
* @example
* require('midway/server')({port:6001, workers:1}).ready(() => {
* require('midway/server')({typescript:true, baseDir:__dirname}).ready(() => {
* console.log('Server has start at 6001');
* });
*
* @param options {Object} 启动参数
* @param options.port {Number} 启动端口
* @param options.typescript {boolean} 是否走ts模式
* @param options.baseDir {String} 应用根目录
* @param options.workers {Number} 启动的 worker 数量, 本地默认是 1
* @param callback {Function} 启动完成的回调
*/
const Master = require('./cluster/master');
const fs = require('fs');
const assert = require('assert');

module.exports = (options, callback) => {
assert(options.baseDir, 'options.baseDir is required and should be set the root path of your app!');
assert(fs.existsSync(options.baseDir), `baseDir ${options.baseDir} does not exist!`);
new Master(options).ready(callback);
};

0 comments on commit 1857c08

Please sign in to comment.