1
+ // tslint:disable:no-console
2
+
1
3
import { promises as fs } from 'fs' ;
2
4
import rimraf from 'rimraf' ;
3
5
import fetch from 'node-fetch' ;
@@ -7,22 +9,22 @@ import { once } from 'events';
7
9
import { EOL } from 'os' ;
8
10
import { promisify } from 'util' ;
9
11
10
- const files = [
12
+ const grammerFiles = [
11
13
'JavaParser.g4' ,
12
14
'JavaLexer.g4'
13
15
] ;
14
16
15
17
const main = async ( ) => {
16
- let isStale = await withLog (
18
+ const isStale = await withLog (
17
19
'Checking if head is stale... ' ,
18
20
getIsStale ( ) ,
19
- isStale => isStale ? 'Stale' : 'Up-to date'
21
+ isStaleValue => isStaleValue ? 'Stale' : 'Up-to date'
20
22
)
21
23
if ( ! isStale && ! process . argv . includes ( '--force' ) ) {
22
24
console . log ( 'Exiting, use --force to build anyway' ) ;
23
25
return ;
24
26
}
25
- let files = await withLog ( 'Fetching files from upstream... ' , getFiles ( ) ) ;
27
+ const files = await withLog ( 'Fetching files from upstream... ' , getFiles ( ) ) ;
26
28
await withLog ( 'Writing files... ' , writeFiles ( files ) ) ;
27
29
await withLog ( 'Updating head.json... ' , updateHead ( ) ) ;
28
30
await withLog ( 'Generating parser...\n' , writeParser ( ) ) ;
@@ -32,8 +34,8 @@ const main = async () => {
32
34
}
33
35
34
36
const 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 ] ) ;
37
39
}
38
40
39
41
const getHead = async ( ) =>
@@ -45,24 +47,24 @@ let upstreamHeadCache: { [file: string]: string } | undefined;
45
47
const getUpstreamHead = async ( ) => {
46
48
if ( upstreamHeadCache ) return upstreamHeadCache ;
47
49
48
- let upstreamHead = mergeAll (
50
+ const upstreamHead = mergeAll (
49
51
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 ( ) ;
53
55
return { [ file ] : commits [ 0 ] . sha as string } ;
54
56
} )
55
57
)
56
58
)
57
59
upstreamHeadCache = upstreamHead ;
58
60
return upstreamHead ;
59
61
}
60
-
62
+
61
63
const getFiles = async ( ) =>
62
64
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 ( ) ;
66
68
return { [ file ] : data } ;
67
69
} )
68
70
) )
@@ -85,13 +87,13 @@ const writeParser = () =>
85
87
execCommand ( `${ prependBinDir ( 'antlr4ts' ) } -visitor -o src/parser -Xexact-output-dir src/parser/JavaLexer.g4 src/parser/JavaParser.g4` )
86
88
87
89
const 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' ) ;
89
91
90
- let exportList =
92
+ const exportList =
91
93
listenerSource
92
94
. split ( EOL )
93
95
. 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 * \} .* / ) ;
95
97
if ( matches === null ) return null ;
96
98
return matches [ 1 ] ;
97
99
} )
@@ -105,7 +107,7 @@ const writeParserContexts = async () => {
105
107
}
106
108
107
109
const writeJavascript = async ( ) => {
108
- await promisify ( rimraf ) ( path . join ( __dirname , " /dist" ) )
110
+ await promisify ( rimraf ) ( path . join ( __dirname , ' /dist' ) )
109
111
await execCommand ( prependBinDir ( 'tsc' ) )
110
112
}
111
113
@@ -116,7 +118,7 @@ const withLog = async <T>(
116
118
) => {
117
119
process . stdout . write ( label ) ;
118
120
try {
119
- let value = await promise ;
121
+ const value = await promise ;
120
122
process . stdout . write ( fulfilMessage ( value ) + '\n' )
121
123
return value ;
122
124
} catch ( error ) {
@@ -126,21 +128,21 @@ const withLog = async <T>(
126
128
}
127
129
128
130
const execCommand = async ( command : string ) => {
129
- let childProcess = exec ( command , { cwd : __dirname } )
131
+ const childProcess = exec ( command , { cwd : __dirname } )
130
132
childProcess . stdout . pipe ( process . stdout ) ;
131
133
childProcess . stderr . pipe ( process . stderr ) ;
132
134
133
- let [ code ] = await once ( childProcess , 'exit' ) as [ number ] ;
135
+ const [ code ] = await once ( childProcess , 'exit' ) as [ number ] ;
134
136
if ( code !== 0 ) throw undefined ;
135
137
}
136
138
137
139
const prependBinDir = ( p : string ) =>
138
- path . join ( __dirname , " /node_modules/.bin/" , p ) ;
140
+ path . join ( __dirname , ' /node_modules/.bin/' , p ) ;
139
141
140
142
type MergeAll = < T extends object [ ] > ( xs : T ) => UnionToIntersection < T [ number ] > ;
141
143
const mergeAll : MergeAll = xs => xs . reduce ( ( m , x ) => ( { ...m , ...x } ) , { } ) as any ;
142
-
143
- type UnionToIntersection < U > =
144
+
145
+ type UnionToIntersection < U > =
144
146
( U extends any ? ( k : U ) => void : never ) extends ( ( k : infer I ) => void ) ? I : never
145
147
146
148
main ( ) ;
0 commit comments