Skip to content

Commit

Permalink
Add limited concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
yanthomasdev committed Nov 28, 2024
1 parent 4c8b9b0 commit bb58040
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"jiti": "2.3.3",
"js-yaml": "^4.1.0",
"neotraverse": "^0.6.18",
"p-all": "^5.0.0",
"path-to-regexp": "6.3.0",
"picomatch": "^4.0.2",
"simple-git": "^3.27.0",
Expand Down
72 changes: 46 additions & 26 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve } from 'node:path';
import { type ConsolaInstance, createConsola } from 'consola';
import pAll from 'p-all';
import picomatch from 'picomatch';
import { glob } from 'tinyglobby';
import { loadConfig, validateInitialConfig } from './config/config.js';
Expand Down Expand Up @@ -91,17 +92,27 @@ class Lunaria {
);
}

/** We use `Promise.all` to allow the promises to run in parallel, increasing the performance considerably. */
const entries = (
await Promise.all(
sourceFilePaths.map(async (path) => {
return await this.#getFileStatus(path, false);
}),
)
).sort((a, b) => (a?.source.path ?? '').localeCompare(b?.source.path ?? ''));

for (const entry of entries) {
if (entry) status.push(entry);
const entries: LunariaStatus = new Array(sourceFilePaths.length);

await pAll(
sourceFilePaths.map((path) => {
return async () => {
const entry = await this.getFileStatus(path);
if (entry) entries.push(entry);
};
}),
{
concurrency: 10,
},
);

// We sort the entries by source path to make the resulting status consistent.
// That is, entries will be laid out by precedence in the `files` array, and then
// sorted internally.
const sortedEntries = entries.sort((a, b) => a.source.path.localeCompare(b.source.path));

for (const entry of sortedEntries) {
status.push(entry);
}
}

Expand Down Expand Up @@ -170,23 +181,20 @@ class Lunaria {
await cache.write(this.#git.cache);
}

return {
...file,
source: {
lang: this.config.sourceLocale.lang,
path: sourcePath,
git: latestSourceChanges,
},
localizations: await Promise.all(
this.config.locales.map(async ({ lang }): Promise<StatusLocalizationEntry> => {
const localizations: StatusLocalizationEntry[] = new Array(this.config.locales.length);

const tasks = this.config.locales.map(({ lang }) => {
return async () => {
{
const localizedPath = toPath(sourcePath, lang);

if (!(await exists(resolve(externalSafePath(external, this.#cwd, localizedPath))))) {
return {
localizations.push({
lang: lang,
path: localizedPath,
status: 'missing',
};
});
return;
}

const latestLocaleChanges = await this.#git.getFileLatestChanges(localizedPath);
Expand Down Expand Up @@ -221,15 +229,27 @@ class Lunaria {
return {};
};

return {
localizations.push({
lang: lang,
path: localizedPath,
git: latestLocaleChanges,
status: isOutdated ? 'outdated' : 'up-to-date',
...(await entryTypeData()),
};
}),
),
});
}
};
});

await pAll(tasks, { concurrency: 5 });

return {
...file,
source: {
lang: this.config.sourceLocale.lang,
path: sourcePath,
git: latestSourceChanges,
},
localizations,
};
}

Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bb58040

Please sign in to comment.