From 95eaa207d8bd4cb5ca2cc1c6279a9bd4096b33d7 Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Tue, 19 Jul 2022 13:48:07 -0400 Subject: [PATCH] Feat: log warning on `astro.config` change, restart server on `astro.config` added (#3968) * feat: log on config change, restart on config add * chore: changeset * chore: remove unused export --- .changeset/wet-wombats-prove.md | 5 +++++ packages/astro/src/cli/index.ts | 35 ++++++++++++++++++++++++++++--- packages/astro/src/core/config.ts | 3 +++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 .changeset/wet-wombats-prove.md diff --git a/.changeset/wet-wombats-prove.md b/.changeset/wet-wombats-prove.md new file mode 100644 index 000000000000..da0b0193469f --- /dev/null +++ b/.changeset/wet-wombats-prove.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Improve warning logs on astro.config change diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index ce588acfa78a..3b3bb6f7b1b7 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -8,7 +8,7 @@ import build from '../core/build/index.js'; import { openConfig } from '../core/config.js'; import devServer from '../core/dev/index.js'; import { collectErrorMetadata } from '../core/errors.js'; -import { debug, LogOptions } from '../core/logger/core.js'; +import { debug, info, LogOptions, warn } from '../core/logger/core.js'; import { enableVerboseLogging, nodeLogDestination } from '../core/logger/node.js'; import { formatConfigErrorMessage, formatErrorMessage, printHelp } from '../core/messages.js'; import preview from '../core/preview/index.js'; @@ -132,7 +132,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) { } } - const { astroConfig, userConfig } = await openConfig({ cwd: root, flags, cmd }); + let { astroConfig, userConfig, userConfigPath } = await openConfig({ cwd: root, flags, cmd }); telemetry.record(event.eventCliSession(cmd, userConfig, flags)); // Common CLI Commands: @@ -140,7 +140,36 @@ async function runCommand(cmd: string, flags: yargs.Arguments) { // by the end of this switch statement. switch (cmd) { case 'dev': { - await devServer(astroConfig, { logging, telemetry }); + async function startDevServer() { + const { watcher, stop } = await devServer( + astroConfig, + { logging, telemetry }, + ); + + watcher.on('change', logRestartServerOnConfigChange); + watcher.on('unlink', logRestartServerOnConfigChange); + function logRestartServerOnConfigChange(changedFile: string) { + if (userConfigPath === changedFile) { + warn(logging, 'astro', 'Astro config updated. Restart server to see changes!'); + } + } + + watcher.on('add', async function restartServerOnNewConfigFile(addedFile: string) { + // if there was not a config before, attempt to resolve + if (!userConfigPath && addedFile.includes('astro.config')) { + const addedConfig = await openConfig({ cwd: root, flags, cmd }); + if (addedConfig.userConfigPath) { + info(logging, 'astro', 'Astro config detected. Restarting server...'); + astroConfig = addedConfig.astroConfig; + userConfig = addedConfig.userConfig; + userConfigPath = addedConfig.userConfigPath; + await stop(); + await startDevServer(); + } + } + }); + } + await startDevServer(); return await new Promise(() => {}); // lives forever } diff --git a/packages/astro/src/core/config.ts b/packages/astro/src/core/config.ts index f0b77e83047f..c08bf3576507 100644 --- a/packages/astro/src/core/config.ts +++ b/packages/astro/src/core/config.ts @@ -453,6 +453,7 @@ export async function resolveConfigURL( interface OpenConfigResult { userConfig: AstroUserConfig; + userConfigPath: string | undefined; astroConfig: AstroConfig; flags: CLIFlags; root: string; @@ -489,12 +490,14 @@ export async function openConfig(configOptions: LoadConfigOptions): Promise