109 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
		
		
			
		
	
	
			109 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
|  | #!/usr/bin/env node
 | ||
|  | 
 | ||
|  | /** | ||
|  |  * SZCI npm wrapper | ||
|  |  * This script executes the appropriate pre-compiled binary based on the current platform | ||
|  |  */ | ||
|  | 
 | ||
|  | import { spawn } from 'child_process'; | ||
|  | import { fileURLToPath } from 'url'; | ||
|  | import { dirname, join } from 'path'; | ||
|  | import { existsSync } from 'fs'; | ||
|  | import { platform, arch } from 'os'; | ||
|  | 
 | ||
|  | const __filename = fileURLToPath(import.meta.url); | ||
|  | const __dirname = dirname(__filename); | ||
|  | 
 | ||
|  | /** | ||
|  |  * Get the binary name for the current platform | ||
|  |  */ | ||
|  | function getBinaryName() { | ||
|  |   const plat = platform(); | ||
|  |   const architecture = arch(); | ||
|  | 
 | ||
|  |   // Map Node's platform/arch to our binary naming
 | ||
|  |   const platformMap = { | ||
|  |     'darwin': 'macos', | ||
|  |     'linux': 'linux', | ||
|  |     'win32': 'windows' | ||
|  |   }; | ||
|  | 
 | ||
|  |   const archMap = { | ||
|  |     'x64': 'x64', | ||
|  |     'arm64': 'arm64' | ||
|  |   }; | ||
|  | 
 | ||
|  |   const mappedPlatform = platformMap[plat]; | ||
|  |   const mappedArch = archMap[architecture]; | ||
|  | 
 | ||
|  |   if (!mappedPlatform || !mappedArch) { | ||
|  |     console.error(`Error: Unsupported platform/architecture: ${plat}/${architecture}`); | ||
|  |     console.error('Supported platforms: Linux, macOS, Windows'); | ||
|  |     console.error('Supported architectures: x64, arm64'); | ||
|  |     process.exit(1); | ||
|  |   } | ||
|  | 
 | ||
|  |   // Construct binary name
 | ||
|  |   let binaryName = `szci-${mappedPlatform}-${mappedArch}`; | ||
|  |   if (plat === 'win32') { | ||
|  |     binaryName += '.exe'; | ||
|  |   } | ||
|  | 
 | ||
|  |   return binaryName; | ||
|  | } | ||
|  | 
 | ||
|  | /** | ||
|  |  * Execute the binary | ||
|  |  */ | ||
|  | function executeBinary() { | ||
|  |   const binaryName = getBinaryName(); | ||
|  |   const binaryPath = join(__dirname, '..', 'dist', 'binaries', binaryName); | ||
|  | 
 | ||
|  |   // Check if binary exists
 | ||
|  |   if (!existsSync(binaryPath)) { | ||
|  |     console.error(`Error: Binary not found at ${binaryPath}`); | ||
|  |     console.error('This might happen if:'); | ||
|  |     console.error('1. The postinstall script failed to run'); | ||
|  |     console.error('2. The platform is not supported'); | ||
|  |     console.error('3. The package was not installed correctly'); | ||
|  |     console.error(''); | ||
|  |     console.error('Try reinstalling the package:'); | ||
|  |     console.error('  npm uninstall -g @ship.zone/szci'); | ||
|  |     console.error('  npm install -g @ship.zone/szci'); | ||
|  |     process.exit(1); | ||
|  |   } | ||
|  | 
 | ||
|  |   // Spawn the binary with all arguments passed through
 | ||
|  |   const child = spawn(binaryPath, process.argv.slice(2), { | ||
|  |     stdio: 'inherit', | ||
|  |     shell: false | ||
|  |   }); | ||
|  | 
 | ||
|  |   // Handle child process events
 | ||
|  |   child.on('error', (err) => { | ||
|  |     console.error(`Error executing szci: ${err.message}`); | ||
|  |     process.exit(1); | ||
|  |   }); | ||
|  | 
 | ||
|  |   child.on('exit', (code, signal) => { | ||
|  |     if (signal) { | ||
|  |       process.kill(process.pid, signal); | ||
|  |     } else { | ||
|  |       process.exit(code || 0); | ||
|  |     } | ||
|  |   }); | ||
|  | 
 | ||
|  |   // Forward signals to child process
 | ||
|  |   const signals = ['SIGINT', 'SIGTERM', 'SIGHUP']; | ||
|  |   signals.forEach(signal => { | ||
|  |     process.on(signal, () => { | ||
|  |       if (!child.killed) { | ||
|  |         child.kill(signal); | ||
|  |       } | ||
|  |     }); | ||
|  |   }); | ||
|  | } | ||
|  | 
 | ||
|  | // Execute
 | ||
|  | executeBinary(); |