Skip to content

Commit

Permalink
Feat: log warning on astro.config change, restart server on `astro.…
Browse files Browse the repository at this point in the history
…config` added (#3968)

* feat: log on config change, restart on config add

* chore: changeset

* chore: remove unused export
  • Loading branch information
bholmesdev authored Jul 19, 2022
1 parent 91e051d commit 95eaa20
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-wombats-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Improve warning logs on astro.config change
35 changes: 32 additions & 3 deletions packages/astro/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -132,15 +132,44 @@ 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:
// These commands run normally. All commands are assumed to have been handled
// 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
}

Expand Down
3 changes: 3 additions & 0 deletions packages/astro/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ export async function resolveConfigURL(

interface OpenConfigResult {
userConfig: AstroUserConfig;
userConfigPath: string | undefined;
astroConfig: AstroConfig;
flags: CLIFlags;
root: string;
Expand Down Expand Up @@ -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,
};
Expand Down

0 comments on commit 95eaa20

Please sign in to comment.