11const esbuild = require ( 'esbuild' ) ;
2- const peggy = require ( 'peggy' ) ;
32const fs = require ( 'node:fs' ) ;
43const path = require ( 'node:path' ) ;
54
6- const CLI_FLAGS = new Set ( process . argv . slice ( 2 ) ) ;
7- const SHOULD_BUILD_BUNDLE = ! CLI_FLAGS . has ( '--parsers-only' ) ;
8- const PEGGY_IMPORT_SOURCE_RE =
9- / ( f r o m \s + [ ' " ] ) \. \/ p e g g y \/ (? ! g e n e r a t e d \/ ) ( [ ^ ' " ] + ) ( [ ' " ] ) / g;
10- const HELP_TEXT = `Usage: node scripts/build.js [--parsers-only]
11-
12- --parsers-only Only generate pre-compiled Peggy parsers.
13- Without --parsers-only, build the Node bundle from .build/src/node.js.` ;
14-
15- if ( CLI_FLAGS . has ( '--help' ) ) {
16- console . log ( HELP_TEXT ) ;
17- process . exit ( 0 ) ;
18- }
19-
20- const PATHS = createPaths ( ) ;
21-
22- function createPaths ( ) {
23- const rootDir = path . resolve ( __dirname , '..' ) ;
24- const buildDir = path . join ( rootDir , '.build' ) ;
25- const buildSrcDir = path . join ( buildDir , 'src' ) ;
26- const buildParsersDir = path . join ( buildSrcDir , 'core/proxy-utils/parsers' ) ;
27- const buildPeggyDir = path . join ( buildParsersDir , 'peggy' ) ;
28-
29- return {
30- rootDir,
31- srcDir : path . join ( rootDir , 'src' ) ,
32- rootTsconfigPath : path . join ( rootDir , 'jsconfig.json' ) ,
33- buildDir,
34- buildSrcDir,
35- buildTsconfigPath : path . join ( buildDir , 'jsconfig.json' ) ,
36- buildPeggyDir,
37- buildGeneratedDir : path . join ( buildPeggyDir , 'generated' ) ,
38- buildParsersIndexPath : path . join ( buildParsersDir , 'index.js' ) ,
39- nodeEntryPath : path . join ( buildSrcDir , 'node.js' ) ,
40- nodeOutputPath : path . join ( rootDir , 'dist/minisubconvert.js' ) ,
41- } ;
42- }
43-
44- function ensureDir ( dirPath ) {
45- fs . mkdirSync ( dirPath , { recursive : true } ) ;
46- }
47-
48- function prepareBuildWorkspace ( ) {
49- fs . rmSync ( PATHS . buildDir , { recursive : true , force : true } ) ;
50- ensureDir ( PATHS . buildDir ) ;
51- fs . cpSync ( PATHS . srcDir , PATHS . buildSrcDir , { recursive : true } ) ;
52- fs . copyFileSync ( PATHS . rootTsconfigPath , PATHS . buildTsconfigPath ) ;
53- }
54-
55- function getPeggyGrammarFiles ( ) {
56- const grammarFiles = fs
57- . readdirSync ( PATHS . buildPeggyDir )
58- . filter ( ( fileName ) => fileName . endsWith ( '.peg' ) )
59- . sort ( ) ;
60-
61- if ( grammarFiles . length === 0 ) {
62- throw new Error ( `No .peg files found in ${ PATHS . buildPeggyDir } ` ) ;
63- }
64-
65- return grammarFiles ;
66- }
67-
68- function createParserModuleCode ( pegFileName , parserSource ) {
69- return [
70- `// Auto-generated from ${ pegFileName } - DO NOT EDIT` ,
71- parserSource ,
72- '' ,
73- 'let cachedParser = null;' ,
74- 'export default function getParser() {' ,
75- ' if (!cachedParser) {' ,
76- ' cachedParser = peg$parse;' ,
77- ' cachedParser.parse = peg$parse;' ,
78- ' }' ,
79- ' return cachedParser;' ,
80- '}' ,
81- '' ,
82- ] . join ( '\n' ) ;
83- }
84-
85- function compilePeggyParser ( pegFileName ) {
86- const grammarPath = path . join ( PATHS . buildPeggyDir , pegFileName ) ;
87- const outputPath = path . join (
88- PATHS . buildGeneratedDir ,
89- `${ path . parse ( pegFileName ) . name } .js` ,
90- ) ;
91- const parserSource = peggy . generate ( fs . readFileSync ( grammarPath , 'utf-8' ) , {
92- output : 'source' ,
93- format : 'es' ,
94- } ) ;
95-
96- fs . writeFileSync (
97- outputPath ,
98- createParserModuleCode ( pegFileName , parserSource ) ,
99- 'utf-8' ,
100- ) ;
101- console . log ( ` Generated: ${ path . relative ( PATHS . rootDir , outputPath ) } ` ) ;
102- }
103-
104- function compilePeggyParsers ( ) {
105- prepareBuildWorkspace ( ) ;
106- ensureDir ( PATHS . buildGeneratedDir ) ;
107-
108- const grammarFiles = getPeggyGrammarFiles ( ) ;
109-
110- console . log ( 'Pre-compiling Peggy grammars...' ) ;
111-
112- for ( const pegFileName of grammarFiles ) {
113- compilePeggyParser ( pegFileName ) ;
114- }
115-
116- rewriteParserIndexImports ( ) ;
117- console . log ( `Generated ${ grammarFiles . length } parser modules.` ) ;
118- }
119-
120- function rewriteParserIndexImports ( ) {
121- const source = fs . readFileSync ( PATHS . buildParsersIndexPath , 'utf-8' ) ;
122- const rewritten = source . replace (
123- PEGGY_IMPORT_SOURCE_RE ,
124- '$1./peggy/generated/$2$3' ,
125- ) ;
126-
127- if ( rewritten !== source ) {
128- fs . writeFileSync ( PATHS . buildParsersIndexPath , rewritten , 'utf-8' ) ;
129- console . log (
130- ` Rewired: ${ path . relative ( PATHS . rootDir , PATHS . buildParsersIndexPath ) } ` ,
131- ) ;
132- }
133- }
5+ const rootDir = path . resolve ( __dirname , '..' ) ;
6+ const PATHS = {
7+ rootDir,
8+ srcDir : path . join ( rootDir , 'src' ) ,
9+ tsconfigPath : path . join ( rootDir , 'jsconfig.json' ) ,
10+ nodeEntryPath : path . join ( rootDir , 'src/node.js' ) ,
11+ nodeOutputPath : path . join ( rootDir , 'dist/minisubconvert.js' ) ,
12+ } ;
13413
13514async function buildNodeBundle ( ) {
136- ensureDir ( path . dirname ( PATHS . nodeOutputPath ) ) ;
15+ fs . mkdirSync ( path . dirname ( PATHS . nodeOutputPath ) , { recursive : true } ) ;
13716
13817 await esbuild . build ( {
13918 entryPoints : [ PATHS . nodeEntryPath ] ,
@@ -147,7 +26,7 @@ async function buildNodeBundle() {
14726 banner : {
14827 js : '#!/usr/bin/env node' ,
14928 } ,
150- tsconfig : PATHS . buildTsconfigPath ,
29+ tsconfig : PATHS . tsconfigPath ,
15130 logLevel : 'info' ,
15231 } ) ;
15332
@@ -158,12 +37,7 @@ async function buildNodeBundle() {
15837
15938async function main ( ) {
16039 try {
161- compilePeggyParsers ( ) ;
162-
163- if ( SHOULD_BUILD_BUNDLE ) {
164- await buildNodeBundle ( ) ;
165- }
166-
40+ await buildNodeBundle ( ) ;
16741 console . log ( 'Build complete.' ) ;
16842 } catch ( error ) {
16943 console . error ( 'Build failed:' , error ) ;
0 commit comments