1
1
const path = require ( 'path' )
2
2
const fs = require ( 'fs' )
3
- const vm = require ( 'vm' )
4
3
const is = require ( 'is' )
5
4
const _ = require ( 'lodash' )
6
5
const chokidar = require ( 'chokidar' )
7
6
const consola = require ( 'consola' )
8
- const requireLike = require ( 'require-like' )
9
7
10
8
const defaultConfig = require ( './defaults' )
11
9
const schema = require ( './schema.json' )
@@ -39,11 +37,7 @@ module.exports = class Config {
39
37
// Apply custom configuration to this Config instance
40
38
this . applyCustomConfig ( )
41
39
} catch ( e ) {
42
- this . logger . error (
43
- `\`${ path . basename (
44
- this . path
45
- ) } \` used for configuration is invalid due to: \n\n${ e . message } `
46
- )
40
+ this . logger . error ( `\`${ path . basename ( this . path ) } \` used for configuration is invalid due to: \n\n${ e . message } ` )
47
41
}
48
42
}
49
43
@@ -110,52 +104,44 @@ module.exports = class Config {
110
104
*/
111
105
onChange ( cb ) {
112
106
// keep a copy of the current configuration
113
- chokidar
114
- . watch ( this . path , { ignoreInitial : true } )
115
- . on ( 'all' , ( event , filePath ) => {
116
- const bk = Object . assign ( { } , this . data )
117
-
118
- // Reset config to the defaults
119
- this . applyDefaults ( this . defaults )
120
-
121
- // Handle each event type of the configuration
122
- switch ( event ) {
123
- case 'add' :
124
- case 'change' :
125
- try {
126
- // Apply custom configuration to this Config instance
127
- this . applyCustomConfig ( )
128
-
129
- // Notify that the custom config file is in use
130
- if ( event === 'add' ) {
131
- this . logger . info (
132
- `\`${ path . basename ( filePath ) } \` is used for configuration.`
133
- )
134
- }
135
- } catch ( e ) {
136
- this . logger . error (
137
- `\`${ path . basename (
138
- filePath
139
- ) } \` used for configuration is invalid due to: \n\n${ e . message } `
140
- )
107
+ chokidar . watch ( this . path , { ignoreInitial : true } ) . on ( 'all' , ( event , filePath ) => {
108
+ const bk = Object . assign ( { } , this . data )
109
+
110
+ // Reset config to the defaults
111
+ this . applyDefaults ( this . defaults )
112
+
113
+ // Handle each event type of the configuration
114
+ switch ( event ) {
115
+ case 'add' :
116
+ case 'change' :
117
+ try {
118
+ // Apply custom configuration to this Config instance
119
+ this . applyCustomConfig ( )
120
+
121
+ // Notify that the custom config file is in use
122
+ if ( event === 'add' ) {
123
+ this . logger . info ( `\`${ path . basename ( filePath ) } \` is used for configuration.` )
141
124
}
142
- break
143
- case 'unlink' :
144
- this . logger . info (
145
- `\`${ path . basename (
146
- filePath
147
- ) } \` configuration file not found, falling back to default configuration.`
125
+ } catch ( e ) {
126
+ this . logger . error (
127
+ `\`${ path . basename ( filePath ) } \` used for configuration is invalid due to: \n\n${ e . message } `
148
128
)
149
- break
150
- }
151
-
152
- // Pass a before and after copies of the config in case
153
- // there was a change to the config.
154
- const configCopy = Object . assign ( { } , this . data )
155
- if ( ! _ . isEqual ( bk , configCopy ) ) {
156
- cb ( bk , configCopy , this . difference ( configCopy , bk ) )
157
- }
158
- } )
129
+ }
130
+ break
131
+ case 'unlink' :
132
+ this . logger . info (
133
+ `\`${ path . basename ( filePath ) } \` configuration file not found, falling back to default configuration.`
134
+ )
135
+ break
136
+ }
137
+
138
+ // Pass a before and after copies of the config in case
139
+ // there was a change to the config.
140
+ const configCopy = Object . assign ( { } , this . data )
141
+ if ( ! _ . isEqual ( bk , configCopy ) ) {
142
+ cb ( bk , configCopy , this . difference ( configCopy , bk ) )
143
+ }
144
+ } )
159
145
}
160
146
161
147
/**
@@ -170,10 +156,7 @@ module.exports = class Config {
170
156
function changes ( object , base ) {
171
157
return _ . transform ( object , function ( result , value , key ) {
172
158
if ( ! _ . isEqual ( value , base [ key ] ) ) {
173
- result [ key ] =
174
- _ . isObject ( value ) && _ . isObject ( base [ key ] )
175
- ? changes ( value , base [ key ] )
176
- : value
159
+ result [ key ] = _ . isObject ( value ) && _ . isObject ( base [ key ] ) ? changes ( value , base [ key ] ) : value
177
160
}
178
161
} )
179
162
}
@@ -187,18 +170,14 @@ module.exports = class Config {
187
170
* @returns {Error|* }
188
171
*/
189
172
getFileConfig ( ) {
190
- const cwdRequire = requireLike ( path . join ( this . cwd , '/' ) )
191
- const configFile = fs . readFileSync ( this . path )
192
- const sandbox = { require : cwdRequire , module : { exports : undefined } }
193
- const context = new vm . createContext ( sandbox )
194
- const config = new vm . Script ( configFile . toString ( ) )
195
- config . runInContext ( context )
173
+ // empty out cache
174
+ delete require . cache [ this . path ]
175
+
176
+ // load file
177
+ const config = require ( this . path )
196
178
197
179
// Fail if the executed code does not result in exporting a function or an object to module.exports
198
- if (
199
- ! is . function ( sandbox . module . exports ) &&
200
- ! is . object ( sandbox . module . exports )
201
- ) {
180
+ if ( ! is . function ( config ) && ! is . object ( config ) ) {
202
181
throw new Error (
203
182
"The application's config should either export an object or a function that returns an object.\n\n" +
204
183
'Object example:\n' +
@@ -215,8 +194,6 @@ module.exports = class Config {
215
194
)
216
195
}
217
196
218
- return is . function ( sandbox . module . exports )
219
- ? sandbox . module . exports ( )
220
- : sandbox . module . exports
197
+ return is . function ( config ) ? config ( process . env . NODE_ENV ) : config
221
198
}
222
199
}
0 commit comments