1
1
#!/bin/env node
2
2
import { existsSync , write } from 'node:fs'
3
3
import { readFile , writeFile , mkdir , rm , cp } from 'node:fs/promises'
4
+ import { dirname } from 'node:path'
4
5
5
6
import { program } from 'commander' ;
6
7
import { randomUUID } from 'node:crypto' ;
@@ -13,20 +14,35 @@ const exec = util.promisify(_exec);
13
14
import fg from 'fast-glob'
14
15
15
16
async function copy_template ( path , subdir ) {
16
- const tests = await fg ( `./extras/${ path } /${ subdir } /*.js` ) ;
17
+ const files = await fg ( `./extras/${ path } /${ subdir } /*.js` ) ;
17
18
const prefix = `./${ subdir } /${ path } /${ subdir } /` . length
18
19
const suffix = ".js" . length
19
- for ( const test of tests ) {
20
- const name = test . substring ( prefix , test . length - suffix ) . replaceAll ( "[module]" , path )
21
- await writeFile ( `./${ subdir } /extras/${ name } .js` , ( ( await readFile ( test ) ) . toString ( ) . replaceAll ( '__MODULE__' , path ) ) )
20
+ for ( const file of files ) {
21
+ const name = file . substring ( prefix , file . length - suffix ) . replaceAll ( "[module]" , path )
22
+ await writeFile ( `./${ subdir } /extras/${ name } .js` , ( ( await readFile ( file ) ) . toString ( ) . replaceAll ( '__MODULE__' , path ) ) )
22
23
23
24
}
24
25
}
25
26
26
27
async function install ( path ) {
27
- await writeFile ( `./src/extras/${ path } .c` , ( ( await readFile ( `./extras/${ path } /src/[module].c` ) ) . toString ( ) . replaceAll ( '__MODULE__' , path ) ) )
28
- await writeFile ( `./src/js/extras/${ path } .js` , ( ( await readFile ( `./extras/${ path } /src/[module].js` ) ) . toString ( ) . replaceAll ( '__MODULE__' , path ) ) )
29
- await writeFile ( `./docs/types/extras/${ path } .d.ts` , ( ( await readFile ( `./extras/${ path } /src/[module].d.ts` ) ) . toString ( ) . replaceAll ( '__MODULE__' , path ) ) )
28
+ await mkdir ( `src/extras/${ path } ` , { errorOnExist : false } ) ;
29
+
30
+ //Copy over all files in src
31
+ {
32
+ const files = await fg ( `./extras/${ path } /src/**/*` ) ;
33
+ const prefix = `./extras/${ path } /src/` . length
34
+ for ( const file of files ) {
35
+ const name = file . substring ( prefix ) . replaceAll ( "[module]" , path )
36
+ const fullPath = `./src/extras/${ path } /${ name } `
37
+ await mkdir ( dirname ( fullPath ) , { errorOnExist : false , recursive :true } ) ;
38
+ await writeFile ( fullPath , ( ( await readFile ( file ) ) . toString ( ) . replaceAll ( '__MODULE__' , path ) ) )
39
+
40
+ }
41
+ }
42
+
43
+ //While js/ts files must be already reduced in a bundle by this point.
44
+ await writeFile ( `./src/js/extras/${ path } .js` , ( ( await readFile ( `./extras/${ path } /bundle/[module].js` ) ) . toString ( ) . replaceAll ( '__MODULE__' , path ) ) )
45
+ await writeFile ( `./docs/types/extras/${ path } .d.ts` , ( ( await readFile ( `./extras/${ path } /bundle/[module].d.ts` ) ) . toString ( ) . replaceAll ( '__MODULE__' , path ) ) )
30
46
31
47
await copy_template ( path , 'examples' )
32
48
await copy_template ( path , 'benchmarks' )
@@ -105,7 +121,7 @@ program.command('clone')
105
121
106
122
//Construct src/extras.bootstrap to initialize the extra modules
107
123
await writeFile ( './src/extras-bootstrap.c.frag' , Object . keys ( config ) . map ( x => `tjs__mod_${ x } _init(ctx, ns);` ) . join ( '\n' ) )
108
- await writeFile ( './src/extras-proto .c.frag' , Object . keys ( config ) . map ( x => `void tjs__mod_ ${ x } _init(JSContext *ctx, JSValue ns); ` ) . join ( '\n' ) )
124
+ await writeFile ( './src/extras-headers .c.frag' , Object . keys ( config ) . map ( x => `#include "./extras/ ${ x } /module.h" ` ) . join ( '\n' ) )
109
125
await writeFile ( './src/extras-bundles.c.frag' , Object . keys ( config ) . map ( x => `#include "bundles/c/extras/${ x } .c"` ) . join ( '\n' ) )
110
126
await writeFile ( './src/extras-entries.c.frag' , Object . keys ( config ) . map ( x => `{ "tjs:${ x } ", tjs__${ x } , tjs__${ x } _size},` ) . join ( '\n' ) )
111
127
0 commit comments