super-productivity/tools/load-env.js
2025-07-14 20:52:51 +02:00

69 lines
1.8 KiB
JavaScript
Executable file

#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
// Load .env file
const envPath = path.join(process.cwd(), '.env');
const envConfig = dotenv.config({ path: envPath });
if (envConfig.error) {
console.warn('⚠️ No .env file found, generating empty env.generated.ts');
}
const env = envConfig.parsed || {};
// Generate TypeScript content
const tsContent = `// This file is auto-generated by tools/load-env.js
// Do not modify directly - edit .env file instead
// Generated at: ${new Date().toISOString()}
/**
* Environment variables loaded from .env file
* Access these constants instead of process.env in your Angular app
*/
export const ENV = {
${Object.entries(env)
.map(([key, value]) => {
// Escape quotes in values
const escapedValue = value.replace(/'/g, "\\'");
return ` ${key}: '${escapedValue}',`;
})
.join('\n')}
} as const;
// Type-safe helper to ensure all expected env vars are defined
export type EnvVars = typeof ENV;
`;
// Ensure the config directory exists
const configDir = path.join(process.cwd(), 'src/app/config');
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
// Write the generated file
const outputPath = path.join(configDir, 'env.generated.ts');
fs.writeFileSync(outputPath, tsContent);
console.log(
`✅ Generated ${outputPath} with ${Object.keys(env).length} environment variables`,
);
// Pass through to the next command
if (process.argv.length > 2) {
// Execute the rest of the command
const { spawn } = require('child_process');
const command = process.argv[2];
const args = process.argv.slice(3);
const child = spawn(command, args, {
stdio: 'inherit',
shell: true,
});
child.on('exit', (code) => {
process.exit(code);
});
}