@@ -12,67 +12,60 @@ const files = [
1212 'JavaLexer.g4'
1313] ;
1414
15- const main = ( ) =>
16- withLog (
15+ const main = async ( ) => {
16+ let isStale = await withLog (
1717 'Checking if head is stale... ' ,
1818 getIsStale ( ) ,
1919 isStale => isStale ? 'Stale' : 'Up-to date'
2020 )
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+ }
3933
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+ }
4538
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 } ;
4943
5044let 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+ } )
5855 )
5956 )
60- . then ( mergeAll )
61- . then ( upstreamHead => {
62- upstreamHeadCache = upstreamHead ;
63- return Promise . resolve ( upstreamHead ) ;
64- } ) ;
57+ upstreamHeadCache = upstreamHead ;
58+ return upstreamHead ;
59+ }
6560
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+ ) )
7669
7770const writeFiles = ( files : { [ file : string ] : string } ) =>
7871 Promise . all (
@@ -82,21 +75,19 @@ const writeFiles = (files: { [file: string]: string }) =>
8275 )
8376 )
8477
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 , ' ' )
9282 )
9383
9484const writeParser = ( ) =>
9585 execCommand ( `${ prependBinDir ( 'antlr4ts' ) } -visitor -o src/parser -Xexact-output-dir src/parser/JavaLexer.g4 src/parser/JavaParser.g4` )
9686
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 =
10091 listenerSource
10192 . split ( EOL )
10293 . map ( ( l ) => {
@@ -105,43 +96,42 @@ const writeParserContexts = () =>
10596 return matches [ 1 ] ;
10697 } )
10798 . 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 } ` , '' ) ;
112100
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+ }
116106
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 > (
118113 label : string ,
119114 promise : Promise < T > ,
120115 fulfilMessage : ( ( value : T ) => string ) = ( ) => 'Done'
121116) => {
122117 process . stdout . write ( label ) ;
123- return promise
124- . then ( value => {
118+ try {
119+ let value = await promise ;
125120 process . stdout . write ( fulfilMessage ( value ) + '\n' )
126- return Promise . resolve ( value ) ;
127- } )
128- . catch ( error => {
121+ return value ;
122+ } catch ( error ) {
129123 process . stdout . write ( 'Something went wrong\n' ) ;
130- return Promise . reject ( error ) ;
131- } )
124+ throw error ;
125+ }
132126}
133127
134- const terminationSignal = Symbol ( 'terminationSignal' ) ;
135-
136- const execCommand = ( command : string ) => {
128+ const execCommand = async ( command : string ) => {
137129 let childProcess = exec ( command , { cwd : __dirname } )
138130 childProcess . stdout . pipe ( process . stdout ) ;
139131 childProcess . stderr . pipe ( process . stderr ) ;
140132
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 ;
145135}
146136
147137const prependBinDir = ( p : string ) =>
0 commit comments