import SftpClient from 'ssh2-sftp-client';

const args = process.argv.splice(2);
const remoteName = args[0];
const remoteList = {
  test: {
    host: '192.168.65.161',
    port: 22,
    username: 'root',
    password: 'carsafe',
    remotePath: '/usr/local/nginx/html/dm',
    localPath: './dist',
  },
  prod: {
    host: '192.168.190.161',
    port: 22,
    username: 'root',
    password: 'Carsafe@2025',
    remotePath: '/usr/local/nginx/html/dm',
    localPath: './dist',
  },
};
console.log('打包环境：', remoteName);
const config = remoteList[remoteName];
if (!remoteName || !config) {
  console.error('找不到对应环境!');
  process.exit(1);
}

async function deploy() {
  const sftp = new SftpClient();

  try {
    console.log('打包完成...');
    console.log('正在连接...');
    await sftp.connect({
      host: config.host,
      port: config.port,
      username: config.username,
      password: config.password,
    });
    console.log('连接成功...');
    console.log('开始上传...');
    await sftp.uploadDir(config.localPath, config.remotePath);
    console.log('上传成功!');
  } catch (err) {
    console.error('部署失败:', err.message);
    process.exit(1);
  } finally {
    await sftp.end();
  }
}

deploy();
