Update index.ts

修复pushPagesSecre为json
This commit is contained in:
sjcuhd
2025-12-25 21:01:30 +08:00
committed by GitHub
parent 98393c8a43
commit 9736d19a46

View File

@@ -278,7 +278,13 @@ const pushPagesSecret = () => {
console.log("🔐 Pushing environment secrets to Pages..."); console.log("🔐 Pushing environment secrets to Pages...");
// 定义运行时所需的环境变量列表 // 定义运行时所需的环境变量列表
const runtimeEnvVars = ['AUTH_GITHUB_ID', 'AUTH_GITHUB_SECRET', 'AUTH_GOOGLE_ID', 'AUTH_GOOGLE_SECRET', 'AUTH_SECRET']; const runtimeEnvVars = [
'AUTH_GITHUB_ID',
'AUTH_GITHUB_SECRET',
'AUTH_GOOGLE_ID',
'AUTH_GOOGLE_SECRET',
'AUTH_SECRET'
];
try { try {
// 确保.env文件存在 // 确保.env文件存在
@@ -286,41 +292,74 @@ const pushPagesSecret = () => {
setupEnvFile(); setupEnvFile();
} }
// 创建一个临时文件,只包含运行时所需的环境变量 // 读取.env文件内容
const envContent = readFileSync(resolve('.env'), 'utf-8'); const envContent = readFileSync(resolve('.env'), 'utf-8');
const runtimeEnvFile = resolve('.env.runtime');
// 解析环境变量为对象
const secrets: Record<string, string> = {};
envContent.split('\n').forEach(line => {
const trimmedLine = line.trim();
// 跳过注释和空行
if (!trimmedLine || trimmedLine.startsWith('#')) {
return;
}
// 解析键值对
const equalIndex = trimmedLine.indexOf('=');
if (equalIndex === -1) {
return;
}
const key = trimmedLine.substring(0, equalIndex).trim();
let value = trimmedLine.substring(equalIndex + 1).trim();
// 移除引号
value = value.replace(/^["']|["']$/g, '');
// 只保留运行时所需的环境变量,且值不为空
if (runtimeEnvVars.includes(key) && value.length > 0) {
secrets[key] = value;
}
});
// 从.env文件中提取运行时变量 // 检查是否有需要推送的secrets
const runtimeEnvContent = envContent if (Object.keys(secrets).length === 0) {
.split('\n') console.log("⚠️ No runtime secrets found to push");
.filter(line => { return;
const trimmedLine = line.trim(); }
// 跳过注释和空行
if (!trimmedLine || trimmedLine.startsWith('#')) return false;
// 检查是否为运行时所需的环境变量 // 创建JSON格式的临时文件
for (const varName of runtimeEnvVars) { const runtimeEnvFile = resolve('.env.runtime.json');
if (line.startsWith(`${varName} =`) || line.startsWith(`${varName}=`)) { writeFileSync(runtimeEnvFile, JSON.stringify(secrets, null, 2));
const value = line.substring(line.indexOf('=') + 1).trim().replace(/^["']|["']$/g, '');
return value.length > 0;
}
}
return false;
})
.join('\n');
// 写入临时文件 console.log(`📝 Found ${Object.keys(secrets).length} secrets to push:`, Object.keys(secrets).join(', '));
writeFileSync(runtimeEnvFile, runtimeEnvContent);
// 使用临时文件推送secrets // 使用临时文件推送secrets
execSync(`pnpm dlx wrangler pages secret bulk ${runtimeEnvFile}`, { stdio: "inherit" }); execSync(`pnpm dlx wrangler pages secret bulk ${runtimeEnvFile}`, {
stdio: "inherit"
});
// 清理临时文件 // 清理临时文件
execSync(`rm ${runtimeEnvFile}`, { stdio: "inherit" }); if (existsSync(runtimeEnvFile)) {
execSync(`rm ${runtimeEnvFile}`, { stdio: "inherit" });
}
console.log("✅ Secrets pushed successfully"); console.log("✅ Secrets pushed successfully");
} catch (error) { } catch (error) {
console.error("❌ Failed to push secrets:", error); console.error("❌ Failed to push secrets:", error);
// 确保清理临时文件
const runtimeEnvFile = resolve('.env.runtime.json');
if (existsSync(runtimeEnvFile)) {
try {
execSync(`rm ${runtimeEnvFile}`, { stdio: "inherit" });
} catch (cleanupError) {
console.error("⚠️ Failed to cleanup temporary file:", cleanupError);
}
}
throw error; throw error;
} }
}; };