Skip to content

Commit

Permalink
watch: watch env files in --watch mode
Browse files Browse the repository at this point in the history
observe env files in --watch mode through the watcher's filterFile and
forward changes to the childProcess

Fixes: nodejs#54001
  • Loading branch information
HBSPS committed Jul 25, 2024
1 parent 3de7a4c commit b1e8725
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/internal/main/watch_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
ArrayPrototypePushApply,
ArrayPrototypeSlice,
StringPrototypeStartsWith,
StringPrototypeSplit,
} = primordials;

const {
Expand All @@ -22,10 +23,11 @@ const { FilesWatcher } = require('internal/watch_mode/files_watcher');
const { green, blue, red, white, clear } = require('internal/util/colors');

const { spawn } = require('child_process');
const { inspect } = require('util');
const { inspect, parseEnv } = require('util');
const { setTimeout, clearTimeout } = require('timers');
const { resolve } = require('path');
const { once } = require('events');
const { readFileSync } = require('fs');

prepareMainThreadExecution(false, false);
markBootstrapComplete();
Expand Down Expand Up @@ -64,11 +66,25 @@ let exited;

function start() {
exited = false;

let kEnv = {};
for (let i = 0; i < process.execArgv.length; i++) {
const arg = process.execArgv[i];
if (StringPrototypeStartsWith(arg, '--env-file')) {
const [,envFileName] = StringPrototypeSplit(arg, '=');
const envFile = resolve(envFileName);
watcher.filterFile(envFile);
const envContent = parseEnv(readFileSync(envFile, 'utf8'));
kEnv = {...kEnv, ...envContent}
}
}

const stdio = kShouldFilterModules ? ['inherit', 'inherit', 'inherit', 'ipc'] : 'inherit';
child = spawn(process.execPath, argsWithoutWatchOptions, {
stdio,
env: {
...process.env,
...kEnv,
WATCH_REPORT_DEPENDENCIES: '1',
},
});
Expand Down

0 comments on commit b1e8725

Please sign in to comment.