Skip to content

Commit 470ec08

Browse files
authored
Cli support parsing multiple files (#1060)
Close #825 Co-authored-by: 成仕伟 <[email protected]> FEATURE: Allow multiple input files to be passed to the CLI tool.
1 parent 147b99e commit 470ec08

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

acorn/src/bin/acorn.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@ import {basename} from "path"
22
import {readFileSync as readFile} from "fs"
33
import * as acorn from "acorn"
44

5-
let infile, forceFile, silent = false, compact = false, tokenize = false
5+
let inputFilePaths = [], forceFileName = false, fileMode = false, silent = false, compact = false, tokenize = false
66
const options = {}
77

88
function help(status) {
99
const print = (status === 0) ? console.log : console.error
1010
print("usage: " + basename(process.argv[1]) + " [--ecma3|--ecma5|--ecma6|--ecma7|--ecma8|--ecma9|...|--ecma2015|--ecma2016|--ecma2017|--ecma2018|...]")
11-
print(" [--tokenize] [--locations] [---allow-hash-bang] [--allow-await-outside-function] [--compact] [--silent] [--module] [--help] [--] [infile]")
11+
print(" [--tokenize] [--locations] [---allow-hash-bang] [--allow-await-outside-function] [--compact] [--silent] [--module] [--help] [--] [<infile>...]")
1212
process.exit(status)
1313
}
1414

1515
for (let i = 2; i < process.argv.length; ++i) {
1616
const arg = process.argv[i]
17-
if ((arg === "-" || arg[0] !== "-") && !infile) infile = arg
18-
else if (arg === "--" && !infile && i + 2 === process.argv.length) forceFile = infile = process.argv[++i]
19-
else if (arg === "--locations") options.locations = true
17+
if (arg[0] !== "-" || arg === "-") inputFilePaths.push(arg)
18+
else if (arg === "--") {
19+
inputFilePaths.push(...process.argv.slice(i + 1))
20+
forceFileName = true
21+
break
22+
} else if (arg === "--locations") options.locations = true
2023
else if (arg === "--allow-hash-bang") options.allowHashBang = true
2124
else if (arg === "--allow-await-outside-function") options.allowAwaitOutsideFunction = true
2225
else if (arg === "--silent") silent = true
@@ -33,31 +36,34 @@ for (let i = 2; i < process.argv.length; ++i) {
3336
}
3437
}
3538

36-
function run(code) {
37-
let result
39+
function run(codeList) {
40+
let result = [], fileIdx = 0
3841
try {
39-
if (!tokenize) {
40-
result = acorn.parse(code, options)
41-
} else {
42-
result = []
43-
let tokenizer = acorn.tokenizer(code, options), token
44-
do {
45-
token = tokenizer.getToken()
46-
result.push(token)
47-
} while (token.type !== acorn.tokTypes.eof)
48-
}
42+
codeList.forEach((code, idx) => {
43+
fileIdx = idx
44+
if (!tokenize) {
45+
result = acorn.parse(code, options)
46+
options.program = result
47+
} else {
48+
let tokenizer = acorn.tokenizer(code, options), token
49+
do {
50+
token = tokenizer.getToken()
51+
result.push(token)
52+
} while (token.type !== acorn.tokTypes.eof)
53+
}
54+
})
4955
} catch (e) {
50-
console.error(infile && infile !== "-" ? e.message.replace(/\(\d+:\d+\)$/, m => m.slice(0, 1) + infile + " " + m.slice(1)) : e.message)
56+
console.error(fileMode ? e.message.replace(/\(\d+:\d+\)$/, m => m.slice(0, 1) + inputFilePaths[fileIdx] + " " + m.slice(1)) : e.message)
5157
process.exit(1)
5258
}
5359
if (!silent) console.log(JSON.stringify(result, null, compact ? null : 2))
5460
}
5561

56-
if (forceFile || infile && infile !== "-") {
57-
run(readFile(infile, "utf8"))
62+
if (fileMode = inputFilePaths.length && (forceFileName || !inputFilePaths.includes("-") || inputFilePaths.length !== 1)) {
63+
run(inputFilePaths.map(path => readFile(path, "utf8")))
5864
} else {
5965
let code = ""
6066
process.stdin.resume()
6167
process.stdin.on("data", chunk => code += chunk)
62-
process.stdin.on("end", () => run(code))
68+
process.stdin.on("end", () => run([code]))
6369
}

0 commit comments

Comments
 (0)