From 26de205b9858671593c8bf1e8f73560229fd94a6 Mon Sep 17 00:00:00 2001 From: Jonathan Neal Date: Mon, 22 Nov 2021 15:11:13 -0500 Subject: [PATCH] Warn on missing pages directory (#1902) * Warn on missing pages directory * skip hostname flag test * kill process at end of config test * fix hostname test * cleanup console logs --- packages/astro/src/cli/index.ts | 1 + packages/astro/src/core/build/index.ts | 2 +- packages/astro/src/core/dev/index.ts | 6 +++--- packages/astro/src/core/ssr/routing.ts | 10 ++++++++-- packages/astro/test/config.test.js | 12 +++++++++--- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index 28acf286db..621678e50f 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -36,6 +36,7 @@ function resolveArgs(flags: Arguments): CLIState { sitemap: typeof flags.sitemap === 'boolean' ? flags.sitemap : undefined, port: typeof flags.port === 'number' ? flags.port : undefined, config: typeof flags.config === 'string' ? flags.config : undefined, + hostname: typeof flags.hostname === 'string' ? flags.hostname : undefined, }; if (flags.version) { diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index 68e2e0024a..049ec8809b 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -48,7 +48,7 @@ class AstroBuilder { const port = config.devOptions.port; // no need to save this (don’t rely on port in builder) this.logging = options.logging; this.origin = config.buildOptions.site ? new URL(config.buildOptions.site).origin : `http://localhost:${port}`; - this.manifest = createRouteManifest({ config }); + this.manifest = createRouteManifest({ config }, this.logging); } async build() { diff --git a/packages/astro/src/core/dev/index.ts b/packages/astro/src/core/dev/index.ts index 924cd87ec2..f1326d30fe 100644 --- a/packages/astro/src/core/dev/index.ts +++ b/packages/astro/src/core/dev/index.ts @@ -76,7 +76,7 @@ export class AstroDevServer { this.site = config.buildOptions.site ? new URL(config.buildOptions.site) : undefined; this.devRoot = this.site ? this.site.pathname : '/'; this.url = new URL(this.devRoot, this.origin); - this.manifest = createRouteManifest({ config }); + this.manifest = createRouteManifest({ config }, this.logging); } async start() { @@ -250,7 +250,7 @@ export class AstroDevServer { return; } this.routeCache = {}; - this.manifest = createRouteManifest({ config: this.config }); + this.manifest = createRouteManifest({ config: this.config }, this.logging); }); viteServer.watcher.on('unlink', (file) => { // Only rebuild routes if deleted file is a page. @@ -258,7 +258,7 @@ export class AstroDevServer { return; } this.routeCache = {}; - this.manifest = createRouteManifest({ config: this.config }); + this.manifest = createRouteManifest({ config: this.config }, this.logging); }); viteServer.watcher.on('change', () => { // No need to rebuild routes on file content changes. diff --git a/packages/astro/src/core/ssr/routing.ts b/packages/astro/src/core/ssr/routing.ts index d3ac3bf589..1eedfb67fc 100644 --- a/packages/astro/src/core/ssr/routing.ts +++ b/packages/astro/src/core/ssr/routing.ts @@ -83,7 +83,7 @@ interface Item { } /** Create manifest of all static routes */ -export function createRouteManifest({ config, cwd }: { config: AstroConfig; cwd?: string }): ManifestData { +export function createRouteManifest({ config, cwd }: { config: AstroConfig; cwd?: string }, logging: LogOptions): ManifestData { const components: string[] = []; const routes: RouteData[] = []; @@ -194,7 +194,13 @@ export function createRouteManifest({ config, cwd }: { config: AstroConfig; cwd? }); } - walk(fileURLToPath(config.pages), [], []); + if (fs.existsSync(config.pages)) { + walk(fileURLToPath(config.pages), [], []); + } else { + const pagesDirRootRelative = config.pages.href.slice(config.projectRoot.href.length); + + warn(logging, 'astro', `Missing pages directory: ${pagesDirRootRelative}`); + } return { routes, diff --git a/packages/astro/test/config.test.js b/packages/astro/test/config.test.js index dc07637d3b..021148a119 100644 --- a/packages/astro/test/config.test.js +++ b/packages/astro/test/config.test.js @@ -21,12 +21,15 @@ describe('config', () => { const proc = devCLI(cwdURL, args); proc.stdout.setEncoding('utf8'); + for await (const chunk of proc.stdout) { if (/Local:/.test(chunk)) { expect(chunk).to.include('127.0.0.1'); break; } } + + proc.kill(); }); }); @@ -36,14 +39,17 @@ describe('config', () => { const cwdURL = new URL(cwd, import.meta.url); const configPath = new URL('./config/my-config.mjs', cwdURL).pathname; const args = ['--config', configPath]; - const process = devCLI(cwdURL, args); + const proc = devCLI(cwdURL, args); - process.stdout.setEncoding('utf8'); - for await (const chunk of process.stdout) { + proc.stdout.setEncoding('utf8'); + + for await (const chunk of proc.stdout) { if (/Server started/.test(chunk)) { break; } } + + proc.kill(); }); });