@@ -30,11 +30,7 @@ class Instance {
30
30
return false ;
31
31
}
32
32
33
- // Ordinarily we'd use this.loadRunningConfig(), but stack overflow
34
- // (the error not the programming bible) happens if we do that.
35
- let env = this . cliConfig . get ( 'running' ) ;
36
- this . config = new Config ( path . join ( this . dir , `config.${ env } .json` ) ) ;
37
- this . loadProcess ( ) ;
33
+ this . loadRunningEnvironment ( ) ;
38
34
39
35
if ( ! this . process . isRunning ( this . dir ) ) {
40
36
this . cliConfig . set ( 'running' , null ) . save ( ) ;
@@ -48,47 +44,56 @@ class Instance {
48
44
return true ;
49
45
}
50
46
47
+ get config ( ) {
48
+ let currentEnv = this . system . environment ;
49
+
50
+ if ( ! this . _config || this . _config . environment !== currentEnv ) {
51
+ this . _config = new Config ( path . join ( this . dir , `config.${ this . system . environment } .json` ) ) ;
52
+ this . _config . environment = this . system . environment ;
53
+ }
54
+
55
+ return this . _config ;
56
+ }
57
+
58
+ get process ( ) {
59
+ let name = this . config . get ( 'process' , 'local' ) ;
60
+
61
+ if ( ! this . _process || name !== this . _process . name ) {
62
+ let manager = this . system . getProcessManager ( name ) ;
63
+ this . _process = new manager . Class ( this . ui , this . system , this ) ;
64
+ this . _process . name = name ;
65
+ }
66
+
67
+ return this . _process ;
68
+ }
69
+
51
70
constructor ( ui , system , dir ) {
52
71
this . ui = ui ;
53
72
this . system = system ;
54
73
this . dir = dir ;
55
74
}
56
75
57
- loadConfig ( skipEnvCheck ) {
58
- // If we are starting in production mode but a development config exists and a production config doesn't,
59
- // we want to start in development mode anyways.
76
+ checkEnvironment ( ) {
60
77
if (
61
- ! skipEnvCheck && this . system . production &&
78
+ this . system . production &&
62
79
Config . exists ( path . join ( this . dir , 'config.development.json' ) ) &&
63
- ! Config . exists ( path . join ( this . dir , 'config.production.json' ) )
80
+ ! Config . exists ( path . join ( 'config.production.json' ) )
64
81
) {
65
82
this . ui . log ( 'Found a development config but not a production config, running in development mode instead' , 'yellow' ) ;
66
83
this . system . setEnvironment ( true , true ) ;
67
84
}
68
- this . config = new Config ( path . join ( this . dir , `config.${ this . system . environment } .json` ) ) ;
69
- this . loadProcess ( ) ;
70
85
}
71
86
72
- loadRunningConfig ( setEnv , setNodeEnv ) {
73
- if ( ! this . running ) {
87
+ loadRunningEnvironment ( setEnv , setNodeEnv ) {
88
+ let env = this . cliConfig . get ( 'running' ) ;
89
+
90
+ if ( ! env ) {
74
91
throw new Error ( 'This instance is not running.' ) ;
75
92
}
76
93
77
- let env = this . cliConfig . get ( 'running' ) ;
78
94
if ( setEnv ) {
79
95
this . system . setEnvironment ( env === 'development' , setNodeEnv ) ;
80
96
}
81
-
82
- this . config = new Config ( path . join ( this . dir , `config.${ env } .json` ) ) ;
83
- this . loadProcess ( ) ;
84
- return env ;
85
- }
86
-
87
- loadProcess ( ) {
88
- let name = this . config . get ( 'process' , 'local' ) ;
89
- let manager = this . system . getProcessManager ( name ) ;
90
- this . process = new manager . Class ( this . ui , this . system , this ) ;
91
- this . processName = manager . name ;
92
97
}
93
98
94
99
summary ( ) {
@@ -101,17 +106,17 @@ class Instance {
101
106
} ;
102
107
}
103
108
104
- let env = this . loadRunningConfig ( ) ;
109
+ this . loadRunningEnvironment ( true ) ;
105
110
106
111
return {
107
112
name : this . name ,
108
113
dir : this . dir . replace ( os . homedir ( ) , '~' ) ,
109
114
running : true ,
110
115
version : this . cliConfig . get ( 'active-version' ) ,
111
- mode : env ,
116
+ mode : this . system . environment ,
112
117
url : this . config . get ( 'url' ) ,
113
118
port : this . config . get ( 'server.port' ) ,
114
- process : this . processName
119
+ process : this . _process . name
115
120
} ;
116
121
}
117
122
0 commit comments