Skip to content

Commit

Permalink
fix: too many files opened for collections with many entries (#6313)
Browse files Browse the repository at this point in the history
* 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 <natemoo-re@users.noreply.github.com>
  • Loading branch information
wbjohn and natemoo-re authored Feb 23, 2023
1 parent 504c7ba commit de3d79e
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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');
Expand All @@ -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: {
Expand All @@ -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 };
Expand Down Expand Up @@ -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(
Expand All @@ -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) {
Expand Down

0 comments on commit de3d79e

Please sign in to comment.