1
+ const fs = require ( 'fs' ) . promises ;
2
+ const path = require ( 'path' ) ;
3
+ const os = require ( 'os' ) ;
4
+ const { exec } = require ( 'child_process' ) ;
5
+ const util = require ( 'util' ) ;
6
+ const execAsync = util . promisify ( exec ) ;
7
+
8
+ const SKIP_DIRS = [ 'node_modules' , '.git' , 'dist' , 'build' ] ;
9
+
10
+ async function processFiles ( dir , platform , arch ) {
11
+ const entries = await fs . readdir ( dir , { withFileTypes : true } ) ;
12
+
13
+ for ( const entry of entries ) {
14
+ const fullPath = path . join ( dir , entry . name ) ;
15
+
16
+ if ( entry . isDirectory ( ) && ! SKIP_DIRS . includes ( entry . name ) ) {
17
+ await processFiles ( fullPath , platform , arch ) ;
18
+ } else if ( entry . isFile ( ) && entry . name . endsWith ( '.json' ) ) {
19
+ let content = await fs . readFile ( fullPath , 'utf8' ) ;
20
+
21
+ if ( content . includes ( 'linux-x64' ) ) {
22
+ const replacement = `${ platform } -${ arch } ` ;
23
+ content = content . replace ( / l i n u x - x 6 4 / g, replacement ) ;
24
+ await fs . writeFile ( fullPath , content , 'utf8' ) ;
25
+ }
26
+ }
27
+ }
28
+ }
29
+
30
+ const targetDir = path . join ( __dirname , './standalone' ) ;
31
+
32
+ function checkOS ( ) {
33
+ const platform = os . platform ( ) ;
34
+ const arch = os . arch ( ) ;
35
+ return { platform, arch } ;
36
+ }
37
+
38
+ async function copyModules ( destPath ) {
39
+ const isWindows = os . platform ( ) === 'win32' ;
40
+
41
+ try {
42
+ if ( isWindows ) {
43
+ await execAsync ( `copy node_modules/.pnpm/[email protected] ${ destPath } \\ && copy node_modules/.pnpm/@esbuild+* ${ destPath } \\` ) ;
44
+ } else {
45
+ await execAsync ( `cp -r node_modules/.pnpm/[email protected] node_modules/.pnpm/@esbuild+* ${ destPath } /` ) ;
46
+ }
47
+ } catch ( error ) {
48
+ console . error ( 'Error copying modules:' , error . message ) ;
49
+ throw error ;
50
+ }
51
+ }
52
+
53
+ async function checkImport ( ) {
54
+ try {
55
+ const pnpmDir = path . resolve ( 'node_modules/.pnpm' ) ;
56
+ const entries = await fs . readdir ( pnpmDir ) ;
57
+
58
+ const esbuildPackage = entries . find ( entry => entry . startsWith ( '@esbuild+' ) ) ;
59
+ if ( ! esbuildPackage ) return null ;
60
+
61
+ const match = esbuildPackage . match ( / @ e s b u i l d \+ ( .+ ) @ 0 \. 2 5 \. 0 / ) ;
62
+ return match ? match [ 1 ] : null ;
63
+ } catch ( error ) {
64
+ console . error ( 'Error checking imports:' , error . message ) ;
65
+ return null ;
66
+ }
67
+ }
68
+
69
+ async function main ( ) {
70
+ try {
71
+ const { platform, arch } = checkOS ( ) ;
72
+ console . log ( `Replacing 'linux-x64' with '${ platform } -${ arch } ' in .json files in ${ targetDir } ` ) ;
73
+ await processFiles ( targetDir , platform , arch ) ;
74
+
75
+ const importName = await checkImport ( ) ;
76
+ if ( importName ) {
77
+ console . log ( `Found esbuild import: ${ importName } ` ) ;
78
+
79
+ const destPath = path . resolve ( './standalone/node_modules/.pnpm' ) ;
80
+ await copyModules ( destPath ) ;
81
+ console . log ( `Modules copied to ${ destPath } ` ) ;
82
+ }
83
+ } catch ( error ) {
84
+ console . error ( 'Error in main process:' , error . message ) ;
85
+ process . exit ( 1 ) ;
86
+ }
87
+ }
88
+
89
+ void main ( ) ;
0 commit comments