import fs from 'fs-extra';
import path from 'path';
import { fileURLToPath } from 'url';
import { downloadProbeBinary } from '../src/downloader.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const binDir = path.resolve(__dirname, '..', 'bin');
async function main() {
try {
console.log(`Creating bin directory at: ${binDir}`);
await fs.ensureDir(binDir);
console.log('Bin directory created successfully');
const readmePath = path.join(binDir, 'README.md');
const readmeContent = `# Probe Binary Directory
This directory is used to store the downloaded probe binary.
The binary is automatically downloaded during package installation.
If you encounter any issues with the download, you can manually place the probe binary in this directory.
Binary name should be:
- \`probe\` (on Linux/macOS)
- \`probe.exe\` (on Windows)
You can download the binary from: https://github.com/buger/probe/releases
`;
await fs.writeFile(readmePath, readmeContent);
console.log('Created README file in bin directory');
const gitignorePath = path.join(binDir, '.gitignore');
const gitignoreContent = `# Ignore all files in this directory
*
# Except these files
!.gitignore
!.gitkeep
!README.md
!probe
`;
await fs.writeFile(gitignorePath, gitignoreContent);
console.log('Created .gitignore file in bin directory');
console.log('Downloading probe binary...');
try {
let packageVersion = '0.0.0';
const possiblePaths = [
path.resolve(__dirname, '..', 'package.json'), path.resolve(__dirname, '..', '..', 'package.json') ];
for (const packageJsonPath of possiblePaths) {
try {
if (fs.existsSync(packageJsonPath)) {
console.log(`Found package.json at: ${packageJsonPath}`);
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
if (packageJson.version) {
packageVersion = packageJson.version;
console.log(`Using version from package.json: ${packageVersion}`);
break;
}
}
} catch (err) {
console.error(`Error reading package.json at ${packageJsonPath}:`, err);
}
}
const binaryPath = await downloadProbeBinary(packageVersion);
console.log(`Successfully downloaded probe binary to: ${binaryPath}`);
const isWindows = process.platform === 'win32';
const placeholderBinaryName = 'probe' + (isWindows ? '.exe' : '');
const placeholderBinaryPath = path.join(binDir, placeholderBinaryName);
if (binaryPath !== placeholderBinaryPath) {
console.log(`Replacing placeholder binary at ${placeholderBinaryPath} with actual binary from ${binaryPath}`);
await fs.copyFile(binaryPath, placeholderBinaryPath);
await fs.chmod(placeholderBinaryPath, 0o755); }
console.log('\nProbe binary was successfully downloaded and installed during installation.');
console.log('You can now use the probe command directly from the command line.');
} catch (error) {
console.error('Error downloading probe binary:', error);
console.error('\nNote: The probe binary will need to be downloaded when you first use the package.');
console.error('If you encounter any issues, you can manually place the binary in the bin directory.');
console.error('You can download it from: https://github.com/buger/probe/releases');
return;
}
} catch (error) {
console.error(`Error in postinstall script: ${error.message}`);
console.error('You may need to manually create the bin directory or run with elevated privileges.');
}
}
main().catch(error => {
console.error('Unexpected error during postinstall:', error);
});