const fs = require("fs");
const path = require("path");
const [sourceFile, targetFile] = process.argv.slice(2);

if (!sourceFile || !targetFile) {
    console.error("请提供源文件和目标文件的路径作为参数。");
    process.exit(1);
}

const sourcePath = path.resolve(sourceFile);
const targetPath = path.resolve(targetFile);

// 复制文件
fs.copyFile(sourcePath, targetPath, (err) => {
    if (err) {
        console.error("复制文件时出错:", err);
        process.exit(1);
    }
    console.log(`成功复制文件从 ${sourcePath} 到 ${targetPath}`);
});

fs.writeFileSync(
    "./public/swenv.js",
    `
  const process = {
    env: {
        NEXT_PUBLIC_FIREBASE_APIKEY: '${process.env.NEXT_PUBLIC_FIREBASE_APIKEY}',
        NEXT_PUBLIC_FIREBASE_AUTHDOMAIN: '${process.env.NEXT_PUBLIC_FIREBASE_AUTHDOMAIN}',
        NEXT_PUBLIC_FIREBASE_PROJECTID: '${process.env.NEXT_PUBLIC_FIREBASE_PROJECTID}',
        NEXT_PUBLIC_FIREBASE_STORAGEBUCKET: '${process.env.NEXT_PUBLIC_FIREBASE_STORAGEBUCKET}',
        NEXT_PUBLIC_FIREBASE_MESSAGINGSENDERID: '${process.env.NEXT_PUBLIC_FIREBASE_MESSAGINGSENDERID}',
        NEXT_PUBLIC_FIREBASE_APPID: '${process.env.NEXT_PUBLIC_FIREBASE_APPID}',
        NEXT_PUBLIC_FIREBASE_MEASUREMENTID: '${process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENTID}',
        NEXT_PUBLIC_FIREBASE_KEYS: '${process.env.NEXT_PUBLIC_FIREBASE_KEYS}',
    }
}
`
);
