mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-14 10:34:06 +02:00
Change frontend .env files to be committed and have .env.*.local overrides (#6207)
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
/**
|
||||
* Copies missing env files from their .example templates, and warns about
|
||||
* any keys present in the example but not set in the environment.
|
||||
* Also warns about any VITE_ vars set in the environment that aren't listed
|
||||
* in any example file.
|
||||
* Ensures `.env.local` (and mode-specific `.env.desktop.local` / `.env.saas.local`)
|
||||
* files exist so developers have a place to put overrides (API keys, machine-specific
|
||||
* settings) without touching the committed `.env` / `.env.desktop` / `.env.saas` files.
|
||||
*
|
||||
* Vite automatically layers these `.local` files on top of the committed ones.
|
||||
*
|
||||
* Usage:
|
||||
* tsx scripts/setup-env.ts # checks .env
|
||||
* tsx scripts/setup-env.ts --desktop # also checks .env.desktop
|
||||
* tsx scripts/setup-env.ts --saas # also checks .env.saas
|
||||
* tsx scripts/setup-env.ts # ensures .env.local
|
||||
* tsx scripts/setup-env.ts --desktop # also ensures .env.desktop.local
|
||||
* tsx scripts/setup-env.ts --saas # also ensures .env.saas.local
|
||||
*/
|
||||
|
||||
import { existsSync, copyFileSync, readFileSync } from "fs";
|
||||
import { existsSync, writeFileSync } from "fs";
|
||||
import { join } from "path";
|
||||
import { config, parse } from "dotenv";
|
||||
|
||||
// npm scripts run from the directory containing package.json (frontend/)
|
||||
const root = process.cwd();
|
||||
@@ -20,75 +20,25 @@ const args = process.argv.slice(2);
|
||||
const isDesktop = args.includes("--desktop");
|
||||
const isSaas = args.includes("--saas");
|
||||
|
||||
console.log(
|
||||
"setup-env: see frontend/README.md#environment-variables for documentation",
|
||||
);
|
||||
|
||||
function getExampleKeys(exampleFile: string): string[] {
|
||||
const examplePath = join(root, exampleFile);
|
||||
if (!existsSync(examplePath)) return [];
|
||||
return Object.keys(parse(readFileSync(examplePath, "utf-8")));
|
||||
function template(parent: string): string {
|
||||
return [
|
||||
"###############################################################################",
|
||||
`# Local overrides for \`frontend/${parent}\``,
|
||||
"# Put API keys and machine-specific settings here. Any variable defined here",
|
||||
`# takes precedence over the committed \`${parent}\``,
|
||||
"###############################################################################",
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function ensureEnvFile(envFile: string, exampleFile: string): boolean {
|
||||
const envPath = join(root, envFile);
|
||||
const examplePath = join(root, exampleFile);
|
||||
|
||||
if (!existsSync(examplePath)) {
|
||||
console.warn(`setup-env: ${exampleFile} not found, skipping ${envFile}`);
|
||||
return false;
|
||||
function ensureLocalFile(localFile: string, parentFile: string): void {
|
||||
const localPath = join(root, localFile);
|
||||
if (!existsSync(localPath)) {
|
||||
writeFileSync(localPath, template(parentFile));
|
||||
console.log(`setup-env: created empty ${localFile} for local overrides`);
|
||||
}
|
||||
|
||||
if (!existsSync(envPath)) {
|
||||
copyFileSync(examplePath, envPath);
|
||||
console.log(`setup-env: created ${envFile} from ${exampleFile}`);
|
||||
}
|
||||
|
||||
config({ path: envPath });
|
||||
|
||||
const missing = getExampleKeys(exampleFile).filter(
|
||||
(k) => !(k in process.env),
|
||||
);
|
||||
|
||||
if (missing.length > 0) {
|
||||
console.error(
|
||||
`setup-env: ${envFile} is missing keys from ${exampleFile}:\n` +
|
||||
missing.map((k) => ` ${k}`).join("\n") +
|
||||
"\n Add them manually or delete your local file to re-copy from the example.",
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
let failed = false;
|
||||
failed = ensureEnvFile(".env", "config/.env.example") || failed;
|
||||
|
||||
if (isDesktop) {
|
||||
failed =
|
||||
ensureEnvFile(".env.desktop", "config/.env.desktop.example") || failed;
|
||||
}
|
||||
|
||||
if (isSaas) {
|
||||
failed = ensureEnvFile(".env.saas", "config/.env.saas.example") || failed;
|
||||
}
|
||||
|
||||
// Warn about any VITE_ vars set in the environment that aren't listed in any example file.
|
||||
const allExampleKeys = new Set([
|
||||
...getExampleKeys("config/.env.example"),
|
||||
...getExampleKeys("config/.env.desktop.example"),
|
||||
...getExampleKeys("config/.env.saas.example"),
|
||||
]);
|
||||
const unknownViteVars = Object.keys(process.env).filter(
|
||||
(k) => k.startsWith("VITE_") && !allExampleKeys.has(k),
|
||||
);
|
||||
if (unknownViteVars.length > 0) {
|
||||
console.warn(
|
||||
"setup-env: the following VITE_ vars are set but not listed in any example file:\n" +
|
||||
unknownViteVars.map((k) => ` ${k}`).join("\n") +
|
||||
"\n Add them to the appropriate config/.env.*.example file if they are required.",
|
||||
);
|
||||
}
|
||||
|
||||
if (failed) process.exit(1);
|
||||
ensureLocalFile(".env.local", ".env");
|
||||
if (isDesktop) ensureLocalFile(".env.desktop.local", ".env.desktop");
|
||||
if (isSaas) ensureLocalFile(".env.saas.local", ".env.saas");
|
||||
|
||||
Reference in New Issue
Block a user