Files
Stirling-PDF/frontend/editor/scripts/setup-env.mts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
2.1 KiB
TypeScript
Raw Normal View History

2026-03-12 13:03:44 +00:00
/**
* 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.
2026-03-12 13:03:44 +00:00
*
* Usage:
* 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
2026-03-12 13:03:44 +00:00
*/
import { existsSync, writeFileSync } from "fs";
2026-05-22 13:40:34 +01:00
import { dirname, join, resolve } from "path";
import { fileURLToPath } from "url";
2026-03-12 13:03:44 +00:00
2026-05-22 13:40:34 +01:00
// .env files live next to the editor's vite.config.ts (frontend/editor/).
// Resolve relative to this script regardless of where the build was invoked.
// `import.meta.dirname` would be tidier but isn't available under tsx's CJS
// transpilation today, so go via fileURLToPath for portability.
const scriptDir = dirname(fileURLToPath(import.meta.url));
const root = resolve(scriptDir, "..");
2026-03-12 13:03:44 +00:00
const args = process.argv.slice(2);
const isDesktop = args.includes("--desktop");
const isSaas = args.includes("--saas");
2026-03-12 13:03:44 +00:00
function template(parent: string): string {
return [
"###############################################################################",
2026-05-22 13:40:34 +01:00
`# Local overrides for \`frontend/editor/${parent}\``,
"# Put API keys and machine-specific settings here. Any variable defined here",
`# takes precedence over the committed \`${parent}\``,
"###############################################################################",
"",
].join("\n");
2026-03-12 13:03:44 +00:00
}
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`);
2026-03-12 13:03:44 +00:00
}
}
ensureLocalFile(".env.local", ".env");
if (isDesktop) ensureLocalFile(".env.desktop.local", ".env.desktop");
if (isSaas) ensureLocalFile(".env.saas.local", ".env.saas");