109 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			109 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
		
			Executable File
		
	
	
	
	
| #!/usr/bin/env node
 | |
| 
 | |
| /**
 | |
|  * MAILER 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 = `mailer-${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 @serve.zone/mailer');
 | |
|     console.error('  npm install -g @serve.zone/mailer');
 | |
|     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 mailer: ${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();
 |