Skip to content

Commit

Permalink
Don't inject .env file as real env variables for child processes
Browse files Browse the repository at this point in the history
This can cause problems when the child process has a custom .env loading mechanism (eg. additional files such as .env.secrets) but
dev-pm sets env vars which overrule the values loaded from .env by child child process
  • Loading branch information
nsams committed Oct 18, 2024
1 parent a54df7a commit 137619f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/daemon-command/script.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ChildProcess, execSync, spawn } from "child_process";
import colors from "colors";
import * as dotenv from "dotenv";
import * as dotenvExpand from "dotenv-expand";
import { Socket } from "net";
import waitOn from "wait-on";

Expand All @@ -8,6 +10,10 @@ import { ScriptDefinition } from "../script-definition.type";
const KEEP_LOG_LINES = 100;
export type ScriptStatus = "started" | "stopping" | "stopped" | "waiting" | "backoff";

const expandedEnv = { ...(process.env as Record<string, string>) };
dotenv.config({ processEnv: expandedEnv });
dotenvExpand.expand({ processEnv: expandedEnv });

export class Script {
scriptDefinition: ScriptDefinition;
status: ScriptStatus = "stopped";
Expand Down Expand Up @@ -40,7 +46,7 @@ export class Script {
const waitOn = Array.isArray(this.scriptDefinition.waitOn) ? this.scriptDefinition.waitOn : [this.scriptDefinition.waitOn];
return waitOn.map((str) =>
str.replace(/\$[a-z\d_]+/gi, function (match) {
const sub = process.env[match.substring(1)];
const sub = expandedEnv[match.substring(1)];
return sub || match;
}),
);
Expand Down
5 changes: 0 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
#!/usr/bin/env node

import { Command, Option } from "commander";
import * as dotenv from "dotenv";
import * as dotenvExpand from "dotenv-expand";

import { logs, restart, shutdown, start, status } from "./commands";
import { startDaemon } from "./commands/start-daemon.command";
import { stop } from "./commands/stop.command";

export { Config } from "./config.type";

const env = dotenv.config();
dotenvExpand.expand(env);

const program = new Command();
// eslint-disable-next-line @typescript-eslint/no-var-requires
program.version(require("../package.json").version);
Expand Down

0 comments on commit 137619f

Please sign in to comment.