From 7a9ec46b3d0543d1c6679291e7ee0cc44b99dc2b Mon Sep 17 00:00:00 2001 From: KGFCH2 Date: Tue, 2 Jun 2026 00:16:37 +0530 Subject: [PATCH] chore: add environment validation script and npm validate:env command --- gsecure/package.json | 3 ++- gsecure/scripts/validate-env.js | 42 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 gsecure/scripts/validate-env.js diff --git a/gsecure/package.json b/gsecure/package.json index b8f70b1..f105fed 100644 --- a/gsecure/package.json +++ b/gsecure/package.json @@ -6,7 +6,8 @@ "dev": "next dev", "build": "next build", "start": "next start", - "lint": "eslint" + "lint": "eslint", + "validate:env": "node scripts/validate-env.js" }, "dependencies": { "axios": "^1.13.2", diff --git a/gsecure/scripts/validate-env.js b/gsecure/scripts/validate-env.js new file mode 100644 index 0000000..5a36665 --- /dev/null +++ b/gsecure/scripts/validate-env.js @@ -0,0 +1,42 @@ +const fs = require('fs'); +const path = require('path'); + +const examplePath = path.join(__dirname, '..', '.env.local.example'); +const envPath = path.join(__dirname, '..', '.env.local'); + +if (!fs.existsSync(examplePath)) { + console.error('Missing .env.local.example in the gsecure directory.'); + process.exit(1); +} + +const exampleContent = fs.readFileSync(examplePath, 'utf8'); +const exampleKeys = exampleContent + .split(/\r?\n/) + .map((line) => line.trim()) + .filter((line) => line && !line.startsWith('#')) + .map((line) => line.split('=')[0].trim()); + +if (!fs.existsSync(envPath)) { + console.error('Missing .env.local. Please copy .env.local.example to .env.local and fill the required values.'); + console.error(' cp .env.local.example .env.local'); + process.exit(1); +} + +const envContent = fs.readFileSync(envPath, 'utf8'); +const envKeys = envContent + .split(/\r?\n/) + .map((line) => line.trim()) + .filter((line) => line && !line.startsWith('#')) + .map((line) => line.split('=')[0].trim()); + +const missingKeys = exampleKeys.filter((key) => !envKeys.includes(key)); + +if (missingKeys.length > 0) { + console.error('The following required environment keys are missing in .env.local:'); + missingKeys.forEach((key) => console.error(` - ${key}`)); + console.error('\nPlease update .env.local with the missing values and try again.'); + process.exit(1); +} + +console.log('✅ .env.local contains all required keys from .env.local.example'); +process.exit(0);