Skip to content

Commit

Permalink
perf(): filter by path when loading collection from github backend (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
demshy committed Aug 31, 2023
1 parent 7b74b68 commit 18ef773
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/decap-cms-backend-git-gateway/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ export default class GitGateway implements Implementation {
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);
Expand Down
8 changes: 6 additions & 2 deletions packages/decap-cms-backend-github/src/implementation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,18 @@ export default class GitHub implements Implementation {
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>;
Expand Down
29 changes: 28 additions & 1 deletion packages/decap-cms-core/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
getI18nDataFiles,
getI18nBackup,
formatI18nBackup,
getI18nInfo,
} from './lib/i18n';

import type AssetProxy from './valueObjects/AssetProxy';
Expand Down Expand Up @@ -307,6 +308,27 @@ function collectionDepth(collection: Collection) {
return depth;
}

function collectionRegex(collection: Collection): RegExp | undefined {
console.log('collection', collection.toJS());
let ruleString = '';

if (collection.get('path')) {
ruleString = `${collection.get('folder')}/${collection.get('path')}`.replace(
/{{.*}}/gm,
'(.*)',
);
}

if (hasI18n(collection)) {
const { defaultLocale } = getI18nInfo(collection) as { defaultLocale: string };
ruleString += `\\.${defaultLocale}\\..*`;
}

console.log('ruleString', ruleString);

return ruleString ? new RegExp(ruleString) : undefined;
}

export class Backend {
implementation: Implementation;
backendName: string;
Expand Down Expand Up @@ -552,7 +574,12 @@ export class Backend {
const depth = collectionDepth(collection);
const extension = selectFolderEntryExtension(collection);
return this.implementation
.allEntriesByFolder(collection.get('folder') as string, extension, depth)
.allEntriesByFolder(
collection.get('folder') as string,
extension,
depth,
collectionRegex(collection),
)
.then(entries => this.processEntries(entries, collection));
}

Expand Down
6 changes: 5 additions & 1 deletion packages/decap-cms-core/src/lib/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,11 @@ function mergeValues(
return entryValue;
}

function mergeSingleFileValue(entryValue: EntryValue, defaultLocale: string, locales: string[]) {
function mergeSingleFileValue(
entryValue: EntryValue,
defaultLocale: string,
locales: string[],
): EntryValue {
const data = entryValue.data[defaultLocale] || {};
const i18n = locales
.filter(l => l !== defaultLocale)
Expand Down
1 change: 1 addition & 0 deletions packages/decap-cms-lib-util/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export interface Implementation {
folder: string,
extension: string,
depth: number,
pathRegex?: RegExp,
) => Promise<ImplementationEntry[]>;
traverseCursor?: (
cursor: Cursor,
Expand Down

0 comments on commit 18ef773

Please sign in to comment.