feat: filter entry list from github before reading files (#868)
This commit is contained in:
parent
f5f79b38f8
commit
ae5e261da5
@ -15,6 +15,7 @@ import {
|
||||
getI18nEntry,
|
||||
getI18nFiles,
|
||||
getI18nFilesDepth,
|
||||
getI18nInfo,
|
||||
groupEntries,
|
||||
hasI18n,
|
||||
} from './lib/i18n';
|
||||
@ -292,6 +293,21 @@ function collectionDepth<EF extends BaseField>(collection: Collection<EF>) {
|
||||
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> {
|
||||
implementation: BC;
|
||||
backendName: string;
|
||||
@ -513,7 +529,12 @@ export class Backend<EF extends BaseField = UnknownField, BC extends BackendClas
|
||||
const depth = collectionDepth(collection);
|
||||
const extension = selectFolderEntryExtension(collection);
|
||||
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));
|
||||
}
|
||||
|
||||
|
@ -376,8 +376,8 @@ export default class GitGateway implements BackendClass {
|
||||
async entriesByFolder(folder: string, extension: string, depth: number) {
|
||||
return this.backend!.entriesByFolder(folder, extension, depth);
|
||||
}
|
||||
allEntriesByFolder(folder: string, extension: string, depth: number) {
|
||||
return this.backend!.allEntriesByFolder(folder, extension, depth);
|
||||
allEntriesByFolder(folder: string, extension: string, depth: number, pathRegex?: RegExp) {
|
||||
return this.backend!.allEntriesByFolder(folder, extension, depth, pathRegex);
|
||||
}
|
||||
entriesByFiles(files: ImplementationFile[]) {
|
||||
return this.backend!.entriesByFiles(files);
|
||||
|
@ -272,14 +272,18 @@ export default class GitHub implements BackendClass {
|
||||
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 listFiles = () =>
|
||||
this.api!.listFiles(folder, {
|
||||
repoURL,
|
||||
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) => {
|
||||
return this.api!.readFile(path, id, { repoURL }) as Promise<string>;
|
||||
|
@ -554,6 +554,7 @@ export abstract class BackendClass {
|
||||
folder: string,
|
||||
extension: string,
|
||||
depth: number,
|
||||
pathRegex?: RegExp,
|
||||
): Promise<ImplementationEntry[]>;
|
||||
abstract traverseCursor(
|
||||
cursor: Cursor,
|
||||
|
Loading…
x
Reference in New Issue
Block a user