@theaiinc/torch
Version:
Standalone CLI for the Coding Agent
116 lines (104 loc) • 3.54 kB
JavaScript
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const platform = process.platform;
const arch = process.arch;
const version = require('../package.json').version;
const binaryName = 'torch';
const packageName = `@theaiinc/torch-${platform}-${arch}`;
console.log(`Post-install: Looking for binary package: ${packageName}`);
// First, try to find the package in node_modules
const possiblePaths = [
path.join(__dirname, '..', 'node_modules', packageName, 'bin', binaryName),
path.join(
__dirname,
'..',
'..',
'node_modules',
packageName,
'bin',
binaryName
),
path.join(
__dirname,
'..',
'..',
'..',
'node_modules',
packageName,
'bin',
binaryName
),
];
console.log('Checking possible binary paths:');
possiblePaths.forEach(p => console.log(` - ${p}`));
let binaryPath = null;
for (const testPath of possiblePaths) {
if (fs.existsSync(testPath)) {
binaryPath = testPath;
console.log(`Found binary at: ${binaryPath}`);
break;
}
}
if (!binaryPath) {
// Try require.resolve as fallback
try {
binaryPath = require.resolve(`${packageName}/bin/${binaryName}`);
console.log(`Found binary via require.resolve: ${binaryPath}`);
} catch (e) {
// Try to install the platform package globally if not found
try {
console.warn(
`Platform package ${packageName} not found. Attempting to install globally...`
);
execSync(`npm install -g ${packageName}@${version}`, {
stdio: 'inherit',
});
// Try again to resolve the binary
binaryPath = require.resolve(`${packageName}/bin/${binaryName}`);
console.log(`Found binary after global install: ${binaryPath}`);
} catch (installErr) {
console.error(
`❌ Error: Optional dependency ${packageName} not found and could not be installed.`
);
console.error(
`This means a pre-compiled binary for your platform (${platform}-${arch}) is not available.`
);
console.error(`Debug info:`);
console.error(` - Platform: ${platform}`);
console.error(` - Architecture: ${arch}`);
console.error(` - Package name: ${packageName}`);
console.error(` - Current directory: ${process.cwd()}`);
console.error(` - Script directory: ${__dirname}`);
// List node_modules contents for debugging
const nodeModulesPath = path.join(__dirname, '..', 'node_modules');
if (fs.existsSync(nodeModulesPath)) {
console.error(` - node_modules contents:`);
try {
const contents = fs.readdirSync(nodeModulesPath);
contents.forEach(item => console.error(` - ${item}`));
} catch (e) {
console.error(` - Error reading node_modules: ${e.message}`);
}
}
// In a CI or global install context, this should be a hard failure.
if (process.env.CI || process.env.npm_config_global) {
process.exit(1);
} else {
console.warn(
'Proceeding with installation, but the CLI will not be executable.'
);
process.exit(0);
}
}
}
}
// Copy the binary to the expected location
const destDir = path.join(__dirname, '..', 'binaries');
const destPath = path.join(destDir, binaryName);
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true });
}
fs.copyFileSync(binaryPath, destPath);
fs.chmodSync(destPath, '755');
console.log(`✅ Binary successfully copied to ${destPath}`);