@@ -2,21 +2,24 @@ import {basename} from "path"
2
2
import { readFileSync as readFile } from "fs"
3
3
import * as acorn from "acorn"
4
4
5
- let infile , forceFile , silent = false , compact = false , tokenize = false
5
+ let inputFilePaths = [ ] , forceFileName = false , fileMode = false , silent = false , compact = false , tokenize = false
6
6
const options = { }
7
7
8
8
function help ( status ) {
9
9
const print = ( status === 0 ) ? console . log : console . error
10
10
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>... ]" )
12
12
process . exit ( status )
13
13
}
14
14
15
15
for ( let i = 2 ; i < process . argv . length ; ++ i ) {
16
16
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
20
23
else if ( arg === "--allow-hash-bang" ) options . allowHashBang = true
21
24
else if ( arg === "--allow-await-outside-function" ) options . allowAwaitOutsideFunction = true
22
25
else if ( arg === "--silent" ) silent = true
@@ -33,31 +36,34 @@ for (let i = 2; i < process.argv.length; ++i) {
33
36
}
34
37
}
35
38
36
- function run ( code ) {
37
- let result
39
+ function run ( codeList ) {
40
+ let result = [ ] , fileIdx = 0
38
41
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
+ } )
49
55
} 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 )
51
57
process . exit ( 1 )
52
58
}
53
59
if ( ! silent ) console . log ( JSON . stringify ( result , null , compact ? null : 2 ) )
54
60
}
55
61
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" ) ) )
58
64
} else {
59
65
let code = ""
60
66
process . stdin . resume ( )
61
67
process . stdin . on ( "data" , chunk => code += chunk )
62
- process . stdin . on ( "end" , ( ) => run ( code ) )
68
+ process . stdin . on ( "end" , ( ) => run ( [ code ] ) )
63
69
}
0 commit comments