Skip to content

Commit

Permalink
Allow overriding env vars from .env files by setting local.settings.j…
Browse files Browse the repository at this point in the history
…son file
  • Loading branch information
AlexPshul committed Oct 15, 2024
1 parent 09935cb commit 9fe7081
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions packages/func/src/executors/start/executor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { Executor } from '@nx/devkit';
import { Executor, readJsonFile } from '@nx/devkit';
import { ChildProcessWithoutNullStreams, spawn } from 'child_process';
import { fileExists } from 'nx/src/utils/fileutils';
import treeKill from 'tree-kill';
import { color } from '../../common';
import { build, watch } from '../common';
import { FuncLogger } from './func-logger';
import { StartExecutorSchema } from './schema';

const loadProcessEnvWithoutOverrides = (projectCwd: string) => {
const localProcessEnvCopy = { ...process.env };
const localSettingsJsonPath = `${projectCwd}/local.settings.json`;
if (!fileExists(localSettingsJsonPath)) return localProcessEnvCopy;

const localSettingsConfig = readJsonFile<{ Values: Record<string, string> }>(localSettingsJsonPath).Values;
if (!localSettingsConfig) return localProcessEnvCopy;

Object.keys(localSettingsConfig).forEach(key => delete localProcessEnvCopy[key]);

return localProcessEnvCopy;
};

const executor: Executor<StartExecutorSchema> = async (options, context) => {
const { port, disableWatch, additionalFlags } = options;
const { workspace, projectName, isVerbose, target } = context;
Expand All @@ -20,7 +34,8 @@ const executor: Executor<StartExecutorSchema> = async (options, context) => {
if (isVerbose) console.log(`Running ${target.executor} command: func ${params.join(' ')}.`);

const cwd = workspace?.projects[projectName].root;
spawned = spawn('func', params, { cwd, detached: false, shell: true });
const noOverridesEnvVars = loadProcessEnvWithoutOverrides(cwd);
spawned = spawn('func', params, { cwd, detached: false, shell: true, env: noOverridesEnvVars });

spawned.stdout.on('data', data => logger.logData(data?.toString()));
spawned.stderr.on('data', data => logger.logError(data?.toString()));
Expand Down

0 comments on commit 9fe7081

Please sign in to comment.