Skip to content

Commit fe9ffe6

Browse files
committedJul 9, 2021
Refactor code-style
1 parent 57d0aee commit fe9ffe6

File tree

9 files changed

+83
-76
lines changed

9 files changed

+83
-76
lines changed
 

‎index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
'use strict'
2-
module.exports = require('./lib')
2+
module.exports = require('./lib/index.js')

‎lib/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var stream = require('stream')
44
var engine = require('unified-engine')
55
var chalk = require('chalk')
66
var chokidar = require('chokidar')
7-
var options = require('./options')
7+
var options = require('./options.js')
88

99
module.exports = start
1010

@@ -88,10 +88,10 @@ function start(cliConfig) {
8888
engine(config, done)
8989

9090
// Handle complete run.
91-
function done(err, code, context) {
92-
if (err) {
91+
function done(error, code, context) {
92+
if (error) {
9393
clean()
94-
fail(err)
94+
fail(error)
9595
} else {
9696
exitStatus = code
9797

@@ -141,10 +141,10 @@ function start(cliConfig) {
141141
}
142142

143143
// Print an error, optionally with stack.
144-
function fail(err, pretty) {
144+
function fail(error, pretty) {
145145
var message =
146-
(pretty ? String(err).trim() : err.stack) ||
147-
/* istanbul ignore next - Old versions of Node */ err
146+
(pretty ? String(error).trim() : error.stack) ||
147+
/* istanbul ignore next - Old versions of Node */ error
148148

149149
exitStatus = 1
150150

‎lib/options.js

+37-39
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var camelcase = require('camelcase')
55
var minimist = require('minimist')
66
var json5 = require('json5')
77
var fault = require('fault')
8-
var schema = require('./schema')
8+
var schema = require('./schema.json')
99

1010
module.exports = options
1111

@@ -18,7 +18,10 @@ var minischema = {
1818
boolean: []
1919
}
2020

21-
schema.forEach(addEach)
21+
let index = -1
22+
while (++index < schema.length) {
23+
addEach(schema[index])
24+
}
2225

2326
// Parse CLI options.
2427
function options(flags, configuration) {
@@ -28,12 +31,14 @@ function options(flags, configuration) {
2831
var help
2932
var ext
3033
var report
34+
let index = -1
3135

32-
schema.forEach(function (option) {
36+
while (++index < schema.length) {
37+
const option = schema[index]
3338
if (option.type === 'string' && config[option.long] === '') {
3439
throw fault('Missing value:%s', inspect(option).join(' '))
3540
}
36-
})
41+
}
3742

3843
ext = commaSeparated(config.ext)
3944
report = reporter(config.report)
@@ -108,26 +113,27 @@ function addEach(option) {
108113

109114
// Parse `extensions`.
110115
function commaSeparated(value) {
111-
return flatten(normalize(value).map(splitList))
116+
return flatten(normalize(value).map((d) => splitList(d)))
112117
}
113118

114119
// Parse `plugins`.
115120
function plugins(value) {
116121
var result = {}
122+
const normalized = normalize(value).map((d) => splitOptions(d))
123+
let index = -1
117124

118-
normalize(value)
119-
.map(splitOptions)
120-
.forEach(function (value) {
121-
result[value[0]] = value[1] ? parseConfig(value[1], {}) : null
122-
})
125+
while (++index < normalized.length) {
126+
const value = normalized[index]
127+
result[value[0]] = value[1] ? parseConfig(value[1], {}) : null
128+
}
123129

124130
return result
125131
}
126132

127133
// Parse `reporter`: only one is accepted.
128134
function reporter(value) {
129135
var all = normalize(value)
130-
.map(splitOptions)
136+
.map((d) => splitOptions(d))
131137
.map(function (value) {
132138
return [value[0], value[1] ? parseConfig(value[1], {}) : null]
133139
})
@@ -138,10 +144,12 @@ function reporter(value) {
138144
// Parse `settings`.
139145
function settings(value) {
140146
var cache = {}
147+
const normalized = normalize(value)
148+
let index = -1
141149

142-
normalize(value).forEach(function (value) {
143-
parseConfig(value, cache)
144-
})
150+
while (++index < normalized.length) {
151+
parseConfig(normalized[index], cache)
152+
}
145153

146154
return cache
147155
}
@@ -180,36 +188,26 @@ function handleUnknownArgument(flag) {
180188
}
181189

182190
// Short options, can be grouped.
183-
flag.slice(1).split('').forEach(each)
184-
185-
function each(key) {
186-
var length = schema.length
187-
var index = -1
188-
var option
189-
190-
while (++index < length) {
191-
option = schema[index]
192-
193-
if (option.short === key) {
194-
return
195-
}
191+
const found = flag.slice(1).split('')
192+
const known = schema.filter((d) => d.short)
193+
const knownKeys = new Set(known.map((d) => d.short))
194+
let index = -1
195+
196+
while (++index < found.length) {
197+
const key = found[index]
198+
if (!knownKeys.has(key)) {
199+
throw fault(
200+
'Unknown short option `-%s`, expected:\n%s',
201+
key,
202+
inspectAll(known)
203+
)
196204
}
197-
198-
throw fault(
199-
'Unknown short option `-%s`, expected:\n%s',
200-
key,
201-
inspectAll(schema.filter(short))
202-
)
203-
}
204-
205-
function short(option) {
206-
return option.short
207205
}
208206
}
209207

210208
// Inspect all `options`.
211209
function inspectAll(options) {
212-
return table(options.map(inspect))
210+
return table(options.map((d) => inspect(d)))
213211
}
214212

215213
// Inspect one `option`.
@@ -240,7 +238,7 @@ function normalize(value) {
240238
return [value]
241239
}
242240

243-
return flatten(value.map(normalize))
241+
return flatten(value.map((d) => normalize(d)))
244242
}
245243

246244
// Flatten `values`.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"touch": "^3.0.0",
5353
"unified": "^9.0.0",
5454
"vfile-reporter-json": "^2.0.0",
55-
"xo": "^0.36.0"
55+
"xo": "^0.38.0"
5656
},
5757
"scripts": {
5858
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",

‎test/fixtures/example/cli.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
var start = require('../../..')
5-
var config = require('../config')
6-
var processor = require('../processor')
4+
var start = require('../../../index.js')
5+
var config = require('../config.json')
6+
var processor = require('../processor.js')
77

88
start(Object.assign({}, config, {cwd: __dirname, processor: processor}))

‎test/fixtures/plugins/cli.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
var start = require('../../..')
5-
var config = require('../config')
6-
var processor = require('../processor')
4+
var start = require('../../../index.js')
5+
var config = require('../config.json')
6+
var processor = require('../processor.js')
77

88
start(
99
Object.assign({}, config, {

‎test/fixtures/settings/cli.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
var start = require('../../..')
5-
var config = require('../config')
6-
var processor = require('../processor')
4+
var start = require('../../../index.js')
5+
var config = require('../config.json')
6+
var processor = require('../processor.js')
77

88
start(
99
Object.assign({}, config, {

‎test/fixtures/uncaught-errors/cli.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
var start = require('../../..')
5-
var config = require('../config')
6-
var processor = require('../processor')
4+
var start = require('../../../index.js')
5+
var processor = require('../processor.js')
6+
var config = require('../config.json')
77

88
start(
99
Object.assign({}, config, {

‎test/index.js

+25-16
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ var unlink = fs.unlinkSync
1616

1717
var fixtures = join(__dirname, 'fixtures')
1818

19-
var helpFlags = ['-h', '--help']
20-
var versionFlags = ['-v', '--version']
21-
var extFlags = ['-e', '--ext']
22-
var settingsFlags = ['-s', '--setting']
23-
var useFlags = ['-u', '--use']
24-
2519
var cwd = join(fixtures, 'example')
2620
var bin = join(cwd, 'cli.js')
2721

@@ -176,7 +170,10 @@ test('unified-args', function (t) {
176170
}
177171
})
178172

179-
helpFlags.forEach(function (flag) {
173+
helpFlag('-h')
174+
helpFlag('--help')
175+
176+
function helpFlag(flag) {
180177
t.test('should show help on `' + flag + '`', function (t) {
181178
var expected = read(join(cwd, 'HELP'), 'utf8').replace(/\r/g, '').trim()
182179

@@ -192,9 +189,12 @@ test('unified-args', function (t) {
192189
)
193190
}
194191
})
195-
})
192+
}
196193

197-
versionFlags.forEach(function (flag) {
194+
versionFlag('-v')
195+
versionFlag('--version')
196+
197+
function versionFlag(flag) {
198198
t.test('should show help on `' + flag + '`', function (t) {
199199
t.plan(1)
200200

@@ -208,7 +208,7 @@ test('unified-args', function (t) {
208208
)
209209
}
210210
})
211-
})
211+
}
212212

213213
t.test('should honour `--color`', function (t) {
214214
var expected =
@@ -243,7 +243,10 @@ test('unified-args', function (t) {
243243
}
244244
})
245245

246-
extFlags.forEach(function (flag) {
246+
extFlag('-e')
247+
extFlag('--ext')
248+
249+
function extFlag(flag) {
247250
t.test('should honour `' + flag + '`', function (t) {
248251
var expected = [
249252
'alpha.text: no issues found',
@@ -304,9 +307,12 @@ test('unified-args', function (t) {
304307
)
305308
}
306309
})
307-
})
310+
}
311+
312+
settingsFlag('-s')
313+
settingsFlag('--setting')
308314

309-
settingsFlags.forEach(function (flag) {
315+
function settingsFlag(flag) {
310316
t.test('should catch syntax errors in `' + flag + '`', function (t) {
311317
var expected =
312318
"Error: Cannot parse `foo:bar` as JSON: JSON5: invalid character 'b' at 1:6"
@@ -341,7 +347,7 @@ test('unified-args', function (t) {
341347
)
342348
}
343349
})
344-
})
350+
}
345351

346352
t.test('shouldn’t fail on property-like settings', function (t) {
347353
var expected = '{"foo":"https://example.com"}'
@@ -361,7 +367,10 @@ test('unified-args', function (t) {
361367
}
362368
})
363369

364-
useFlags.forEach(function (flag) {
370+
useFlag('-u')
371+
useFlag('--use')
372+
373+
function useFlag(flag) {
365374
t.test('should load a plugin with `' + flag + '`', function (t) {
366375
var bin = join(fixtures, 'plugins', 'cli.js')
367376

@@ -416,7 +425,7 @@ test('unified-args', function (t) {
416425
)
417426
}
418427
})
419-
})
428+
}
420429

421430
t.test('should honour `--report`', function (t) {
422431
var expected = JSON.stringify([

0 commit comments

Comments
 (0)
Please sign in to comment.