Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: log warning on astro.config change, restart server on astro.config added #3968

Merged
merged 3 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I included that fuzzy addedFile.includes('astro.config') to avoid the work-intensive openConfig on most file changes.

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