From f6add3924d5cd59925a6ea4bf7f2f731709bc893 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Mon, 7 Nov 2022 15:14:34 -0500 Subject: [PATCH] When creating a new dev container, restart it if previously started (#5314) * When creating a new dev container, restart it if previously started * Adding a changeset --- .changeset/selfish-mangos-drum.md | 5 +++ packages/astro/src/core/dev/index.ts | 2 +- packages/astro/src/core/dev/restart.ts | 10 ++--- packages/astro/test/units/dev/restart.test.js | 39 ++++++++++++++++++- 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 .changeset/selfish-mangos-drum.md diff --git a/.changeset/selfish-mangos-drum.md b/.changeset/selfish-mangos-drum.md new file mode 100644 index 000000000..8fa2e7e75 --- /dev/null +++ b/.changeset/selfish-mangos-drum.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes regression with config file restarts diff --git a/packages/astro/src/core/dev/index.ts b/packages/astro/src/core/dev/index.ts index 7c0ed10fd..04af04486 100644 --- a/packages/astro/src/core/dev/index.ts +++ b/packages/astro/src/core/dev/index.ts @@ -1,3 +1,3 @@ -export { createContainer, runInContainer, startContainer } from './container.js'; +export { createContainer, runInContainer, startContainer, isStarted } from './container.js'; export { default } from './dev.js'; export { createContainerWithAutomaticRestart } from './restart.js'; diff --git a/packages/astro/src/core/dev/restart.ts b/packages/astro/src/core/dev/restart.ts index edbc9af02..9ded41325 100644 --- a/packages/astro/src/core/dev/restart.ts +++ b/packages/astro/src/core/dev/restart.ts @@ -8,10 +8,10 @@ import { createContainer, isStarted, startContainer } from './container.js'; async function createRestartedContainer( container: Container, - settings: AstroSettings + settings: AstroSettings, + needsStart: boolean ): Promise { const { logging, fs, resolvedRoot, configFlag, configFlagPath } = container; - const needsStart = isStarted(container); const newContainer = await createContainer({ isRestart: true, logging, @@ -78,10 +78,10 @@ export async function restartContainer({ const { logging, close, resolvedRoot, settings: existingSettings } = container; container.restartInFlight = true; - //console.clear(); // TODO move this if (beforeRestart) { beforeRestart(); } + const needsStart = isStarted(container); try { const newConfig = await openConfig({ cwd: resolvedRoot, @@ -96,7 +96,7 @@ export async function restartContainer({ const settings = createSettings(astroConfig, resolvedRoot); await close(); return { - container: await createRestartedContainer(container, settings), + container: await createRestartedContainer(container, settings, needsStart), error: null, }; } catch (_err) { @@ -105,7 +105,7 @@ export async function restartContainer({ await close(); info(logging, 'astro', 'Continuing with previous valid configuration\n'); return { - container: await createRestartedContainer(container, existingSettings), + container: await createRestartedContainer(container, existingSettings, needsStart), error, }; } diff --git a/packages/astro/test/units/dev/restart.test.js b/packages/astro/test/units/dev/restart.test.js index a993019c4..2e78f789d 100644 --- a/packages/astro/test/units/dev/restart.test.js +++ b/packages/astro/test/units/dev/restart.test.js @@ -3,9 +3,10 @@ import * as cheerio from 'cheerio'; import { createContainerWithAutomaticRestart, - runInContainer, + isStarted, + startContainer, } from '../../../dist/core/dev/index.js'; -import { createFs, createRequestAndResponse } from '../test-utils.js'; +import { createFs, createRequestAndResponse, triggerFSEvent } from '../test-utils.js'; const root = new URL('../../fixtures/alias/', import.meta.url); @@ -74,4 +75,38 @@ describe('dev container restarts', () => { await restart.container.close(); } }); + + it('Restarts the container if previously started', async () => { + const fs = createFs( + { + '/src/pages/index.astro': ` + + Test + +

Test

+ + + `, + '/astro.config.mjs': ``, + }, + root + ); + + let restart = await createContainerWithAutomaticRestart({ + params: { fs, root }, + }); + await startContainer(restart.container); + expect(isStarted(restart.container)).to.equal(true); + + try { + // Trigger a change + let restartComplete = restart.restarted(); + triggerFSEvent(restart.container, fs, '/astro.config.mjs', 'change'); + await restartComplete; + + expect(isStarted(restart.container)).to.equal(true); + } finally { + await restart.container.close(); + } + }); });