c2patool-npm
Version:
NPM wrapper for c2patool executable
123 lines (104 loc) • 3.15 kB
JavaScript
const fs = require('fs');
const path = require('path');
const os = require('os');
const { spawnSync } = require('child_process');
const readline = require('readline');
// Create readline interface for user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Determine the platform and architecture
const platform = os.platform();
const arch = os.arch();
// Map platform and architecture to the correct binary path
function getBinaryInfo() {
let binaryName = 'c2patool';
// Add .exe extension for Windows
if (platform === 'win32') {
binaryName += '.exe';
}
// Map architecture
let archName;
if (arch === 'x64') {
archName = 'x64';
} else if (arch === 'arm64') {
archName = 'arm64';
} else {
throw new Error(`Unsupported architecture: ${arch}`);
}
// Map platform
let platformName;
if (platform === 'darwin') {
platformName = 'darwin';
} else if (platform === 'linux') {
platformName = 'linux';
} else if (platform === 'win32') {
platformName = 'win32';
} else {
throw new Error(`Unsupported platform: ${platform}`);
}
return {
binaryName,
platformName,
archName,
vendorPath: path.join(__dirname, 'vendor', `${platformName}-${archName}`, binaryName)
};
}
// Copy the binary to the vendor directory
function copyBinary(sourcePath, targetPath) {
try {
// Create the directory if it doesn't exist
const targetDir = path.dirname(targetPath);
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}
// Copy the binary
fs.copyFileSync(sourcePath, targetPath);
console.log(`Copied binary from ${sourcePath} to ${targetPath}`);
// Make the binary executable (not needed on Windows)
if (platform !== 'win32') {
fs.chmodSync(targetPath, 0o755);
console.log(`Made binary executable: ${targetPath}`);
}
return true;
} catch (err) {
console.error(`Failed to copy binary: ${err.message}`);
return false;
}
}
// Main setup function
function setup() {
try {
const { binaryName, platformName, archName, vendorPath } = getBinaryInfo();
console.log(`Setting up c2patool for ${platformName}-${archName}`);
console.log(`Target path: ${vendorPath}`);
// Ask for the path to the c2patool binary
rl.question(`Please enter the path to your ${binaryName} binary: `, (sourcePath) => {
if (!sourcePath) {
console.error('No path provided. Setup aborted.');
rl.close();
return;
}
// Check if the source file exists
if (!fs.existsSync(sourcePath)) {
console.error(`File not found: ${sourcePath}`);
rl.close();
return;
}
// Copy the binary
if (copyBinary(sourcePath, vendorPath)) {
console.log('Setup completed successfully!');
} else {
console.error('Setup failed.');
}
rl.close();
});
} catch (err) {
console.error(`Error during setup: ${err.message}`);
rl.close();
}
}
// Run the setup
setup();