Skip to content

Commit

Permalink
feat: loadEnvironmentVariables() show the number of updated env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed Sep 26, 2023
1 parent e744d1b commit 4b0abc6
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 5 deletions.
20 changes: 16 additions & 4 deletions packages/shared-lib-node/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export const yargsOptionsBuilderForEnv = {
/**
* This function loads environment variables from `.env` files.
* */
export function loadEnvironmentVariables(argv: Options, cwd: string): Record<string, string> {
let envPaths = (argv.env ?? []).map((envPath) => envPath.toString());
export function loadEnvironmentVariables(argv: Options, cwd: string, orgCwd?: string): Record<string, string> {
let envPaths = (argv.env ?? []).map((envPath) => path.resolve(orgCwd ?? cwd, envPath.toString()));
const cascade =
argv.cascadeEnv ??
(argv.cascadeNodeEnv
Expand All @@ -52,20 +52,32 @@ export function loadEnvironmentVariables(argv: Options, cwd: string): Record<str
? process.env.WB_ENV || process.env.APP_ENV || process.env.NODE_ENV || 'development'
: undefined);
if (typeof cascade === 'string') {
if (envPaths.length === 0) envPaths.push('.env');
if (envPaths.length === 0) envPaths.push(path.join(cwd, '.env'));
envPaths = envPaths.flatMap((envPath) =>
cascade
? [`${envPath}.${cascade}.local`, `${envPath}.local`, `${envPath}.${cascade}`, envPath]
: [`${envPath}.local`, envPath]
);
}
envPaths = envPaths.map((envPath) => path.relative(cwd, envPath));
if (argv.verbose) {
console.info('Loading env files:', envPaths);
}

let envVars = {};
let envVars: Record<string, string> = {};
const orgEnvVars = { ...process.env };
for (const envPath of envPaths) {
envVars = { ...config({ path: path.join(cwd, envPath) }).parsed, ...envVars };
let count = 0;
for (const [key, value] of Object.entries(envVars)) {
if (orgEnvVars[key] !== value) {
orgEnvVars[key] = value;
count++;
}
}
if (count > 0) {
console.info(`Updated ${count} environment variables:`, envPath);
}
}

if (argv.checkEnv) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENV=development2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENV=production2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENV=test2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENV=development1
1 change: 1 addition & 0 deletions packages/shared-lib-node/test-fixtures/app/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENV=production1
1 change: 1 addition & 0 deletions packages/shared-lib-node/test-fixtures/app/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENV=test1
48 changes: 48 additions & 0 deletions packages/shared-lib-node/tests/env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { beforeEach, describe, expect, it } from 'vitest';

import { loadEnvironmentVariables } from '../src/env.js';

describe('loadEnvironmentVariables()', () => {
beforeEach(() => {
process.env.WB_ENV = '';
process.env.NODE_ENV = '';
});

it('should load no env vars with empty options', () => {
const envVars = loadEnvironmentVariables({}, 'test-fixtures/app');
expect(envVars).toEqual({});
});

it('should load env vars with --auto-cascade-env', () => {
const envVars = loadEnvironmentVariables({ autoCascadeEnv: true }, 'test-fixtures/app');
expect(envVars).toEqual({ NAME: 'app1', ENV: 'development1' });
});

it('should load env vars with --cascade-env=production', () => {
const envVars = loadEnvironmentVariables({ cascadeEnv: 'production', env: ['.env'] }, 'test-fixtures/app');
expect(envVars).toEqual({ NAME: 'app1', ENV: 'production1' });
});

it('should load env vars with --cascade-node-env and NODE_ENV=""', () => {
process.env.NODE_ENV = '';
const envVars = loadEnvironmentVariables({ cascadeNodeEnv: true, env: ['.env'] }, 'test-fixtures/app');
expect(envVars).toEqual({ NAME: 'app1', ENV: 'development1' });
});

it('should load env vars with --cascade-node-env and NODE_ENV=test', () => {
process.env.NODE_ENV = 'test';
const envVars = loadEnvironmentVariables({ cascadeNodeEnv: true, env: ['.env'] }, 'test-fixtures/app');
expect(envVars).toEqual({ NAME: 'app1', ENV: 'test1' });
});

it('should load env vars with --env=test-fixtures/another-app/.env --auto-cascade-env, WB_ENV=test and NODE_ENV=production', () => {
process.env.WB_ENV = 'test';
process.env.NODE_ENV = 'production';
const envVars = loadEnvironmentVariables(
{ autoCascadeEnv: true, env: ['.env'] },
'test-fixtures/app',
'test-fixtures/another-app'
);
expect(envVars).toEqual({ NAME: 'app2', ENV: 'test2' });
});
});
2 changes: 1 addition & 1 deletion packages/shared-lib-node/tests/hash.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'node:fs';
import path from 'node:path';

import { test, expect } from 'vitest';
import { expect, test } from 'vitest';

import { calculateHashFromFiles, updateHashFromFiles } from '../src/hash.js';

Expand Down

0 comments on commit 4b0abc6

Please sign in to comment.