@@ -12,67 +12,60 @@ const files = [
12
12
'JavaLexer.g4'
13
13
] ;
14
14
15
- const main = ( ) =>
16
- withLog (
15
+ const main = async ( ) => {
16
+ let isStale = await withLog (
17
17
'Checking if head is stale... ' ,
18
18
getIsStale ( ) ,
19
19
isStale => isStale ? 'Stale' : 'Up-to date'
20
20
)
21
- . then ( isStale => isStale || process . argv . includes ( '--force' ) )
22
- . then ( shouldBuild =>
23
- ! shouldBuild
24
- ? ( console . log ( 'Exiting, use --force to build anyway' ) , Promise . reject ( terminationSignal ) )
25
- : Promise . resolve ( )
26
- )
27
- . then ( ( ) => withLog ( 'Fetching files from upstream... ' , getFiles ( ) ) )
28
- . then ( files => withLog ( 'Writing files... ' , writeFiles ( files ) ) )
29
- . then ( ( ) => withLog ( 'Updating head.json... ' , updateHead ( ) ) )
30
- . then ( ( ) => withLog ( 'Generating parser...\n' , writeParser ( ) ) )
31
- . then ( ( ) => withLog ( 'Generating contexts... ' , writeParserContexts ( ) ) )
32
- . then ( ( ) => withLog ( 'Compiling typescript files... ' , writeJavascript ( ) ) )
33
- . then ( ( ) => console . log ( 'Build successful!' ) )
34
- . catch ( payload =>
35
- payload === terminationSignal
36
- ? Promise . resolve ( )
37
- : Promise . reject ( payload )
38
- )
21
+ if ( ! isStale && ! process . argv . includes ( '--force' ) ) {
22
+ console . log ( 'Exiting, use --force to build anyway' ) ;
23
+ return ;
24
+ }
25
+ let files = await withLog ( 'Fetching files from upstream... ' , getFiles ( ) ) ;
26
+ await withLog ( 'Writing files... ' , writeFiles ( files ) ) ;
27
+ await withLog ( 'Updating head.json... ' , updateHead ( ) ) ;
28
+ await withLog ( 'Generating parser...\n' , writeParser ( ) ) ;
29
+ await withLog ( 'Generating contexts... ' , writeParserContexts ( ) ) ;
30
+ await withLog ( 'Compiling typescript files... ' , writeJavascript ( ) ) ;
31
+ console . log ( 'Build successful!' ) ;
32
+ }
39
33
40
- const getIsStale = ( ) =>
41
- Promise . all ( [ getHead ( ) , getUpstreamHead ( ) ] )
42
- . then ( ( [ head , upstreamHead ] ) =>
43
- files . some ( file => head [ file ] !== upstreamHead [ file ] )
44
- )
34
+ const getIsStale = async ( ) => {
35
+ let [ head , upstreamHead ] = await Promise . all ( [ getHead ( ) , getUpstreamHead ( ) ] ) ;
36
+ return files . some ( file => head [ file ] !== upstreamHead [ file ] ) ;
37
+ }
45
38
46
- const getHead = ( ) =>
47
- fs . readFile ( path . join ( __dirname , 'src/head.json' ) , 'utf-8' )
48
- . then ( JSON . parse ) as Promise < { [ file : string ] : string } >
39
+ const getHead = async ( ) =>
40
+ JSON . parse (
41
+ await fs . readFile ( path . join ( __dirname , 'src/head.json' ) , 'utf-8' )
42
+ ) as { [ file : string ] : string } ;
49
43
50
44
let upstreamHeadCache : { [ file : string ] : string } | undefined ;
51
- const getUpstreamHead = ( ) =>
52
- upstreamHeadCache ? Promise . resolve ( upstreamHeadCache ) :
53
- Promise . all (
54
- files . map ( file =>
55
- fetch ( `https://api.github.com/repos/antlr/grammars-v4/commits?path=java/java/${ file } ` )
56
- . then ( res => res . json ( ) )
57
- . then ( commits => ( { [ file ] : commits [ 0 ] . sha as string } ) )
45
+ const getUpstreamHead = async ( ) => {
46
+ if ( upstreamHeadCache ) return upstreamHeadCache ;
47
+
48
+ let upstreamHead = mergeAll (
49
+ await Promise . all (
50
+ files . map ( async file => {
51
+ let res = await fetch ( `https://api.github.com/repos/antlr/grammars-v4/commits?path=java/java/${ file } ` ) ;
52
+ let commits = await res . json ( ) ;
53
+ return { [ file ] : commits [ 0 ] . sha as string } ;
54
+ } )
58
55
)
59
56
)
60
- . then ( mergeAll )
61
- . then ( upstreamHead => {
62
- upstreamHeadCache = upstreamHead ;
63
- return Promise . resolve ( upstreamHead ) ;
64
- } ) ;
57
+ upstreamHeadCache = upstreamHead ;
58
+ return upstreamHead ;
59
+ }
65
60
66
- const getFiles = ( ) =>
67
- Promise . all (
68
- files . map (
69
- file =>
70
- fetch ( `https://raw.githubusercontent.com/antlr/grammars-v4/master/java/java/${ file } ` )
71
- . then ( res => res . text ( ) )
72
- . then ( data => ( { [ file ] : data } ) )
73
- )
74
- )
75
- . then ( mergeAll )
61
+ const getFiles = async ( ) =>
62
+ mergeAll ( await Promise . all (
63
+ files . map ( async file => {
64
+ let res = await fetch ( `https://raw.githubusercontent.com/antlr/grammars-v4/master/java/java/${ file } ` )
65
+ let data = await res . text ( ) ;
66
+ return { [ file ] : data } ;
67
+ } )
68
+ ) )
76
69
77
70
const writeFiles = ( files : { [ file : string ] : string } ) =>
78
71
Promise . all (
@@ -82,21 +75,19 @@ const writeFiles = (files: { [file: string]: string }) =>
82
75
)
83
76
)
84
77
85
- const updateHead = ( ) =>
86
- getUpstreamHead ( )
87
- . then ( head =>
88
- fs . writeFile (
89
- path . join ( __dirname , 'src/head.json' ) ,
90
- JSON . stringify ( head , null , ' ' )
91
- )
78
+ const updateHead = async ( ) =>
79
+ fs . writeFile (
80
+ path . join ( __dirname , 'src/head.json' ) ,
81
+ JSON . stringify ( await getUpstreamHead ( ) , null , ' ' )
92
82
)
93
83
94
84
const writeParser = ( ) =>
95
85
execCommand ( `${ prependBinDir ( 'antlr4ts' ) } -visitor -o src/parser -Xexact-output-dir src/parser/JavaLexer.g4 src/parser/JavaParser.g4` )
96
86
97
- const writeParserContexts = ( ) =>
98
- fs . readFile ( path . join ( __dirname , '/src/parser/JavaParserListener.ts' ) , 'utf-8' )
99
- . then ( listenerSource =>
87
+ const writeParserContexts = async ( ) => {
88
+ let listenerSource = await fs . readFile ( path . join ( __dirname , '/src/parser/JavaParserListener.ts' ) , 'utf-8' ) ;
89
+
90
+ let exportList =
100
91
listenerSource
101
92
. split ( EOL )
102
93
. map ( ( l ) => {
@@ -105,43 +96,42 @@ const writeParserContexts = () =>
105
96
return matches [ 1 ] ;
106
97
} )
107
98
. filter ( ( c ) => c !== null )
108
- )
109
- . then ( contexts => contexts . reduce ( ( list , context ) => list + ` ${ context } ,${ EOL } ` , '' ) )
110
- . then ( exportList => `export {${ EOL } ${ exportList } } from './JavaParser';` )
111
- . then ( contextsSource => fs . writeFile ( path . join ( __dirname , '/src/parser/JavaContexts.ts' ) , contextsSource ) ) ;
99
+ . reduce ( ( list , context ) => list + ` ${ context } ,${ EOL } ` , '' ) ;
112
100
113
- const writeJavascript = ( ) =>
114
- promisify ( rimraf ) ( path . join ( __dirname , "/dist" ) )
115
- . then ( ( ) => execCommand ( prependBinDir ( 'tsc' ) ) ) ;
101
+ await fs . writeFile (
102
+ path . join ( __dirname , '/src/parser/JavaContexts.ts' ) ,
103
+ `export {${ EOL } ${ exportList } } from './JavaParser';`
104
+ ) ;
105
+ }
116
106
117
- const withLog = < T > (
107
+ const writeJavascript = async ( ) => {
108
+ await promisify ( rimraf ) ( path . join ( __dirname , "/dist" ) )
109
+ await execCommand ( prependBinDir ( 'tsc' ) )
110
+ }
111
+
112
+ const withLog = async < T > (
118
113
label : string ,
119
114
promise : Promise < T > ,
120
115
fulfilMessage : ( ( value : T ) => string ) = ( ) => 'Done'
121
116
) => {
122
117
process . stdout . write ( label ) ;
123
- return promise
124
- . then ( value => {
118
+ try {
119
+ let value = await promise ;
125
120
process . stdout . write ( fulfilMessage ( value ) + '\n' )
126
- return Promise . resolve ( value ) ;
127
- } )
128
- . catch ( error => {
121
+ return value ;
122
+ } catch ( error ) {
129
123
process . stdout . write ( 'Something went wrong\n' ) ;
130
- return Promise . reject ( error ) ;
131
- } )
124
+ throw error ;
125
+ }
132
126
}
133
127
134
- const terminationSignal = Symbol ( 'terminationSignal' ) ;
135
-
136
- const execCommand = ( command : string ) => {
128
+ const execCommand = async ( command : string ) => {
137
129
let childProcess = exec ( command , { cwd : __dirname } )
138
130
childProcess . stdout . pipe ( process . stdout ) ;
139
131
childProcess . stderr . pipe ( process . stderr ) ;
140
132
141
- return (
142
- once ( childProcess , 'exit' )
143
- . then ( ( [ code ] : [ number ] ) => code === 0 ? Promise . resolve ( ) : Promise . reject ( ) )
144
- )
133
+ let [ code ] = await once ( childProcess , 'exit' ) as [ number ] ;
134
+ if ( code !== 0 ) throw undefined ;
145
135
}
146
136
147
137
const prependBinDir = ( p : string ) =>
0 commit comments