Skip to content

Commit 6e3bc49

Browse files
committed
watch: reload env file for --env-file-if-exists
1 parent 74717cb commit 6e3bc49

File tree

2 files changed

+58
-54
lines changed

2 files changed

+58
-54
lines changed

lib/internal/main/watch_mode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ markBootstrapComplete();
3333
// TODO(MoLow): Make kill signal configurable
3434
const kKillSignal = 'SIGTERM';
3535
const kShouldFilterModules = getOptionValue('--watch-path').length === 0;
36-
const kEnvFile = getOptionValue('--env-file');
36+
const kEnvFile = getOptionValue('--env-file') || getOptionValue('--env-file-if-exists');
3737
const kWatchedPaths = ArrayPrototypeMap(getOptionValue('--watch-path'), (path) => resolve(path));
3838
const kPreserveOutput = getOptionValue('--watch-preserve-output');
3939
const kCommand = ArrayPrototypeSlice(process.argv, 1);

test/sequential/test-watch-mode.mjs

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ async function runWriteSucceed({
104104
if (watchFlag !== null) args.unshift(watchFlag);
105105
const child = spawn(execPath, args, { encoding: 'utf8', stdio: 'pipe', ...options });
106106
let completes = 0;
107-
let cancelRestarts = () => {};
107+
let cancelRestarts = () => { };
108108
let stderr = '';
109109
const stdout = [];
110110

@@ -141,7 +141,7 @@ async function runWriteSucceed({
141141

142142
async function failWriteSucceed({ file, watchedFile }) {
143143
const child = spawn(execPath, ['--watch', '--no-warnings', file], { encoding: 'utf8', stdio: 'pipe' });
144-
let cancelRestarts = () => {};
144+
let cancelRestarts = () => { };
145145

146146
try {
147147
// Break the chunks into lines
@@ -164,9 +164,11 @@ tmpdir.refresh();
164164
describe('watch mode', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_000 }, () => {
165165
it('should watch changes to a file', async () => {
166166
const file = createTmpFile();
167-
const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: file, watchFlag: '--watch=true', options: {
168-
timeout: 10000
169-
} });
167+
const { stderr, stdout } = await runWriteSucceed({
168+
file, watchedFile: file, watchFlag: '--watch=true', options: {
169+
timeout: 10000
170+
}
171+
});
170172

171173
assert.strictEqual(stderr, '');
172174
assert.deepStrictEqual(stdout, [
@@ -192,55 +194,57 @@ describe('watch mode', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_00
192194
]);
193195
});
194196

195-
it('should reload env variables when --env-file changes', async () => {
196-
const envKey = `TEST_ENV_${Date.now()}`;
197-
const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey});`);
198-
const envFile = createTmpFile(`${envKey}=value1`, '.env');
199-
const { done, restart } = runInBackground({ args: ['--watch', `--env-file=${envFile}`, jsFile] });
200-
201-
try {
202-
await restart();
203-
writeFileSync(envFile, `${envKey}=value2`);
204-
205-
// Second restart, after env change
206-
const { stdout, stderr } = await restart();
207-
208-
assert.strictEqual(stderr, '');
209-
assert.deepStrictEqual(stdout, [
210-
`Restarting ${inspect(jsFile)}`,
211-
'ENV: value2',
212-
`Completed running ${inspect(jsFile)}`,
213-
]);
214-
} finally {
215-
await done();
216-
}
217-
});
197+
for (const cmd in ['--env-file', '--env-file-if-exists']) {
198+
it(`should reload env variables when ${cmd} changes`, async () => {
199+
const envKey = `TEST_ENV_${Date.now()}`;
200+
const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey});`);
201+
const envFile = createTmpFile(`${envKey}=value1`, '.env');
202+
const { done, restart } = runInBackground({ args: ['--watch', `${cmd}=${envFile}`, jsFile] });
203+
204+
try {
205+
await restart();
206+
writeFileSync(envFile, `${envKey}=value2`);
207+
208+
// Second restart, after env change
209+
const { stdout, stderr } = await restart();
210+
211+
assert.strictEqual(stderr, '');
212+
assert.deepStrictEqual(stdout, [
213+
`Restarting ${inspect(jsFile)}`,
214+
'ENV: value2',
215+
`Completed running ${inspect(jsFile)}`,
216+
]);
217+
} finally {
218+
await done();
219+
}
220+
});
218221

219-
it('should load new env variables when --env-file changes', async () => {
220-
const envKey = `TEST_ENV_${Date.now()}`;
221-
const envKey2 = `TEST_ENV_2_${Date.now()}`;
222-
const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey} + '\\n' + 'ENV2: ' + process.env.${envKey2});`);
223-
const envFile = createTmpFile(`${envKey}=value1`, '.env');
224-
const { done, restart } = runInBackground({ args: ['--watch', `--env-file=${envFile}`, jsFile] });
225-
226-
try {
227-
await restart();
228-
writeFileSync(envFile, `${envKey}=value1\n${envKey2}=newValue`);
229-
230-
// Second restart, after env change
231-
const { stderr, stdout } = await restart();
232-
233-
assert.strictEqual(stderr, '');
234-
assert.deepStrictEqual(stdout, [
235-
`Restarting ${inspect(jsFile)}`,
236-
'ENV: value1',
237-
'ENV2: newValue',
238-
`Completed running ${inspect(jsFile)}`,
239-
]);
240-
} finally {
241-
await done();
242-
}
243-
});
222+
it(`should load new env variables when ${cmd} changes`, async () => {
223+
const envKey = `TEST_ENV_${Date.now()}`;
224+
const envKey2 = `TEST_ENV_2_${Date.now()}`;
225+
const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey} + '\\n' + 'ENV2: ' + process.env.${envKey2});`);
226+
const envFile = createTmpFile(`${envKey}=value1`, '.env');
227+
const { done, restart } = runInBackground({ args: ['--watch', `${cmd}=${envFile}`, jsFile] });
228+
229+
try {
230+
await restart();
231+
writeFileSync(envFile, `${envKey}=value1\n${envKey2}=newValue`);
232+
233+
// Second restart, after env change
234+
const { stderr, stdout } = await restart();
235+
236+
assert.strictEqual(stderr, '');
237+
assert.deepStrictEqual(stdout, [
238+
`Restarting ${inspect(jsFile)}`,
239+
'ENV: value1',
240+
'ENV2: newValue',
241+
`Completed running ${inspect(jsFile)}`,
242+
]);
243+
} finally {
244+
await done();
245+
}
246+
});
247+
}
244248

245249
it('should watch changes to a failing file', async () => {
246250
const file = createTmpFile('throw new Error("fails");');

0 commit comments

Comments
 (0)