From de3d79e06b6914c394ba3703a7611dcf44e13814 Mon Sep 17 00:00:00 2001 From: John Beene Date: Thu, 23 Feb 2023 14:05:17 -0600 Subject: [PATCH] fix: too many files opened for collections with many entries (#6313) * fix: too many files opened for collections with many entries * Update packages/astro/src/content/types-generator.ts * Update packages/astro/src/content/types-generator.ts * Update packages/astro/src/content/types-generator.ts * Update packages/astro/src/content/types-generator.ts * Update packages/astro/src/content/types-generator.ts * Update packages/astro/src/content/types-generator.ts --------- Co-authored-by: Nate Moore --- packages/astro/src/content/types-generator.ts | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/packages/astro/src/content/types-generator.ts b/packages/astro/src/content/types-generator.ts index a7899e923..8d990d586 100644 --- a/packages/astro/src/content/types-generator.ts +++ b/packages/astro/src/content/types-generator.ts @@ -41,6 +41,11 @@ type CreateContentGeneratorParams = { type EventOpts = { logLevel: 'info' | 'warn' }; +type EventWithOptions = { + type: ContentEvent; + opts: EventOpts | undefined; +}; + class UnsupportedFileTypeError extends Error {} export async function createContentTypesGenerator({ @@ -53,7 +58,7 @@ export async function createContentTypesGenerator({ const contentTypes: ContentTypes = {}; const contentPaths = getContentPaths(settings.config, fs); - let events: Promise<{ shouldGenerateTypes: boolean; error?: Error }>[] = []; + let events: EventWithOptions[] = []; let debounceTimeout: NodeJS.Timeout | undefined; const contentTypesBase = await fs.promises.readFile(contentPaths.typesTemplate, 'utf-8'); @@ -65,7 +70,11 @@ export async function createContentTypesGenerator({ return { typesGenerated: false, reason: 'no-content-dir' }; } - events.push(handleEvent({ name: 'add', entry: contentPaths.config.url }, { logLevel: 'warn' })); + events.push({ + type: { name: 'add', entry: contentPaths.config.url }, + opts: { logLevel: 'warn' }, + }); + const globResult = await glob('**', { cwd: fileURLToPath(contentPaths.contentDir), fs: { @@ -80,7 +89,7 @@ export async function createContentTypesGenerator({ (e) => !e.href.startsWith(contentPaths.config.url.href) ); for (const entry of entries) { - events.push(handleEvent({ name: 'add', entry }, { logLevel: 'warn' })); + events.push({ type: { name: 'add', entry }, opts: { logLevel: 'warn' } }); } await runEvents(); return { typesGenerated: true }; @@ -204,12 +213,15 @@ export async function createContentTypesGenerator({ function queueEvent(rawEvent: RawContentEvent, opts?: EventOpts) { const event = { - entry: pathToFileURL(rawEvent.entry), - name: rawEvent.name, + type: { + entry: pathToFileURL(rawEvent.entry), + name: rawEvent.name, + }, + opts, }; - if (!event.entry.pathname.startsWith(contentPaths.contentDir.pathname)) return; + if (!event.type.entry.pathname.startsWith(contentPaths.contentDir.pathname)) return; - events.push(handleEvent(event, opts)); + events.push(event); debounceTimeout && clearTimeout(debounceTimeout); debounceTimeout = setTimeout( @@ -220,7 +232,13 @@ export async function createContentTypesGenerator({ async function runEvents(opts?: EventOpts) { const logLevel = opts?.logLevel ?? 'info'; - const eventResponses = await Promise.all(events); + const eventResponses = []; + + for (const event of events) { + const response = await handleEvent(event.type, event.opts); + eventResponses.push(response); + } + events = []; let unsupportedFiles = []; for (const response of eventResponses) {