Skip to content

Commit

Permalink
watch: reload env file for --env-file-if-exists
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBa committed Jan 17, 2025
1 parent 74717cb commit 6e3bc49
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 54 deletions.
2 changes: 1 addition & 1 deletion lib/internal/main/watch_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ markBootstrapComplete();
// TODO(MoLow): Make kill signal configurable
const kKillSignal = 'SIGTERM';
const kShouldFilterModules = getOptionValue('--watch-path').length === 0;
const kEnvFile = getOptionValue('--env-file');
const kEnvFile = getOptionValue('--env-file') || getOptionValue('--env-file-if-exists');
const kWatchedPaths = ArrayPrototypeMap(getOptionValue('--watch-path'), (path) => resolve(path));
const kPreserveOutput = getOptionValue('--watch-preserve-output');
const kCommand = ArrayPrototypeSlice(process.argv, 1);
Expand Down
110 changes: 57 additions & 53 deletions test/sequential/test-watch-mode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async function runWriteSucceed({
if (watchFlag !== null) args.unshift(watchFlag);
const child = spawn(execPath, args, { encoding: 'utf8', stdio: 'pipe', ...options });
let completes = 0;
let cancelRestarts = () => {};
let cancelRestarts = () => { };
let stderr = '';
const stdout = [];

Expand Down Expand Up @@ -141,7 +141,7 @@ async function runWriteSucceed({

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

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

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

it('should reload env variables when --env-file changes', async () => {
const envKey = `TEST_ENV_${Date.now()}`;
const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey});`);
const envFile = createTmpFile(`${envKey}=value1`, '.env');
const { done, restart } = runInBackground({ args: ['--watch', `--env-file=${envFile}`, jsFile] });

try {
await restart();
writeFileSync(envFile, `${envKey}=value2`);

// Second restart, after env change
const { stdout, stderr } = await restart();

assert.strictEqual(stderr, '');
assert.deepStrictEqual(stdout, [
`Restarting ${inspect(jsFile)}`,
'ENV: value2',
`Completed running ${inspect(jsFile)}`,
]);
} finally {
await done();
}
});
for (const cmd in ['--env-file', '--env-file-if-exists']) {
it(`should reload env variables when ${cmd} changes`, async () => {
const envKey = `TEST_ENV_${Date.now()}`;
const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey});`);
const envFile = createTmpFile(`${envKey}=value1`, '.env');
const { done, restart } = runInBackground({ args: ['--watch', `${cmd}=${envFile}`, jsFile] });

try {
await restart();
writeFileSync(envFile, `${envKey}=value2`);

// Second restart, after env change
const { stdout, stderr } = await restart();

assert.strictEqual(stderr, '');
assert.deepStrictEqual(stdout, [
`Restarting ${inspect(jsFile)}`,
'ENV: value2',
`Completed running ${inspect(jsFile)}`,
]);
} finally {
await done();
}
});

it('should load new env variables when --env-file changes', async () => {
const envKey = `TEST_ENV_${Date.now()}`;
const envKey2 = `TEST_ENV_2_${Date.now()}`;
const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey} + '\\n' + 'ENV2: ' + process.env.${envKey2});`);
const envFile = createTmpFile(`${envKey}=value1`, '.env');
const { done, restart } = runInBackground({ args: ['--watch', `--env-file=${envFile}`, jsFile] });

try {
await restart();
writeFileSync(envFile, `${envKey}=value1\n${envKey2}=newValue`);

// Second restart, after env change
const { stderr, stdout } = await restart();

assert.strictEqual(stderr, '');
assert.deepStrictEqual(stdout, [
`Restarting ${inspect(jsFile)}`,
'ENV: value1',
'ENV2: newValue',
`Completed running ${inspect(jsFile)}`,
]);
} finally {
await done();
}
});
it(`should load new env variables when ${cmd} changes`, async () => {
const envKey = `TEST_ENV_${Date.now()}`;
const envKey2 = `TEST_ENV_2_${Date.now()}`;
const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey} + '\\n' + 'ENV2: ' + process.env.${envKey2});`);
const envFile = createTmpFile(`${envKey}=value1`, '.env');
const { done, restart } = runInBackground({ args: ['--watch', `${cmd}=${envFile}`, jsFile] });

try {
await restart();
writeFileSync(envFile, `${envKey}=value1\n${envKey2}=newValue`);

// Second restart, after env change
const { stderr, stdout } = await restart();

assert.strictEqual(stderr, '');
assert.deepStrictEqual(stdout, [
`Restarting ${inspect(jsFile)}`,
'ENV: value1',
'ENV2: newValue',
`Completed running ${inspect(jsFile)}`,
]);
} finally {
await done();
}
});
}

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

0 comments on commit 6e3bc49

Please sign in to comment.