File size: 1,221 Bytes
46252cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { config } from 'dotenv';
import * as fs from 'fs';
import * as path from 'path';
import { getDataDirectory } from '../config/data-directory';
import { clearBlankEnv, BLANK_SHADOWED_ENV_KEYS } from '../config/env-precedence';

/**
 * Load environment for the standalone TypeORM CLI data-sources with the SAME precedence as
 * src/main.ts: `process.env` > `.env` > the configured data directory's `.env.generated` (all dotenv
 * `override: false`), with blank compose-forwarded keys cleared first. Without this, the migration CLI
 * ignored the dashboard-written configuration and ran against the default SQLite DB even when the
 * deployment was configured (via the dashboard) for PostgreSQL — i.e. `migration:run:prod` targeted
 * the wrong database. `cwd` is injectable for testing.
 */
export function loadCliEnv(cwd: string = process.cwd()): void {
  clearBlankEnv(process.env, BLANK_SHADOWED_ENV_KEYS);
  const userEnvPath = path.resolve(cwd, '.env');
  const generatedEnvPath = path.join(getDataDirectory(cwd), '.env.generated');
  if (fs.existsSync(userEnvPath)) config({ path: userEnvPath, override: false });
  if (fs.existsSync(generatedEnvPath)) config({ path: generatedEnvPath, override: false });
}