@@ -6,10 +6,13 @@ import path from "node:path";
66import util from "node:util" ;
77import packageInfo from "./package.json" ;
88
9+ class ParserError {
10+ constructor ( public message : string , public value : any ) { }
11+ }
12+
913type ParserName = keyof typeof packageInfo . devDependencies ;
1014const exec = util . promisify ( require ( "child_process" ) . exec ) ;
1115const outDir = path . join ( __dirname , "out" ) ;
12- let hasErrors = false ;
1316
1417function getPackagePath ( name : string ) {
1518 try {
@@ -25,9 +28,7 @@ async function gitCloneOverload(name: ParserName) {
2528 const match = value . match ( / ^ g i t h u b : ( \S + ) # ( \S + ) $ / ) ;
2629
2730 if ( match == null ) {
28- console . error ( `❗ Failed to parse git repo for ${ name } :\n` , value ) ;
29- hasErrors = true ;
30- return ;
31+ throw new ParserError ( `❗ Failed to parse git repo for ${ name } ` , value ) ;
3132 }
3233
3334 try {
@@ -41,8 +42,7 @@ async function gitCloneOverload(name: ParserName) {
4142 process . chdir ( packagePath ) ;
4243 await exec ( `git reset --hard ${ commitHash } ` ) ;
4344 } catch ( e ) {
44- console . error ( `❗Failed to clone git repo for ${ name } :\n` , e ) ;
45- hasErrors = true ;
45+ throw new ParserError ( `❗Failed to clone git repo for ${ name } ` , e ) ;
4646 }
4747}
4848
@@ -63,18 +63,14 @@ async function buildParserWASM(
6363 const cwd = subPath ? path . join ( packagePath , subPath ) : packagePath ;
6464
6565 if ( ! fs . existsSync ( cwd ) ) {
66- console . error ( `❗ Failed to find cwd ${ label } :\n` , cwd ) ;
67- hasErrors = true ;
68- return ;
66+ throw new ParserError ( `❗ Failed to find cwd ${ label } ` , cwd ) ;
6967 }
7068
7169 if ( generate ) {
7270 try {
7371 await exec ( generateCommand , { cwd } ) ;
7472 } catch ( e ) {
75- console . error ( `❗ Failed to generate ${ label } :\n` , e ) ;
76- hasErrors = true ;
77- return ;
73+ throw new ParserError ( `❗ Failed to generate ${ label } ` , e ) ;
7874 }
7975 }
8076
@@ -83,12 +79,11 @@ async function buildParserWASM(
8379 await exec ( `mv *.wasm ${ outDir } ` , { cwd } ) ;
8480 console . log ( `✅ Finished building ${ label } ` ) ;
8581 } catch ( e ) {
86- console . error ( `❗ Failed to build ${ label } :\n` , e ) ;
87- hasErrors = true ;
82+ throw new ParserError ( `❗ Failed to build ${ label } ` , e ) ;
8883 }
8984}
9085
91- function buildParserWASMS ( ) {
86+ async function buildParserWASMS ( ) {
9287 const grammars = Object . keys ( packageInfo . devDependencies ) . filter (
9388 ( n ) =>
9489 ( n . startsWith ( "tree-sitter-" ) &&
@@ -97,57 +92,70 @@ function buildParserWASMS() {
9792 n === "@elm-tooling/tree-sitter-elm"
9893 ) as ParserName [ ] ;
9994
100- return PromisePool . withConcurrency ( os . cpus ( ) . length )
95+ let hasErrors = false ;
96+
97+ await PromisePool . withConcurrency ( os . cpus ( ) . length )
10198 . for ( grammars )
10299 . process ( async ( name : ParserName ) => {
103- switch ( name ) {
104- case "tree-sitter-php" :
105- await buildParserWASM ( name , { subPath : "php" } ) ;
106- break ;
107-
108- case "tree-sitter-typescript" :
109- await buildParserWASM ( name , { subPath : "typescript" } ) ;
110- await buildParserWASM ( name , { subPath : "tsx" } ) ;
111- break ;
112-
113- case "tree-sitter-xml" :
114- await buildParserWASM ( name , { subPath : "xml" } ) ;
115- await buildParserWASM ( name , { subPath : "dtd" } ) ;
116- break ;
117-
118- case "tree-sitter-markdown" :
119- await gitCloneOverload ( name ) ;
120- await buildParserWASM ( name , {
121- subPath : "tree-sitter-markdown" ,
122- } ) ;
123- await buildParserWASM ( name , {
124- subPath : "tree-sitter-markdown-inline" ,
125- } ) ;
126- break ;
127-
128- case "tree-sitter-elixir" :
129- case "tree-sitter-perl" :
130- case "tree-sitter-query" :
131- await gitCloneOverload ( name ) ;
132- await buildParserWASM ( name , { generate : true } ) ;
133- break ;
134-
135- case "tree-sitter-latex" :
136- case "tree-sitter-swift" :
137- await buildParserWASM ( name , { generate : true } ) ;
138- break ;
139-
140- default :
141- await buildParserWASM ( name ) ;
100+ try {
101+ switch ( name ) {
102+ case "tree-sitter-php" :
103+ await buildParserWASM ( name , { subPath : "php" } ) ;
104+ break ;
105+
106+ case "tree-sitter-typescript" :
107+ await buildParserWASM ( name , { subPath : "typescript" } ) ;
108+ await buildParserWASM ( name , { subPath : "tsx" } ) ;
109+ break ;
110+
111+ case "tree-sitter-xml" :
112+ await buildParserWASM ( name , { subPath : "xml" } ) ;
113+ await buildParserWASM ( name , { subPath : "dtd" } ) ;
114+ break ;
115+
116+ case "tree-sitter-markdown" :
117+ await gitCloneOverload ( name ) ;
118+ await buildParserWASM ( name , {
119+ subPath : "tree-sitter-markdown" ,
120+ } ) ;
121+ await buildParserWASM ( name , {
122+ subPath : "tree-sitter-markdown-inline" ,
123+ } ) ;
124+ break ;
125+
126+ case "tree-sitter-elixir" :
127+ case "tree-sitter-perl" :
128+ case "tree-sitter-query" :
129+ // await gitCloneOverload(name);
130+ await buildParserWASM ( name , { generate : true } ) ;
131+ break ;
132+
133+ case "tree-sitter-latex" :
134+ case "tree-sitter-swift" :
135+ await buildParserWASM ( name , { generate : true } ) ;
136+ break ;
137+
138+ default :
139+ await buildParserWASM ( name ) ;
140+ }
141+ } catch ( e ) {
142+ if ( e instanceof ParserError ) {
143+ console . error ( e . message + ":\n" , e . value ) ;
144+ } else {
145+ console . error ( e ) ;
146+ }
147+ hasErrors = true ;
142148 }
143149 } ) ;
150+
151+ if ( hasErrors ) {
152+ throw new Error ( "Failed to build some parsers" ) ;
153+ }
144154}
145155
146156fs . mkdirSync ( outDir ) ;
147157process . chdir ( outDir ) ;
148158
149- buildParserWASMS ( ) . then ( ( ) => {
150- if ( hasErrors ) {
151- process . exit ( 1 ) ;
152- }
159+ buildParserWASMS ( ) . catch ( ( ) => {
160+ process . exit ( 1 ) ;
153161} ) ;
0 commit comments