@@ -4,21 +4,24 @@ var path = require('path');
4
4
var fs = require ( 'fs' ) ;
5
5
var acorn = require ( './acorn.js' ) ;
6
6
7
- var infile , forceFile , silent = false , compact = false , tokenize = false ;
7
+ var inputFilePaths = [ ] , forceFileName = false , fileMode = false , silent = false , compact = false , tokenize = false ;
8
8
var options = { } ;
9
9
10
10
function help ( status ) {
11
11
var print = ( status === 0 ) ? console . log : console . error ;
12
12
print ( "usage: " + path . basename ( process . argv [ 1 ] ) + " [--ecma3|--ecma5|--ecma6|--ecma7|--ecma8|--ecma9|...|--ecma2015|--ecma2016|--ecma2017|--ecma2018|...]" ) ;
13
- print ( " [--tokenize] [--locations] [---allow-hash-bang] [--allow-await-outside-function] [--compact] [--silent] [--module] [--help] [--] [infile]" ) ;
13
+ print ( " [--tokenize] [--locations] [---allow-hash-bang] [--allow-await-outside-function] [--compact] [--silent] [--module] [--help] [--] [< infile>... ]" ) ;
14
14
process . exit ( status ) ;
15
15
}
16
16
17
17
for ( var i = 2 ; i < process . argv . length ; ++ i ) {
18
18
var arg = process . argv [ i ] ;
19
- if ( ( arg === "-" || arg [ 0 ] !== "-" ) && ! infile ) { infile = arg ; }
20
- else if ( arg === "--" && ! infile && i + 2 === process . argv . length ) { forceFile = infile = process . argv [ ++ i ] ; }
21
- else if ( arg === "--locations" ) { options . locations = true ; }
19
+ if ( arg [ 0 ] !== "-" || arg === "-" ) { inputFilePaths . push ( arg ) ; }
20
+ else if ( arg === "--" ) {
21
+ inputFilePaths . push . apply ( inputFilePaths , process . argv . slice ( i + 1 ) ) ;
22
+ forceFileName = true ;
23
+ break
24
+ } else if ( arg === "--locations" ) { options . locations = true ; }
22
25
else if ( arg === "--allow-hash-bang" ) { options . allowHashBang = true ; }
23
26
else if ( arg === "--allow-await-outside-function" ) { options . allowAwaitOutsideFunction = true ; }
24
27
else if ( arg === "--silent" ) { silent = true ; }
@@ -35,31 +38,34 @@ for (var i = 2; i < process.argv.length; ++i) {
35
38
}
36
39
}
37
40
38
- function run ( code ) {
39
- var result ;
41
+ function run ( codeList ) {
42
+ var result = [ ] , fileIdx = 0 ;
40
43
try {
41
- if ( ! tokenize ) {
42
- result = acorn . parse ( code , options ) ;
43
- } else {
44
- result = [ ] ;
45
- var tokenizer = acorn . tokenizer ( code , options ) , token ;
46
- do {
47
- token = tokenizer . getToken ( ) ;
48
- result . push ( token ) ;
49
- } while ( token . type !== acorn . tokTypes . eof )
50
- }
44
+ codeList . forEach ( function ( code , idx ) {
45
+ fileIdx = idx ;
46
+ if ( ! tokenize ) {
47
+ result = acorn . parse ( code , options ) ;
48
+ options . program = result ;
49
+ } else {
50
+ var tokenizer = acorn . tokenizer ( code , options ) , token ;
51
+ do {
52
+ token = tokenizer . getToken ( ) ;
53
+ result . push ( token ) ;
54
+ } while ( token . type !== acorn . tokTypes . eof )
55
+ }
56
+ } ) ;
51
57
} catch ( e ) {
52
- console . error ( infile && infile !== "-" ? e . message . replace ( / \( \d + : \d + \) $ / , function ( m ) { return m . slice ( 0 , 1 ) + infile + " " + m . slice ( 1 ) ; } ) : e . message ) ;
58
+ console . error ( fileMode ? e . message . replace ( / \( \d + : \d + \) $ / , function ( m ) { return m . slice ( 0 , 1 ) + inputFilePaths [ fileIdx ] + " " + m . slice ( 1 ) ; } ) : e . message ) ;
53
59
process . exit ( 1 ) ;
54
60
}
55
61
if ( ! silent ) { console . log ( JSON . stringify ( result , null , compact ? null : 2 ) ) ; }
56
62
}
57
63
58
- if ( forceFile || infile && infile !== "-" ) {
59
- run ( fs . readFileSync ( infile , "utf8" ) ) ;
64
+ if ( fileMode = inputFilePaths . length && ( forceFileName || ! inputFilePaths . includes ( "-" ) || inputFilePaths . length !== 1 ) ) {
65
+ run ( inputFilePaths . map ( function ( path ) { return fs . readFileSync ( path , "utf8" ) ; } ) ) ;
60
66
} else {
61
67
var code = "" ;
62
68
process . stdin . resume ( ) ;
63
69
process . stdin . on ( "data" , function ( chunk ) { return code += chunk ; } ) ;
64
- process . stdin . on ( "end" , function ( ) { return run ( code ) ; } ) ;
70
+ process . stdin . on ( "end" , function ( ) { return run ( [ code ] ) ; } ) ;
65
71
}
0 commit comments