From 3257dd28901c785a6a661211b98c5ef2cb3b9aa4 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Fri, 19 May 2023 13:53:46 +0200 Subject: [PATCH] Improve content collection error message for empty folders (#7118) --- .changeset/tasty-geese-fix.md | 5 ++++ packages/astro/src/content/types-generator.ts | 26 ++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 .changeset/tasty-geese-fix.md diff --git a/.changeset/tasty-geese-fix.md b/.changeset/tasty-geese-fix.md new file mode 100644 index 000000000000..5c02621a686a --- /dev/null +++ b/.changeset/tasty-geese-fix.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix unnecessary warning showing on start when a collection folder was empty. The warning was also enhanced to add more information about possible causes. diff --git a/packages/astro/src/content/types-generator.ts b/packages/astro/src/content/types-generator.ts index aa8c7369d616..ee4c9907735c 100644 --- a/packages/astro/src/content/types-generator.ts +++ b/packages/astro/src/content/types-generator.ts @@ -101,15 +101,21 @@ export async function createContentTypesGenerator({ readdir: fs.readdir.bind(fs), readdirSync: fs.readdirSync.bind(fs), }, + onlyFiles: false, + objectMode: true, }); - const entries = globResult - .map((e) => new URL(e, contentPaths.contentDir)) - .filter( - // Config loading handled first. Avoid running twice. - (e) => !e.href.startsWith(contentPaths.config.url.href) - ); - for (const entry of entries) { - events.push({ type: { name: 'add', entry }, opts: { logLevel: 'warn' } }); + + for (const entry of globResult) { + const entryURL = new URL(entry.path, contentPaths.contentDir); + if (entryURL.href.startsWith(contentPaths.config.url.href)) continue; + if (entry.dirent.isFile()) { + events.push({ + type: { name: 'add', entry: entryURL }, + opts: { logLevel: 'warn' }, + }); + } else if (entry.dirent.isDirectory()) { + events.push({ type: { name: 'addDir', entry: entryURL }, opts: { logLevel: 'warn' } }); + } } await runEvents(); return { typesGenerated: true }; @@ -503,9 +509,9 @@ function warnNonexistentCollections({ warn( logging, 'content', - `${JSON.stringify( + `The ${JSON.stringify( configuredCollection - )} is not a collection. Check your content config for typos.` + )} collection does not have an associated folder in your \`content\` directory. Make sure the folder exists, or check your content config for typos.` ); } }