Skip to content

Commit 7466552

Browse files
committed
fix: remove minimist
1 parent 4cf57da commit 7466552

File tree

5 files changed

+201
-37
lines changed

5 files changed

+201
-37
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
### Unreleased [[code][c-unreleased], [diff][d-unreleased]]
22

3+
- Fix: Remove dependence on minimist to patch CVE-2021-44906. ([#266])
4+
35
[c-unreleased]: https://github.com/json5/json5/tree/master
46
[d-unreleased]: https://github.com/json5/json5/compare/v2.2.0...HEAD
57

@@ -360,3 +362,4 @@ parser for the regular JSON format.
360362
[#229]: https://github.com/json5/json5/issues/229
361363
[#236]: https://github.com/json5/json5/issues/236
362364
[#244]: https://github.com/json5/json5/issues/244
365+
[#266]: https://github.com/json5/json5/issues/266

lib/cli.js

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,17 @@
22

33
const fs = require('fs')
44
const path = require('path')
5-
const minimist = require('minimist')
65
const pkg = require('../package.json')
76
const JSON5 = require('./')
87

9-
const argv = minimist(process.argv.slice(2), {
10-
alias: {
11-
'convert': 'c',
12-
'space': 's',
13-
'validate': 'v',
14-
'out-file': 'o',
15-
'version': 'V',
16-
'help': 'h',
17-
},
18-
boolean: [
19-
'convert',
20-
'validate',
21-
'version',
22-
'help',
23-
],
24-
string: [
25-
'space',
26-
'out-file',
27-
],
28-
})
8+
const argv = parseArgs()
299

3010
if (argv.version) {
3111
version()
3212
} else if (argv.help) {
3313
usage()
3414
} else {
35-
const inFilename = argv._[0]
15+
const inFilename = argv.defaults[0]
3616

3717
let readStream
3818
if (inFilename) {
@@ -65,7 +45,7 @@ if (argv.version) {
6545
// --convert is for backward compatibility with v0.5.1. If
6646
// specified with <file> and not --out-file, then a file with
6747
// the same name but with a .json extension will be written.
68-
if (argv.convert && inFilename && !argv.o) {
48+
if (argv.convert && inFilename && !argv.outFile) {
6949
const parsedFilename = path.parse(inFilename)
7050
const outFilename = path.format(
7151
Object.assign(
@@ -75,8 +55,8 @@ if (argv.version) {
7555
)
7656

7757
writeStream = fs.createWriteStream(outFilename)
78-
} else if (argv.o) {
79-
writeStream = fs.createWriteStream(argv.o)
58+
} else if (argv.outFile) {
59+
writeStream = fs.createWriteStream(argv.outFile)
8060
} else {
8161
writeStream = process.stdout
8262
}
@@ -90,6 +70,66 @@ if (argv.version) {
9070
})
9171
}
9272

73+
function parseArgs () {
74+
let convert
75+
let space
76+
let validate
77+
let outFile
78+
let version
79+
let help
80+
const defaults = []
81+
82+
const args = process.argv.slice(2)
83+
for (let i = 0; i < args.length; i++) {
84+
const arg = args[i]
85+
switch (arg) {
86+
case '--convert':
87+
case '-c':
88+
convert = true
89+
break
90+
91+
case '--space':
92+
case '-s':
93+
space = args[++i]
94+
break
95+
96+
case '--validate':
97+
case '-v':
98+
validate = true
99+
break
100+
101+
case '--out-file':
102+
case '-o':
103+
outFile = args[++i]
104+
break
105+
106+
case '--version':
107+
case '-V':
108+
version = true
109+
break
110+
111+
case '--help':
112+
case '-h':
113+
help = true
114+
break
115+
116+
default:
117+
defaults.push(arg)
118+
break
119+
}
120+
}
121+
122+
return {
123+
convert,
124+
space,
125+
validate,
126+
outFile,
127+
version,
128+
help,
129+
defaults,
130+
}
131+
}
132+
93133
function version () {
94134
console.log(pkg.version)
95135
}

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@
4848
"url": "https://github.com/json5/json5/issues"
4949
},
5050
"homepage": "http://json5.org/",
51-
"dependencies": {
52-
"minimist": "^1.2.5"
53-
},
5451
"devDependencies": {
5552
"core-js": "^2.6.5",
5653
"eslint": "^5.15.3",

test/cli.js

Lines changed: 131 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ tap.test('CLI', t => {
4343
})
4444
})
4545

46-
t.test('indents output with the number of spaces specified', t => {
46+
t.test('indents output with the number of spaces specified with -s', t => {
4747
const proc = child.spawn(
4848
process.execPath,
4949
[
@@ -65,7 +65,29 @@ tap.test('CLI', t => {
6565
})
6666
})
6767

68-
t.test('indents output with tabs when specified', t => {
68+
t.test('indents output with the number of spaces specified with --space', t => {
69+
const proc = child.spawn(
70+
process.execPath,
71+
[
72+
cliPath,
73+
path.resolve(__dirname, 'test.json5'),
74+
'--space',
75+
'4',
76+
]
77+
)
78+
79+
let output = ''
80+
proc.stdout.on('data', data => {
81+
output += data
82+
})
83+
84+
proc.stdout.on('end', () => {
85+
assert.strictEqual(output, '{\n "a": 1,\n "b": 2\n}')
86+
t.end()
87+
})
88+
})
89+
90+
t.test('indents output with tabs when specified with -s', t => {
6991
const proc = child.spawn(
7092
process.execPath,
7193
[
@@ -87,7 +109,7 @@ tap.test('CLI', t => {
87109
})
88110
})
89111

90-
t.test('outputs to the specified file', t => {
112+
t.test('outputs to the specified file with -o', t => {
91113
const proc = child.spawn(
92114
process.execPath,
93115
[
@@ -116,7 +138,36 @@ tap.test('CLI', t => {
116138
})
117139
})
118140

119-
t.test('validates valid JSON5 files', t => {
141+
t.test('outputs to the specified file with --out-file', t => {
142+
const proc = child.spawn(
143+
process.execPath,
144+
[
145+
cliPath,
146+
path.resolve(__dirname, 'test.json5'),
147+
'--out-file',
148+
path.resolve(__dirname, 'output.json'),
149+
]
150+
)
151+
152+
proc.on('exit', () => {
153+
assert.strictEqual(
154+
fs.readFileSync(
155+
path.resolve(__dirname, 'output.json'),
156+
'utf8'
157+
),
158+
'{"a":1,"b":2}'
159+
)
160+
t.end()
161+
})
162+
163+
t.tearDown(() => {
164+
try {
165+
fs.unlinkSync(path.resolve(__dirname, 'output.json'))
166+
} catch (err) {}
167+
})
168+
})
169+
170+
t.test('validates valid JSON5 files with -v', t => {
120171
const proc = child.spawn(
121172
process.execPath,
122173
[
@@ -132,7 +183,23 @@ tap.test('CLI', t => {
132183
})
133184
})
134185

135-
t.test('validates invalid JSON5 files', t => {
186+
t.test('validates valid JSON5 files with --validate', t => {
187+
const proc = child.spawn(
188+
process.execPath,
189+
[
190+
cliPath,
191+
path.resolve(__dirname, 'test.json5'),
192+
'--validate',
193+
]
194+
)
195+
196+
proc.on('exit', code => {
197+
assert.strictEqual(code, 0)
198+
t.end()
199+
})
200+
})
201+
202+
t.test('validates invalid JSON5 files with -v', t => {
136203
const proc = child.spawn(
137204
process.execPath,
138205
[
@@ -157,7 +224,7 @@ tap.test('CLI', t => {
157224
})
158225
})
159226

160-
t.test('outputs the version number when specified', t => {
227+
t.test('outputs the version number when specified with -V', t => {
161228
const proc = child.spawn(process.execPath, [cliPath, '-V'])
162229

163230
let output = ''
@@ -171,7 +238,21 @@ tap.test('CLI', t => {
171238
})
172239
})
173240

174-
t.test('outputs usage information when specified', t => {
241+
t.test('outputs the version number when specified with --version', t => {
242+
const proc = child.spawn(process.execPath, [cliPath, '--version'])
243+
244+
let output = ''
245+
proc.stdout.on('data', data => {
246+
output += data
247+
})
248+
249+
proc.stdout.on('end', () => {
250+
assert.strictEqual(output, pkg.version + '\n')
251+
t.end()
252+
})
253+
})
254+
255+
t.test('outputs usage information when specified with -h', t => {
175256
const proc = child.spawn(process.execPath, [cliPath, '-h'])
176257

177258
let output = ''
@@ -185,7 +266,21 @@ tap.test('CLI', t => {
185266
})
186267
})
187268

188-
t.test('is backward compatible with v0.5.1', t => {
269+
t.test('outputs usage information when specified with --help', t => {
270+
const proc = child.spawn(process.execPath, [cliPath, '--help'])
271+
272+
let output = ''
273+
proc.stdout.on('data', data => {
274+
output += data
275+
})
276+
277+
proc.stdout.on('end', () => {
278+
assert(/Usage/.test(output))
279+
t.end()
280+
})
281+
})
282+
283+
t.test('is backward compatible with v0.5.1 with -c', t => {
189284
const proc = child.spawn(
190285
process.execPath,
191286
[
@@ -213,5 +308,33 @@ tap.test('CLI', t => {
213308
})
214309
})
215310

311+
t.test('is backward compatible with v0.5.1 with --convert', t => {
312+
const proc = child.spawn(
313+
process.execPath,
314+
[
315+
cliPath,
316+
'--convert',
317+
path.resolve(__dirname, 'test.json5'),
318+
]
319+
)
320+
321+
proc.on('exit', () => {
322+
assert.strictEqual(
323+
fs.readFileSync(
324+
path.resolve(__dirname, 'test.json'),
325+
'utf8'
326+
),
327+
'{"a":1,"b":2}'
328+
)
329+
t.end()
330+
})
331+
332+
t.tearDown(() => {
333+
try {
334+
fs.unlinkSync(path.resolve(__dirname, 'test.json'))
335+
} catch (err) {}
336+
})
337+
})
338+
216339
t.end()
217340
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy