33const fs = require ( 'fs' ) ;
44const path = require ( 'path' ) ;
55
6- // Version configurations
7- const VERSION_CONFIGS = {
8- '13' : {
9- libpgQueryTag : '13-2.2.0' ,
10- useEmscriptenPatch : true
11- } ,
12- '14' : {
13- libpgQueryTag : '14-3.0.0' ,
14- useEmscriptenPatch : false
15- } ,
16- '15' : {
17- libpgQueryTag : '15-4.2.4' ,
18- useEmscriptenPatch : false
19- } ,
20- '16' : {
21- libpgQueryTag : '16-5.2.0' ,
22- useEmscriptenPatch : false
23- } ,
24- '17' : {
25- libpgQueryTag : '17-6.1.0' ,
26- useEmscriptenPatch : false
27- }
28- } ;
29-
30- // Headers for different file types
31- const HEADERS = {
32- // JavaScript/TypeScript/C style comment
33- default : `/**
6+ const HEADER = `/**
347 * DO NOT MODIFY MANUALLY β this is generated from the templates dir
358 *
369 * To make changes, edit the files in the templates/ directory and run:
3710 * npm run copy:templates
3811 */
3912
40- ` ,
41- // Makefile style comment
42- makefile : `# DO NOT MODIFY MANUALLY β this is generated from the templates dir
13+ ` ;
14+
15+ const MAKEFILE_HEADER = `# DO NOT MODIFY MANUALLY β this is generated from the templates dir
4316#
4417# To make changes, edit the files in the templates/ directory and run:
4518# npm run copy:templates
4619
47- `
48- } ;
49-
50- // File extensions that should get headers
51- const HEADER_EXTENSIONS = [ '.ts' , '.js' , '.c' ] ;
52- const MAKEFILE_NAMES = [ 'Makefile' , 'makefile' ] ;
20+ ` ;
5321
54- /**
55- * Process template content with simple mustache-like syntax
56- * @param {string } content - Template content
57- * @param {object } config - Configuration object
58- * @returns {string } Processed content
59- */
60- function processTemplate ( content , config ) {
61- // Replace simple variables
62- content = content . replace ( / \{ \{ L I B P G _ Q U E R Y _ T A G \} \} / g, config . libpgQueryTag ) ;
63-
64- // Handle conditional blocks
65- // {{#USE_EMSCRIPTEN_PATCH}}...{{/USE_EMSCRIPTEN_PATCH}}
66- const conditionalRegex = / \{ \{ # ( \w + ) \} \} ( [ \s \S ] * ?) \{ \{ \/ \1\} \} / g;
67-
68- content = content . replace ( conditionalRegex , ( match , flag , blockContent ) => {
69- if ( flag === 'USE_EMSCRIPTEN_PATCH' && config . useEmscriptenPatch ) {
70- return blockContent ;
71- }
72- return '' ;
73- } ) ;
74-
75- return content ;
76- }
77-
78- /**
79- * Add header to file content if applicable
80- * @param {string } filePath - Path to the file
81- * @param {string } content - File content
82- * @returns {string } Content with header if applicable
83- */
84- function addHeaderIfNeeded ( filePath , content ) {
85- const basename = path . basename ( filePath ) ;
86- const ext = path . extname ( filePath ) ;
87-
88- // Check if it's a Makefile
89- if ( MAKEFILE_NAMES . includes ( basename ) ) {
90- return HEADERS . makefile + content ;
91- }
92-
93- // Check if it's a source file that needs a header
94- if ( HEADER_EXTENSIONS . includes ( ext ) ) {
95- return HEADERS . default + content ;
22+ // Version-specific configurations
23+ const VERSION_CONFIGS = {
24+ '13' : {
25+ tag : '13-2.2.0' ,
26+ hasEmscriptenPatch : true
27+ } ,
28+ '14' : {
29+ tag : '14-3.0.0' ,
30+ hasEmscriptenPatch : false
31+ } ,
32+ '15' : {
33+ tag : '15-4.2.4' ,
34+ hasEmscriptenPatch : false
35+ } ,
36+ '16' : {
37+ tag : '16-5.2.0' ,
38+ hasEmscriptenPatch : false
39+ } ,
40+ '17' : {
41+ tag : '17-6.1.0' ,
42+ hasEmscriptenPatch : false
9643 }
97-
98- return content ;
99- }
44+ } ;
10045
101- /**
102- * Copy a file from template to destination with processing
103- * @param {string } templatePath - Source template path
104- * @param {string } destPath - Destination path
105- * @param {object } config - Version configuration
106- */
107- function copyTemplate ( templatePath , destPath , config ) {
108- const content = fs . readFileSync ( templatePath , 'utf8' ) ;
109- const processedContent = processTemplate ( content , config ) ;
110- const finalContent = addHeaderIfNeeded ( destPath , processedContent ) ;
111-
112- // Ensure destination directory exists
113- const destDir = path . dirname ( destPath ) ;
114- if ( ! fs . existsSync ( destDir ) ) {
115- fs . mkdirSync ( destDir , { recursive : true } ) ;
116- }
117-
118- fs . writeFileSync ( destPath , finalContent ) ;
119- }
46+ // Files to copy from templates
47+ const TEMPLATE_FILES = [
48+ { src : 'LICENSE' , dest : 'LICENSE' , header : false } ,
49+ { src : 'wasm_wrapper.c' , dest : 'src/wasm_wrapper.c' , header : HEADER } ,
50+ { src : 'libpg-query.d.ts' , dest : 'src/libpg-query.d.ts' , header : HEADER } ,
51+ { src : 'index.ts' , dest : 'src/index.ts' , header : HEADER }
52+ ] ;
12053
121- /**
122- * Copy all templates for a specific version
123- * @param {string } version - Version number
124- * @param {object } config - Version configuration
125- */
126- function copyTemplatesForVersion ( version , config ) {
54+ function copyTemplates ( ) {
12755 const templatesDir = path . join ( __dirname , '..' , 'templates' ) ;
128- const versionDir = path . join ( __dirname , '..' , 'versions' , version ) ;
56+ const versionsDir = path . join ( __dirname , '..' , 'versions' ) ;
12957
130- // Check if version directory exists
131- if ( ! fs . existsSync ( versionDir ) ) {
132- console . warn ( `Warning: Directory ${ versionDir } does not exist. Skipping...` ) ;
133- return ;
134- }
135-
136- // Files to copy
137- const filesToCopy = [
138- 'LICENSE' ,
139- 'Makefile' ,
140- 'src/index.ts' ,
141- 'src/libpg-query.d.ts' ,
142- 'src/wasm_wrapper.c'
143- ] ;
144-
145- filesToCopy . forEach ( file => {
146- const templatePath = path . join ( templatesDir , file ) ;
147- const destPath = path . join ( versionDir , file ) ;
58+ // Process each version
59+ for ( const [ version , config ] of Object . entries ( VERSION_CONFIGS ) ) {
60+ const versionDir = path . join ( versionsDir , version ) ;
61+ console . log ( `\nProcessing version ${ version } ...` ) ;
14862
149- if ( ! fs . existsSync ( templatePath ) ) {
150- console . error ( `Error: Template file ${ templatePath } does not exist!` ) ;
151- return ;
63+ // Copy template files
64+ for ( const file of TEMPLATE_FILES ) {
65+ const srcPath = path . join ( templatesDir , file . src ) ;
66+ const destPath = path . join ( versionDir , file . dest ) ;
67+
68+ // Ensure destination directory exists
69+ const destDir = path . dirname ( destPath ) ;
70+ if ( ! fs . existsSync ( destDir ) ) {
71+ fs . mkdirSync ( destDir , { recursive : true } ) ;
72+ }
73+
74+ // Read template content
75+ let content = fs . readFileSync ( srcPath , 'utf8' ) ;
76+
77+ // Add header if specified
78+ if ( file . header ) {
79+ content = file . header + content ;
80+ }
81+
82+ // Write to destination
83+ fs . writeFileSync ( destPath , content ) ;
84+ console . log ( ` β Copied ${ file . src } to ${ file . dest } ` ) ;
15285 }
15386
154- copyTemplate ( templatePath , destPath , config ) ;
155- } ) ;
156-
157- console . log ( `β Version ${ version } completed` ) ;
158- }
159-
160- /**
161- * Main function
162- */
163- function main ( ) {
164- console . log ( 'Copying template files to version directories...\n' ) ;
165-
166- // Process each version
167- Object . entries ( VERSION_CONFIGS ) . forEach ( ( [ version , config ] ) => {
168- console . log ( `Processing version ${ version } ...` ) ;
169- copyTemplatesForVersion ( version , config ) ;
170- } ) ;
87+ // Process Makefile template
88+ const makefileTemplate = fs . readFileSync ( path . join ( templatesDir , 'Makefile.template' ) , 'utf8' ) ;
89+ let makefileContent = makefileTemplate . replace ( / { { VERSION_ T A G } } / g, config . tag ) ;
90+
91+ // Handle the USE_EMSCRIPTEN_PATCH placeholder
92+ if ( config . hasEmscriptenPatch ) {
93+ // For version 13, keep the patch block (remove only the placeholders)
94+ makefileContent = makefileContent . replace (
95+ / { { # U S E _ E M S C R I P T E N _ P A T C H } } \n ? / g,
96+ ''
97+ ) ;
98+ makefileContent = makefileContent . replace (
99+ / { { \/ U S E _ E M S C R I P T E N _ P A T C H } } \n ? / g,
100+ ''
101+ ) ;
102+ } else {
103+ // For other versions, remove the entire block including placeholders
104+ makefileContent = makefileContent . replace (
105+ / { { # U S E _ E M S C R I P T E N _ P A T C H } } [ \s \S ] * ?{ { \/ U S E _ E M S C R I P T E N _ P A T C H } } \n ? / g,
106+ ''
107+ ) ;
108+ }
109+
110+ // Write Makefile with header
111+ fs . writeFileSync ( path . join ( versionDir , 'Makefile' ) , MAKEFILE_HEADER + makefileContent ) ;
112+ console . log ( ` β Generated Makefile with tag ${ config . tag } ` ) ;
113+ }
171114
172- console . log ( '\nAll versions processed successfully!' ) ;
173- }
174-
175- // Run if called directly
176- if ( require . main === module ) {
177- main ( ) ;
115+ console . log ( '\nβ
Template copying completed!' ) ;
178116}
179117
180- module . exports = { processTemplate, copyTemplatesForVersion } ;
118+ // Run the script
119+ copyTemplates ( ) ;
0 commit comments