@@ -44,6 +44,14 @@ class StartCommand extends Command {
4444 description : 'whether run at background daemon mode' ,
4545 type : 'boolean' ,
4646 } ,
47+ stdout : {
48+ description : 'A file that stdout redirect to' ,
49+ type : 'string' ,
50+ } ,
51+ stderr : {
52+ description : 'A file that stderr redirect to' ,
53+ type : 'string' ,
54+ } ,
4755 } ;
4856 }
4957
@@ -53,6 +61,8 @@ class StartCommand extends Command {
5361
5462 * run ( context ) {
5563 const argv = Object . assign ( { } , context . argv ) ;
64+ const HOME = homedir ( ) ;
65+ const logDir = path . join ( HOME , 'logs' ) ;
5666
5767 // egg-script start
5868 // egg-script start ./server
@@ -63,46 +73,47 @@ class StartCommand extends Command {
6373
6474 const isDaemon = argv . daemon ;
6575
66- argv . framework = utils . getFrameworkPath ( {
76+ argv . framework = yield this . getFrameworkPath ( {
6777 framework : argv . framework ,
6878 baseDir,
6979 } ) ;
7080
71- const env = context . env ;
72- env . PWD = baseDir ;
73- env . HOME = homedir ( ) ;
74- env . NODE_ENV = 'production' ;
75-
76- // cli argv -> process.env.EGG_SERVER_ENV -> `undefined` then egg will use `prod`
77- if ( argv . env ) {
78- // if undefined, should not pass key due to `spwan`, https://github.com/nodejs/node/blob/master/lib/child_process.js#L470
79- env . EGG_SERVER_ENV = argv . env ;
80- argv . env = undefined ;
81- }
82-
8381 const pkgInfo = require ( path . join ( baseDir , 'package.json' ) ) ;
84- const logDir = path . join ( env . HOME , 'logs' , pkgInfo . name ) ;
85-
8682 argv . title = argv . title || `egg-server-${ pkgInfo . name } ` ;
8783
84+ argv . stdout = argv . stdout || path . join ( logDir , 'master-stdout.log' ) ;
85+ argv . stderr = argv . stderr || path . join ( logDir , 'master-stderr.log' ) ;
86+
87+ const env = context . env ;
88+ env . HOME = HOME ;
89+ env . NODE_ENV = 'production' ;
90+
8891 // adjust env for win
89- let envPath = env . PATH || env . Path ;
92+ const envPath = env . PATH || env . Path ;
9093 if ( envPath ) {
9194 // for nodeinstall
92- envPath = path . join ( baseDir , 'node_modules/.bin' ) + path . delimiter + envPath ;
95+ env . PATH = path . join ( baseDir , 'node_modules/.bin' ) + path . delimiter + envPath ;
9396 }
9497
9598 // for alinode
9699 env . ENABLE_NODE_LOG = 'YES' ;
97100 env . NODE_LOG_DIR = env . NODE_LOG_DIR || path . join ( logDir , 'alinode' ) ;
98101 yield mkdirp ( env . NODE_LOG_DIR ) ;
99102
103+ // cli argv -> process.env.EGG_SERVER_ENV -> `undefined` then egg will use `prod`
104+ if ( argv . env ) {
105+ // if undefined, should not pass key due to `spwan`, https://github.com/nodejs/node/blob/master/lib/child_process.js#L470
106+ env . EGG_SERVER_ENV = argv . env ;
107+ argv . env = undefined ;
108+ }
109+
100110 // remove unused properties, alias had been remove by `removeAlias`
101111 argv . _ = undefined ;
102112 argv . $0 = undefined ;
103113 argv . daemon = undefined ;
104114
105115 const options = {
116+ cwd : baseDir ,
106117 execArgv : context . execArgv ,
107118 env,
108119 stdio : 'inherit' ,
@@ -117,11 +128,11 @@ class StartCommand extends Command {
117128 // whether run in the background.
118129 if ( isDaemon ) {
119130 this . logger . info ( `save log file to ${ logDir } ` ) ;
120- const { stdout, stderr } = yield getRotatelog ( logDir ) ;
131+ const [ stdout , stderr ] = yield [ getRotatelog ( argv . stdout ) , getRotatelog ( argv . stderr ) ] ;
121132 options . stdio = [ 'ignore' , stdout , stderr , 'ipc' ] ;
122133 options . detached = true ;
123134
124- const child = spawn ( 'node' , eggArgs , options ) ;
135+ const child = this . child = spawn ( 'node' , eggArgs , options ) ;
125136 child . on ( 'message' , msg => {
126137 if ( msg && msg . action === 'egg-ready' ) {
127138 this . logger . info ( `egg started on ${ msg . data . address } ` ) ;
@@ -135,32 +146,24 @@ class StartCommand extends Command {
135146 this . helper . spawn ( 'node' , eggArgs , options ) ;
136147 }
137148 }
138- }
139149
140- function * getRotatelog ( logDir ) {
141- const stdoutPath = path . join ( logDir , 'master-stdout.log' ) ;
142- const stderrPath = path . join ( logDir , 'master-stderr.log' ) ;
150+ * getFrameworkPath ( params ) {
151+ return utils . getFrameworkPath ( params ) ;
152+ }
143153
144- // format style: .20150602.193100
145- const timestamp = moment ( ) . format ( '.YYYYMMDD.HHmmss' ) ;
154+ }
146155
147- yield mkdirp ( logDir ) ;
156+ function * getRotatelog ( logfile ) {
157+ yield mkdirp ( path . dirname ( logfile ) ) ;
148158
149- /* istanbul ignore else */
150- if ( yield fs . exists ( stdoutPath ) ) {
159+ if ( yield fs . exists ( logfile ) ) {
160+ // format style: .20150602.193100
161+ const timestamp = moment ( ) . format ( '.YYYYMMDD.HHmmss' ) ;
151162 // Note: rename last log to next start time, not when last log file created
152- yield fs . rename ( stdoutPath , stdoutPath + timestamp ) ;
153- }
154-
155- /* istanbul ignore else */
156- if ( yield fs . exists ( stderrPath ) ) {
157- yield fs . rename ( stderrPath , stderrPath + timestamp ) ;
163+ yield fs . rename ( logfile , logfile + timestamp ) ;
158164 }
159165
160- return yield {
161- stdout : fs . open ( stdoutPath , 'a' ) ,
162- stderr : fs . open ( stderrPath , 'a' ) ,
163- } ;
166+ return yield fs . open ( logfile , 'a' ) ;
164167}
165168
166169module . exports = StartCommand ;
0 commit comments