"use strict";
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
const prefix = "VITE_APP_";
const APP_DIR = "./application";
function generateScriptContent() {
const prefixRegex = new RegExp(`^${prefix}`);
const env = process.env;
const config = Object.keys(env)
.filter(key => prefixRegex.test(key))
.reduce((c, key) => Object.assign({}, c, { [key]: env[key] }), {});
return `window.env=${JSON.stringify(config)};`;
}
function getSha256Hash(input) {
const hash = crypto.createHash("sha256");
hash.update(input);
return hash.digest("hex");
}
function updateIndexHtml(envFileName, basePath) {
const indexPath = path.join(APP_DIR, "index.html");
if (!fs.existsSync(indexPath)) {
console.error(`Error: ${indexPath} not found`);
return;
}
let indexContent = fs.readFileSync(indexPath, "utf8");
// Replace base path placeholder with actual value
// indexContent = indexContent.replace(new RegExp(RUNTIME_BASE_URL_PLACEHOLDER, "g"), basePath || "/");
// Replace any existing env script with the new one
const envScriptRegex = /`;
if (envScriptRegex.test(indexContent)) {
indexContent = indexContent.replace(envScriptRegex, newEnvScript);
} else {
// If no existing env script, add it before the first script tag
const insertPoint = indexContent.indexOf("