Skip to content

Commit

Permalink
When creating a new dev container, restart it if previously started (#…
Browse files Browse the repository at this point in the history
…5314)

* When creating a new dev container, restart it if previously started

* Adding a changeset
  • Loading branch information
matthewp authored Nov 7, 2022
1 parent c44d5dd commit f6add39
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-mangos-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes regression with config file restarts
2 changes: 1 addition & 1 deletion packages/astro/src/core/dev/index.ts
Original file line number Diff line number Diff line change
@@ -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';
10 changes: 5 additions & 5 deletions packages/astro/src/core/dev/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { createContainer, isStarted, startContainer } from './container.js';

async function createRestartedContainer(
container: Container,
settings: AstroSettings
settings: AstroSettings,
needsStart: boolean
): Promise<Container> {
const { logging, fs, resolvedRoot, configFlag, configFlagPath } = container;
const needsStart = isStarted(container);
const newContainer = await createContainer({
isRestart: true,
logging,
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand All @@ -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,
};
}
Expand Down
39 changes: 37 additions & 2 deletions packages/astro/test/units/dev/restart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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': `
<html>
<head><title>Test</title></head>
<body>
<h1>Test</h1>
</body>
</html>
`,
'/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();
}
});
});

0 comments on commit f6add39

Please sign in to comment.