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
This commit is contained in:
Ben Holmes 2022-07-19 13:48:07 -04:00 committed by GitHub
parent 91e051dc62
commit 95eaa207d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Improve warning logs on astro.config change

View file

@ -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
}

View file

@ -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<Open
}
if (config) {
userConfig = config.value;
userConfigPath = config.filePath;
}
const astroConfig = await resolveConfig(userConfig, root, flags, configOptions.cmd);
return {
astroConfig,
userConfig,
userConfigPath,
flags,
root,
};