73 lines
1.7 KiB
JavaScript
73 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
const MACHINE_ENV_PATHS = [
|
|
process.env.MachineEnvFile,
|
|
process.env.MACHINE_ENV_FILE,
|
|
'/source/global-env-vars.txt',
|
|
process.env.HOME
|
|
? path.join(process.env.HOME, 'source/global-env-vars.txt')
|
|
: '',
|
|
].filter(Boolean);
|
|
|
|
const PROJECT_ENV_PATHS = [
|
|
'project.env',
|
|
'/app/project.env',
|
|
'apps/project.env',
|
|
];
|
|
|
|
function stripQuotes(value) {
|
|
const trimmed = value.trim();
|
|
if (
|
|
(trimmed.startsWith('"') && trimmed.endsWith('"')) ||
|
|
(trimmed.startsWith("'") && trimmed.endsWith("'"))
|
|
) {
|
|
return trimmed.slice(1, -1);
|
|
}
|
|
return trimmed;
|
|
}
|
|
|
|
function loadEnvFile(filePath, options = {}) {
|
|
const { override = false } = options;
|
|
const content = fs.readFileSync(filePath, 'utf8');
|
|
for (const line of content.split(/\r?\n/u)) {
|
|
if (!line.trim() || /^\s*#/u.test(line)) continue;
|
|
|
|
const match = line.match(/^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$/u);
|
|
if (!match) continue;
|
|
|
|
if (!override && process.env[match[1]] !== undefined) continue;
|
|
process.env[match[1]] = stripQuotes(match[2]);
|
|
}
|
|
}
|
|
|
|
const machineEnvFile = MACHINE_ENV_PATHS.find((candidate) =>
|
|
fs.existsSync(candidate),
|
|
);
|
|
if (machineEnvFile) {
|
|
loadEnvFile(machineEnvFile, { override: true });
|
|
}
|
|
|
|
for (const projectEnvFile of PROJECT_ENV_PATHS) {
|
|
if (fs.existsSync(projectEnvFile)) {
|
|
loadEnvFile(projectEnvFile);
|
|
}
|
|
}
|
|
|
|
const missing = [
|
|
'INFISICAL_CLIENT_ID',
|
|
'INFISICAL_CLIENT_SECRET',
|
|
'INFISICAL_PROJECT_ID',
|
|
].filter((key) => !process.env[key]);
|
|
|
|
if (missing.length > 0) {
|
|
console.error(
|
|
`Missing required environment variables: ${missing.join(', ')}`,
|
|
);
|
|
process.exit(1);
|
|
}
|
|
|
|
require('./dist/main.js');
|