Skip to content

Commit

Permalink
Merge branch 'main' into client-address
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored Jul 19, 2022
2 parents 25f8370 + 6f69516 commit 5dccc59
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 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
6 changes: 0 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,6 @@ jobs:
repository: withastro/docs
path: smoke/docs

- name: Checkout astro.build
uses: actions/checkout@v3
with:
repository: withastro/astro.build
path: smoke/astro-build

- name: Install dependencies
run: pnpm install --no-frozen-lockfile

Expand Down
32 changes: 29 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,41 @@ 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 5dccc59

Please sign in to comment.