c2patool-npm
Version:
NPM wrapper for c2patool executable
83 lines (71 loc) • 2.1 kB
JavaScript
const fs = require('fs');
const path = require('path');
const os = require('os');
const { spawnSync } = require('child_process');
// 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)
};
}
// Make the binary executable (not needed on Windows)
function ensureExecutable(binaryPath) {
if (platform !== 'win32') {
try {
fs.chmodSync(binaryPath, 0o755);
console.log(`Made binary executable: ${binaryPath}`);
} catch (err) {
console.error(`Failed to make binary executable: ${err.message}`);
}
}
}
// Check if the binary exists and is executable
function checkBinary() {
try {
const { vendorPath } = getBinaryInfo();
if (!fs.existsSync(vendorPath)) {
console.error(`Binary not found at ${vendorPath}`);
console.error('Please make sure to place the appropriate c2patool binary in the vendor directory.');
process.exit(1);
}
ensureExecutable(vendorPath);
console.log('c2patool binary is ready to use.');
} catch (err) {
console.error(`Error during installation: ${err.message}`);
process.exit(1);
}
}
// Run the installation check
checkBinary();