1
1
import path from 'path'
2
- import yaml from 'js-yaml'
3
- import json from 'parse-json'
2
+ import jsYaml from 'js-yaml'
3
+ import parseJson from 'parse-json'
4
4
import createDebug from 'debug'
5
5
import loadPlugin from 'load-plugin'
6
- import plain from 'is-plain-obj'
6
+ import isPlainObj from 'is-plain-obj'
7
7
import fault from 'fault'
8
8
import { FindUp } from './find-up.js'
9
9
10
- var debug = createDebug ( 'unified-engine:configuration' )
10
+ const debug = createDebug ( 'unified-engine:configuration' )
11
11
12
- var own = { } . hasOwnProperty
12
+ const own = { } . hasOwnProperty
13
13
14
- var loaders = {
14
+ const loaders = {
15
15
'.json' : loadJson ,
16
16
'.cjs' : loadScriptOrModule ,
17
17
'.mjs' : loadScriptOrModule ,
@@ -20,12 +20,12 @@ var loaders = {
20
20
'.yml' : loadYaml
21
21
}
22
22
23
- var defaultLoader = loadJson
23
+ const defaultLoader = loadJson
24
24
25
25
Configuration . prototype . load = load
26
26
27
27
export function Configuration ( options ) {
28
- var names = [ ]
28
+ const names = [ ]
29
29
30
30
this . cwd = options . cwd
31
31
this . packageField = options . packageField
@@ -58,13 +58,13 @@ export function Configuration(options) {
58
58
filePath : options . rcPath ,
59
59
cwd : options . cwd ,
60
60
detect : options . detectConfig ,
61
- names : names ,
61
+ names,
62
62
create : this . create
63
63
} )
64
64
}
65
65
66
66
function load ( filePath , callback ) {
67
- var self = this
67
+ const self = this
68
68
69
69
self . findUp . load ( filePath || path . resolve ( this . cwd , 'stdin.js' ) , done )
70
70
@@ -73,18 +73,18 @@ function load(filePath, callback) {
73
73
return callback ( error , file )
74
74
}
75
75
76
- self . create ( ) . then ( function ( result ) {
76
+ self . create ( ) . then ( ( result ) => {
77
77
callback ( null , result )
78
78
} , callback )
79
79
}
80
80
}
81
81
82
82
async function create ( buf , filePath ) {
83
- var self = this
84
- var fn = ( filePath && loaders [ path . extname ( filePath ) ] ) || defaultLoader
85
- var options = { prefix : self . pluginPrefix , cwd : self . cwd }
86
- var result = { settings : { } , plugins : [ ] }
87
- var contents
83
+ const self = this
84
+ const fn = ( filePath && loaders [ path . extname ( filePath ) ] ) || defaultLoader
85
+ const options = { prefix : self . pluginPrefix , cwd : self . cwd }
86
+ const result = { settings : { } , plugins : [ ] }
87
+ let contents
88
88
89
89
if ( filePath ) {
90
90
contents = await fn . apply ( self , arguments )
@@ -129,11 +129,11 @@ function loadScriptOrModule(_, filePath) {
129
129
}
130
130
131
131
function loadYaml ( buf , filePath ) {
132
- return yaml . safeLoad ( buf , { filename : path . basename ( filePath ) } )
132
+ return jsYaml . safeLoad ( buf , { filename : path . basename ( filePath ) } )
133
133
}
134
134
135
135
function loadJson ( buf , filePath ) {
136
- var result = json ( buf , filePath )
136
+ let result = parseJson ( buf , filePath )
137
137
138
138
if ( path . basename ( filePath ) === 'package.json' ) {
139
139
result = result [ this . packageField ]
@@ -152,7 +152,7 @@ async function merge(target, raw, options) {
152
152
return target
153
153
154
154
async function addPreset ( result ) {
155
- var plugins = result . plugins
155
+ const plugins = result . plugins
156
156
157
157
if ( plugins === null || plugins === undefined ) {
158
158
// Empty.
@@ -168,11 +168,10 @@ async function merge(target, raw, options) {
168
168
}
169
169
170
170
async function addEach ( result ) {
171
- var index = - 1
172
- var value
171
+ let index = - 1
173
172
174
173
while ( ++ index < result . length ) {
175
- value = result [ index ]
174
+ const value = result [ index ]
176
175
177
176
// Keep order sequential instead of parallel.
178
177
// eslint-disable-next-line no-await-in-loop
@@ -183,12 +182,14 @@ async function merge(target, raw, options) {
183
182
}
184
183
185
184
async function addIn ( result ) {
186
- var key
185
+ let key
187
186
188
187
for ( key in result ) {
189
- // Keep order sequential instead of parallel.
190
- // eslint-disable-next-line no-await-in-loop
191
- await use ( key , result [ key ] )
188
+ if ( own . call ( result , key ) ) {
189
+ // Keep order sequential instead of parallel.
190
+ // eslint-disable-next-line no-await-in-loop
191
+ await use ( key , result [ key ] )
192
+ }
192
193
}
193
194
}
194
195
@@ -203,8 +204,8 @@ async function merge(target, raw, options) {
203
204
}
204
205
205
206
async function addModule ( id , value ) {
206
- var fp = loadPlugin . resolve ( id , { cwd : options . root , prefix : options . prefix } )
207
- var result
207
+ let fp = loadPlugin . resolve ( id , { cwd : options . root , prefix : options . prefix } )
208
+ let result
208
209
209
210
if ( fp ) {
210
211
result = await loadFromAbsolutePath ( fp , options . root )
@@ -219,7 +220,7 @@ async function merge(target, raw, options) {
219
220
Object . assign ( { } , options , { root : path . dirname ( fp ) } )
220
221
)
221
222
}
222
- } catch ( _ ) {
223
+ } catch {
223
224
throw fault (
224
225
'Error: Expected preset or plugin, not %s, at `%s`' ,
225
226
result ,
@@ -236,7 +237,7 @@ async function merge(target, raw, options) {
236
237
}
237
238
238
239
function addPlugin ( result , value ) {
239
- var entry = find ( target . plugins , result )
240
+ const entry = find ( target . plugins , result )
240
241
241
242
if ( entry ) {
242
243
reconfigure ( entry , value )
@@ -247,15 +248,15 @@ async function merge(target, raw, options) {
247
248
}
248
249
249
250
function reconfigure ( entry , value ) {
250
- if ( plain ( entry [ 1 ] ) && plain ( value ) ) {
251
+ if ( isPlainObj ( entry [ 1 ] ) && isPlainObj ( value ) ) {
251
252
value = Object . assign ( { } , entry [ 1 ] , value )
252
253
}
253
254
254
255
entry [ 1 ] = value
255
256
}
256
257
257
258
function find ( entries , plugin ) {
258
- var index = - 1
259
+ let index = - 1
259
260
260
261
while ( ++ index < entries . length ) {
261
262
if ( entries [ index ] [ 0 ] === plugin ) {
@@ -265,8 +266,8 @@ function find(entries, plugin) {
265
266
}
266
267
267
268
function failingModule ( id , error ) {
268
- var cache = failingModule . cache || ( failingModule . cache = { } )
269
- var submodule = own . call ( cache , id ) ? cache [ id ] : ( cache [ id ] = fail )
269
+ const cache = failingModule . cache || ( failingModule . cache = { } )
270
+ const submodule = own . call ( cache , id ) ? cache [ id ] : ( cache [ id ] = fail )
270
271
return submodule
271
272
function fail ( ) {
272
273
throw error
0 commit comments