1+ // tslint:disable:no-console
2+
13import { promises as fs } from 'fs' ;
24import rimraf from 'rimraf' ;
35import fetch from 'node-fetch' ;
@@ -7,22 +9,22 @@ import { once } from 'events';
79import { EOL } from 'os' ;
810import { promisify } from 'util' ;
911
10- const files = [
12+ const grammerFiles = [
1113 'JavaParser.g4' ,
1214 'JavaLexer.g4'
1315] ;
1416
1517const main = async ( ) => {
16- let isStale = await withLog (
18+ const isStale = await withLog (
1719 'Checking if head is stale... ' ,
1820 getIsStale ( ) ,
19- isStale => isStale ? 'Stale' : 'Up-to date'
21+ isStaleValue => isStaleValue ? 'Stale' : 'Up-to date'
2022 )
2123 if ( ! isStale && ! process . argv . includes ( '--force' ) ) {
2224 console . log ( 'Exiting, use --force to build anyway' ) ;
2325 return ;
2426 }
25- let files = await withLog ( 'Fetching files from upstream... ' , getFiles ( ) ) ;
27+ const files = await withLog ( 'Fetching files from upstream... ' , getFiles ( ) ) ;
2628 await withLog ( 'Writing files... ' , writeFiles ( files ) ) ;
2729 await withLog ( 'Updating head.json... ' , updateHead ( ) ) ;
2830 await withLog ( 'Generating parser...\n' , writeParser ( ) ) ;
@@ -32,8 +34,8 @@ const main = async () => {
3234}
3335
3436const getIsStale = async ( ) => {
35- let [ head , upstreamHead ] = await Promise . all ( [ getHead ( ) , getUpstreamHead ( ) ] ) ;
36- return files . some ( file => head [ file ] !== upstreamHead [ file ] ) ;
37+ const [ head , upstreamHead ] = await Promise . all ( [ getHead ( ) , getUpstreamHead ( ) ] ) ;
38+ return grammerFiles . some ( file => head [ file ] !== upstreamHead [ file ] ) ;
3739}
3840
3941const getHead = async ( ) =>
@@ -45,24 +47,24 @@ let upstreamHeadCache: { [file: string]: string } | undefined;
4547const getUpstreamHead = async ( ) => {
4648 if ( upstreamHeadCache ) return upstreamHeadCache ;
4749
48- let upstreamHead = mergeAll (
50+ const upstreamHead = mergeAll (
4951 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 ( ) ;
52+ grammerFiles . map ( async file => {
53+ const res = await fetch ( `https://api.github.com/repos/antlr/grammars-v4/commits?path=java/java/${ file } ` ) ;
54+ const commits = await res . json ( ) ;
5355 return { [ file ] : commits [ 0 ] . sha as string } ;
5456 } )
5557 )
5658 )
5759 upstreamHeadCache = upstreamHead ;
5860 return upstreamHead ;
5961}
60-
62+
6163const getFiles = async ( ) =>
6264 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 ( ) ;
65+ grammerFiles . map ( async file => {
66+ const res = await fetch ( `https://raw.githubusercontent.com/antlr/grammars-v4/master/java/java/${ file } ` )
67+ const data = await res . text ( ) ;
6668 return { [ file ] : data } ;
6769 } )
6870 ) )
@@ -85,13 +87,13 @@ const writeParser = () =>
8587 execCommand ( `${ prependBinDir ( 'antlr4ts' ) } -visitor -o src/parser -Xexact-output-dir src/parser/JavaLexer.g4 src/parser/JavaParser.g4` )
8688
8789const writeParserContexts = async ( ) => {
88- let listenerSource = await fs . readFile ( path . join ( __dirname , '/src/parser/JavaParserListener.ts' ) , 'utf-8' ) ;
90+ const listenerSource = await fs . readFile ( path . join ( __dirname , '/src/parser/JavaParserListener.ts' ) , 'utf-8' ) ;
8991
90- let exportList =
92+ const exportList =
9193 listenerSource
9294 . split ( EOL )
9395 . map ( ( l ) => {
94- let matches = l . match ( / i m p o r t \s * \{ \s * ( .* C o n t e x t ) \s * \} .* / ) ;
96+ const matches = l . match ( / i m p o r t \s * \{ \s * ( .* C o n t e x t ) \s * \} .* / ) ;
9597 if ( matches === null ) return null ;
9698 return matches [ 1 ] ;
9799 } )
@@ -105,7 +107,7 @@ const writeParserContexts = async () => {
105107}
106108
107109const writeJavascript = async ( ) => {
108- await promisify ( rimraf ) ( path . join ( __dirname , " /dist" ) )
110+ await promisify ( rimraf ) ( path . join ( __dirname , ' /dist' ) )
109111 await execCommand ( prependBinDir ( 'tsc' ) )
110112}
111113
@@ -116,7 +118,7 @@ const withLog = async <T>(
116118) => {
117119 process . stdout . write ( label ) ;
118120 try {
119- let value = await promise ;
121+ const value = await promise ;
120122 process . stdout . write ( fulfilMessage ( value ) + '\n' )
121123 return value ;
122124 } catch ( error ) {
@@ -126,21 +128,21 @@ const withLog = async <T>(
126128}
127129
128130const execCommand = async ( command : string ) => {
129- let childProcess = exec ( command , { cwd : __dirname } )
131+ const childProcess = exec ( command , { cwd : __dirname } )
130132 childProcess . stdout . pipe ( process . stdout ) ;
131133 childProcess . stderr . pipe ( process . stderr ) ;
132134
133- let [ code ] = await once ( childProcess , 'exit' ) as [ number ] ;
135+ const [ code ] = await once ( childProcess , 'exit' ) as [ number ] ;
134136 if ( code !== 0 ) throw undefined ;
135137}
136138
137139const prependBinDir = ( p : string ) =>
138- path . join ( __dirname , " /node_modules/.bin/" , p ) ;
140+ path . join ( __dirname , ' /node_modules/.bin/' , p ) ;
139141
140142type MergeAll = < T extends object [ ] > ( xs : T ) => UnionToIntersection < T [ number ] > ;
141143const mergeAll : MergeAll = xs => xs . reduce ( ( m , x ) => ( { ...m , ...x } ) , { } ) as any ;
142-
143- type UnionToIntersection < U > =
144+
145+ type UnionToIntersection < U > =
144146 ( U extends any ? ( k : U ) => void : never ) extends ( ( k : infer I ) => void ) ? I : never
145147
146148main ( ) ;
0 commit comments