mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-16 19:33:11 +02:00
Add system for managing env vars (#5902)
# Description of Changes Previously, `VITE_*` environment variables were scattered across the codebase with hardcoded fallback values inline (e.g. `import.meta.env.VITE_STRIPE_KEY || 'pk_live_...'`). This made it unclear which variables were required, what they were for, and caused real keys to be silently used in builds where they hadn't been explicitly configured. ## What's changed I've added `frontend/.env.example` and `frontend/.env.desktop.example`, which declare every `VITE_*` variable the app uses, with comments explaining each one and sensible defaults where applicable. These are the source of truth for what's required. I've added a setup script which runs before `npm run dev`, `build`, `tauri-dev`, and all `tauri-build*` commands. It: - Creates your local `.env` / `.env.desktop` from the example files on first run, so you don't need to do anything manually - Errors if you're missing keys that the example defines (e.g. after pulling changes that added a new variable). These can either be manually-set env vars, or in your `.env` file (env vars take precedence over `.env` file vars when running) - Warns if you have `VITE_*` variables set in your environment that aren't listed in any example file I've removed all `|| 'hardcoded-value'` defaults from source files because they are not necessary in this system, as all variables must be explicitly set (they can be set to `VITE_ENV_VAR=`, just as long as the variable actually exists). I think this system will make it really obvious exactly what you need to set and what's actually running in the code. I've added a test that checks that every `import.meta.env.VITE_*` reference found in source is present in at least one example file, so new variables can't be added without being documented. ## For contributors New contributors shouldn't need to do anything - `npm run dev` will create your `.env` automatically. If you already have a `.env` file in the `frontend/` folder, you may well need to update it to make the system happy. Here's an example output from running `npm run dev` with an old `.env` file: ``` $ npm run dev > [email protected] dev > npm run prep && vite > [email protected] prep > tsx scripts/setup-env.ts && npm run generate-icons setup-env: see frontend/README.md#environment-variables for documentation setup-env: .env is missing keys from config/.env.example: VITE_GOOGLE_DRIVE_CLIENT_ID VITE_GOOGLE_DRIVE_API_KEY VITE_GOOGLE_DRIVE_APP_ID VITE_PUBLIC_POSTHOG_KEY VITE_PUBLIC_POSTHOG_HOST Add them manually or delete your local file to re-copy from the example. setup-env: the following VITE_ vars are set but not listed in any example file: VITE_DEV_BYPASS_AUTH Add them to config/.env.example or config/.env.desktop.example if they are required. ``` If you add a new `VITE_*` variable to the codebase, add it to the appropriate `frontend/config/.env.example` file or the test will fail.
This commit is contained in:
+25
-20
@@ -79,36 +79,39 @@
|
||||
"web-vitals": "^5.1.0"
|
||||
},
|
||||
"scripts": {
|
||||
"pretauri-build": "node scripts/build-provisioner.mjs",
|
||||
"predev": "npm run generate-icons",
|
||||
"dev": "vite",
|
||||
"dev:core": "vite --mode core",
|
||||
"dev:proprietary": "vite --mode proprietary",
|
||||
"dev:saas": "vite --mode saas",
|
||||
"dev:desktop": "vite --mode desktop",
|
||||
"prebuild": "npm run generate-icons",
|
||||
"prep": "tsx scripts/setup-env.ts && npm run generate-icons",
|
||||
"prep:saas": "tsx scripts/setup-env.ts --saas && npm run generate-icons",
|
||||
"prep:desktop": "tsx scripts/setup-env.ts --desktop && npm run generate-icons",
|
||||
"prep:desktop-build": "node scripts/build-provisioner.mjs && npm run prep:desktop",
|
||||
"dev": "npm run prep && vite",
|
||||
"dev:core": "npm run prep && vite --mode core",
|
||||
"dev:proprietary": "npm run prep && vite --mode proprietary",
|
||||
"dev:saas": "npm run prep:saas && vite --mode saas",
|
||||
"dev:desktop": "npm run prep:desktop && vite --mode desktop",
|
||||
"lint": "npm run lint:eslint && npm run lint:cycles",
|
||||
"lint:eslint": "eslint --max-warnings=0",
|
||||
"lint:cycles": "dpdm src --circular --no-warning --no-tree --exit-code circular:1",
|
||||
"build": "vite build",
|
||||
"build:core": "vite build --mode core",
|
||||
"build:proprietary": "vite build --mode proprietary",
|
||||
"build:saas": "vite build --mode saas",
|
||||
"build:desktop": "vite build --mode desktop",
|
||||
"build": "npm run prep && vite build",
|
||||
"build:core": "npm run prep && vite build --mode core",
|
||||
"build:proprietary": "npm run prep && vite build --mode proprietary",
|
||||
"build:saas": "npm run prep:saas && vite build --mode saas",
|
||||
"build:desktop": "npm run prep:desktop && vite build --mode desktop",
|
||||
"preview": "vite preview",
|
||||
"tauri-dev": "tauri dev --no-watch",
|
||||
"tauri-build": "tauri build",
|
||||
"tauri-build-dev": "tauri build --no-bundle",
|
||||
"tauri-build-dev-mac": "tauri build --bundles app",
|
||||
"tauri-build-dev-windows": "tauri build --bundles nsis",
|
||||
"tauri-build-dev-linux": "tauri build --bundles appimage",
|
||||
"tauri-dev": "npm run prep:desktop && tauri dev --no-watch",
|
||||
"tauri-build": "npm run prep:desktop-build && tauri build",
|
||||
"_tauri-build-dev": "npm run prep:desktop && tauri build",
|
||||
"tauri-build-dev": "npm run _tauri-build-dev -- --no-bundle",
|
||||
"tauri-build-dev-mac": "npm run _tauri-build-dev -- --bundles app",
|
||||
"tauri-build-dev-windows": "npm run _tauri-build-dev -- --bundles nsis",
|
||||
"tauri-build-dev-linux": "npm run _tauri-build-dev -- --bundles appimage",
|
||||
"tauri-clean": "cd src-tauri && cargo clean && cd .. && rm -rf dist build",
|
||||
"typecheck": "npm run typecheck:proprietary",
|
||||
"typecheck:core": "tsc --noEmit --project src/core/tsconfig.json",
|
||||
"typecheck:proprietary": "tsc --noEmit --project src/proprietary/tsconfig.json",
|
||||
"typecheck:saas": "tsc --noEmit --project src/saas/tsconfig.json",
|
||||
"typecheck:desktop": "tsc --noEmit --project src/desktop/tsconfig.json",
|
||||
"typecheck:all": "npm run typecheck:core && npm run typecheck:proprietary && npm run typecheck:saas && npm run typecheck:desktop",
|
||||
"typecheck:scripts": "tsc --noEmit --project scripts/tsconfig.json",
|
||||
"typecheck:all": "npm run typecheck:core && npm run typecheck:proprietary && npm run typecheck:saas && npm run typecheck:desktop && npm run typecheck:scripts",
|
||||
"check": "npm run typecheck && npm run lint && npm run test:run",
|
||||
"generate-licenses": "node scripts/generate-licenses.js",
|
||||
"generate-icons": "node scripts/generate-icons.js",
|
||||
@@ -160,6 +163,7 @@
|
||||
"@typescript-eslint/parser": "^8.44.1",
|
||||
"@vitejs/plugin-react-swc": "^4.1.0",
|
||||
"@vitest/coverage-v8": "^3.2.4",
|
||||
"dotenv": "^16.4.7",
|
||||
"dpdm": "^3.14.0",
|
||||
"eslint": "^10.0.2",
|
||||
"jsdom": "^27.0.0",
|
||||
@@ -171,6 +175,7 @@
|
||||
"postcss-simple-vars": "^7.0.1",
|
||||
"puppeteer": "^24.25.0",
|
||||
"typescript": "^5.9.2",
|
||||
"tsx": "^4.19.4",
|
||||
"typescript-eslint": "^8.44.1",
|
||||
"vite": "^7.1.7",
|
||||
"vite-plugin-static-copy": "^3.1.4",
|
||||
|
||||
Reference in New Issue
Block a user