import path from 'path';
import { Client } from 'basic-ftp';
import config from './ftp-config.json' with { type: 'json' };

async function uploadPWA() {
  const client = new Client();
  client.ftp.verbose = true;

  try {
    await client.access({
      host: config.host,
      user: config.user,
      password: config.password,
      port: config.port,      
    });

    // 上传PWA构建目录中的所有文件
    const localPath = path.resolve('./dist/offline');
    const remotePath = config.remotePath;

    await client.ensureDir(remotePath);
    await client.clearWorkingDir();
    await client.uploadFromDir(localPath);

    console.log('PWA版本已成功上传到FTP服务器');
  } catch (error) {
    console.error('FTP上传失败:', error);
  } finally {
    client.close();
  }
}

uploadPWA();