ls-plus 0.0.1

Enhanced ls command with modern features - supports both cargo and npm installation
#!/usr/bin/env node

const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');

// 获取二进制文件路径
function getBinaryPath() {
  const platform = process.platform;
  const binaryName = platform === 'win32' ? 'ls-plus.exe' : 'ls-plus';
  const binaryPath = path.join(__dirname, binaryName);
  
  if (!fs.existsSync(binaryPath)) {
    console.error('❌ Binary not found at:', binaryPath);
    console.error('💡 Installation may have failed. Please try:');
    console.error('   npm install --force');
    console.error('   or');
    console.error('   yarn install --force');
    process.exit(1);
  }
  
  return binaryPath;
}

// 执行二进制文件
function runBinary() {
  const binaryPath = getBinaryPath();
  // 直接传递用户参数,不包含脚本名
  const [_node, _script, ...userArgs] = process.argv;
  
  const child = spawn(binaryPath, userArgs, {
    stdio: 'inherit',
    windowsHide: false
  });
  
  child.on('error', (error) => {
    console.error('❌ Failed to start ls-plus:', error.message);
    if (error.code === 'ENOENT') {
      console.error('💡 Binary file may be corrupted or missing.');
      console.error('   Try reinstalling: npm install --force');
    } else if (error.code === 'EACCES') {
      console.error('💡 Permission denied. Try:');
      console.error('   chmod +x', binaryPath);
    }
    process.exit(1);
  });
  
  child.on('exit', (code, signal) => {
    if (signal) {
      process.kill(process.pid, signal);
    } else {
      process.exit(code || 0);
    }
  });
}

// 主函数
if (require.main === module) {
  runBinary();
}

module.exports = { runBinary };