feat: filter entry list from github before reading files (#868)

This commit is contained in:
Daniel Lautzenheiser 2023-09-07 08:06:04 -04:00 committed by GitHub
parent f5f79b38f8
commit ae5e261da5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 5 deletions

View File

@ -15,6 +15,7 @@ import {
getI18nEntry, getI18nEntry,
getI18nFiles, getI18nFiles,
getI18nFilesDepth, getI18nFilesDepth,
getI18nInfo,
groupEntries, groupEntries,
hasI18n, hasI18n,
} from './lib/i18n'; } from './lib/i18n';
@ -292,6 +293,21 @@ function collectionDepth<EF extends BaseField>(collection: Collection<EF>) {
return depth; return depth;
} }
function collectionRegex<EF extends BaseField>(collection: Collection<EF>): RegExp | undefined {
let ruleString = '';
if ('folder' in collection && collection.path) {
ruleString = `${collection.folder}/${collection.path}`.replace(/{{.*}}/gm, '(.*)');
}
if (hasI18n(collection)) {
const { defaultLocale } = getI18nInfo(collection);
ruleString += `\\.${defaultLocale}\\..*`;
}
return ruleString ? new RegExp(ruleString) : undefined;
}
export class Backend<EF extends BaseField = UnknownField, BC extends BackendClass = BackendClass> { export class Backend<EF extends BaseField = UnknownField, BC extends BackendClass = BackendClass> {
implementation: BC; implementation: BC;
backendName: string; backendName: string;
@ -513,7 +529,12 @@ export class Backend<EF extends BaseField = UnknownField, BC extends BackendClas
const depth = collectionDepth(collection); const depth = collectionDepth(collection);
const extension = selectFolderEntryExtension(collection); const extension = selectFolderEntryExtension(collection);
return this.implementation return this.implementation
.allEntriesByFolder(collection.folder as string, extension, depth) .allEntriesByFolder(
collection.folder as string,
extension,
depth,
collectionRegex(collection),
)
.then(entries => this.processEntries(entries, collection)); .then(entries => this.processEntries(entries, collection));
} }

View File

@ -376,8 +376,8 @@ export default class GitGateway implements BackendClass {
async entriesByFolder(folder: string, extension: string, depth: number) { async entriesByFolder(folder: string, extension: string, depth: number) {
return this.backend!.entriesByFolder(folder, extension, depth); return this.backend!.entriesByFolder(folder, extension, depth);
} }
allEntriesByFolder(folder: string, extension: string, depth: number) { allEntriesByFolder(folder: string, extension: string, depth: number, pathRegex?: RegExp) {
return this.backend!.allEntriesByFolder(folder, extension, depth); return this.backend!.allEntriesByFolder(folder, extension, depth, pathRegex);
} }
entriesByFiles(files: ImplementationFile[]) { entriesByFiles(files: ImplementationFile[]) {
return this.backend!.entriesByFiles(files); return this.backend!.entriesByFiles(files);

View File

@ -272,14 +272,18 @@ export default class GitHub implements BackendClass {
return files; return files;
} }
async allEntriesByFolder(folder: string, extension: string, depth: number) { async allEntriesByFolder(folder: string, extension: string, depth: number, pathRegex?: RegExp) {
const repoURL = this.api!.originRepoURL; const repoURL = this.api!.originRepoURL;
const listFiles = () => const listFiles = () =>
this.api!.listFiles(folder, { this.api!.listFiles(folder, {
repoURL, repoURL,
depth, depth,
}).then(files => files.filter(file => filterByExtension(file, extension))); }).then(files =>
files.filter(
file => (!pathRegex || pathRegex.test(file.path)) && filterByExtension(file, extension),
),
);
const readFile = (path: string, id: string | null | undefined) => { const readFile = (path: string, id: string | null | undefined) => {
return this.api!.readFile(path, id, { repoURL }) as Promise<string>; return this.api!.readFile(path, id, { repoURL }) as Promise<string>;

View File

@ -554,6 +554,7 @@ export abstract class BackendClass {
folder: string, folder: string,
extension: string, extension: string,
depth: number, depth: number,
pathRegex?: RegExp,
): Promise<ImplementationEntry[]>; ): Promise<ImplementationEntry[]>;
abstract traverseCursor( abstract traverseCursor(
cursor: Cursor, cursor: Cursor,