ziro 0.0.2

跨平台端口管理工具 - 快速查找和终止占用端口的进程
#!/usr/bin/env node

const https = require('https');
const fs = require('fs');
const path = require('path');
const { detectPlatform, getBinaryName, getDownloadUrl } = require('./detect-platform');

// 从 package.json 读取版本号
const packageJson = require('../package.json');
const version = packageJson.version;

async function download(url, dest) {
  return new Promise((resolve, reject) => {
    console.log(`: ${url}`);
    
    const file = fs.createWriteStream(dest);
    
    https.get(url, {
      headers: {
        'User-Agent': 'ziro-installer',
      },
    }, (response) => {
      // 处理重定向
      if (response.statusCode === 302 || response.statusCode === 301) {
        const redirectUrl = response.headers.location;
        console.log(`: ${redirectUrl}`);
        
        https.get(redirectUrl, (redirectResponse) => {
          if (redirectResponse.statusCode !== 200) {
            reject(new Error(`HTTP : ${redirectResponse.statusCode}`));
            return;
          }
          
          const totalBytes = parseInt(redirectResponse.headers['content-length'], 10);
          let downloadedBytes = 0;
          
          redirectResponse.on('data', (chunk) => {
            downloadedBytes += chunk.length;
            const percent = ((downloadedBytes / totalBytes) * 100).toFixed(1);
            process.stdout.write(`\r: ${percent}%`);
          });
          
          redirectResponse.pipe(file);
          
          file.on('finish', () => {
            file.close();
            console.log('\n下载完成');
            resolve();
          });
        }).on('error', (err) => {
          fs.unlink(dest, () => {});
          reject(err);
        });
        
        return;
      }
      
      if (response.statusCode !== 200) {
        reject(new Error(`HTTP : ${response.statusCode}`));
        return;
      }
      
      const totalBytes = parseInt(response.headers['content-length'], 10);
      let downloadedBytes = 0;
      
      response.on('data', (chunk) => {
        downloadedBytes += chunk.length;
        if (totalBytes) {
          const percent = ((downloadedBytes / totalBytes) * 100).toFixed(1);
          process.stdout.write(`\r: ${percent}%`);
        }
      });
      
      response.pipe(file);
      
      file.on('finish', () => {
        file.close();
        console.log('\n下载完成');
        resolve();
      });
    }).on('error', (err) => {
      fs.unlink(dest, () => {});
      reject(err);
    });
  });
}

async function install() {
  try {
    console.log('正在安装 ziro...');
    
    // 检测平台
    const platformInfo = detectPlatform();
    console.log(`: ${platformInfo.platform} (${platformInfo.arch})`);
    
    // 确保 bin 目录存在
    const binDir = path.join(__dirname, '..', 'bin');
    if (!fs.existsSync(binDir)) {
      fs.mkdirSync(binDir, { recursive: true });
    }
    
    // 生成二进制文件名和路径
    const binaryName = getBinaryName(platformInfo);
    const binaryPath = path.join(binDir, platformInfo.raw.platform === 'win32' ? 'ziro.exe' : 'ziro');
    
    // 构建下载 URL
    const downloadUrl = getDownloadUrl(version, platformInfo);
    
    // 下载二进制文件
    try {
      await download(downloadUrl, binaryPath);
    } catch (error) {
      console.error('\n下载失败:', error.message);
      console.error('\n可能的原因:');
      console.error('1. 该版本的预编译二进制文件尚未发布');
      console.error('2. 网络连接问题');
      console.error('3. 当前平台/架构不受支持');
      console.error('\n替代方案:');
      console.error('1. 使用 Cargo 安装: cargo install ziro');
      console.error('2. 从源码编译: git clone https://github.com/Protagonistss/ziro && cd ziro && cargo build --release');
      process.exit(1);
    }
    
    // 设置执行权限(Unix 系统)
    if (platformInfo.raw.platform !== 'win32') {
      fs.chmodSync(binaryPath, 0o755);
      console.log('设置执行权限完成');
    }
    
    console.log('✓ 安装成功!');
    console.log('\n使用方法:');
    console.log('  ziro find <port>     - 查找占用端口的进程');
    console.log('  ziro kill <port>...  - 终止占用端口的进程');
    console.log('  ziro list            - 列出所有端口占用情况');
    console.log('  ziro --help          - 查看帮助信息');
    
  } catch (error) {
    console.error('安装失败:', error.message);
    console.error('\n如果问题持续存在,请访问: https://github.com/Protagonistss/ziro/issues');
    process.exit(1);
  }
}

// 仅在直接运行时执行(不是被 require 时)
if (require.main === module) {
  install();
}

module.exports = { install };