fix(proxy-server): better handle files based collections (#3237)

This commit is contained in:
Erez Rokah 2020-02-12 09:56:42 +02:00 committed by GitHub
parent 19fdc161d8
commit 3678053f0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 18 deletions

View File

@ -57,7 +57,7 @@ export const defaultSchema = ({ path = requiredString } = {}) => {
is: 'entriesByFiles',
then: defaultParams.keys({
files: Joi.array()
.items(Joi.object({ path }))
.items(Joi.object({ path, label: Joi.string() }))
.required(),
}),
},

View File

@ -38,23 +38,23 @@ export const localFsMiddleware = ({ repoPath }: Options) => {
const payload = body.params as EntriesByFolderParams;
const { folder, extension, depth } = payload;
const entries = await listRepoFiles(repoPath, folder, extension, depth).then(files =>
entriesFromFiles(repoPath, files),
entriesFromFiles(
repoPath,
files.map(file => ({ path: file })),
),
);
res.json(entries);
break;
}
case 'entriesByFiles': {
const payload = body.params as EntriesByFilesParams;
const entries = await entriesFromFiles(
repoPath,
payload.files.map(file => file.path),
);
const entries = await entriesFromFiles(repoPath, payload.files);
res.json(entries);
break;
}
case 'getEntry': {
const payload = body.params as GetEntryParams;
const [entry] = await entriesFromFiles(repoPath, [payload.path]);
const [entry] = await entriesFromFiles(repoPath, [{ path: payload.path }]);
res.json(entry);
break;
}

View File

@ -98,7 +98,7 @@ const entriesFromDiffs = async (
);
const entryPath = data.metaData.objects.entry.path;
const [entry] = await runOnBranch(git, cmsBranch, () =>
entriesFromFiles(repoPath, [entryPath]),
entriesFromFiles(repoPath, [{ path: entryPath }]),
);
const rawDiff = await git.diff([branch, cmsBranch, '--', entryPath]);
@ -218,7 +218,10 @@ export const localGitMiddleware = ({ repoPath }: Options) => {
const { folder, extension, depth } = payload;
const entries = await runOnBranch(git, branch, () =>
listRepoFiles(repoPath, folder, extension, depth).then(files =>
entriesFromFiles(repoPath, files),
entriesFromFiles(
repoPath,
files.map(file => ({ path: file })),
),
),
);
res.json(entries);
@ -227,10 +230,7 @@ export const localGitMiddleware = ({ repoPath }: Options) => {
case 'entriesByFiles': {
const payload = body.params as EntriesByFilesParams;
const entries = await runOnBranch(git, branch, () =>
entriesFromFiles(
repoPath,
payload.files.map(file => file.path),
),
entriesFromFiles(repoPath, payload.files),
);
res.json(entries);
break;
@ -238,7 +238,7 @@ export const localGitMiddleware = ({ repoPath }: Options) => {
case 'getEntry': {
const payload = body.params as GetEntryParams;
const [entry] = await runOnBranch(git, branch, () =>
entriesFromFiles(repoPath, [payload.path]),
entriesFromFiles(repoPath, [{ path: payload.path }]),
);
res.json(entry);
break;

View File

@ -12,17 +12,23 @@ const sha256 = (buffer: Buffer) => {
// normalize windows os path format
const normalizePath = (path: string) => path.replace(/\\/g, '/');
export const entriesFromFiles = async (repoPath: string, files: string[]) => {
export const entriesFromFiles = async (
repoPath: string,
files: { path: string; label?: string }[],
) => {
return Promise.all(
files.map(async file => {
try {
const content = await fs.readFile(path.join(repoPath, file));
const content = await fs.readFile(path.join(repoPath, file.path));
return {
data: content.toString(),
file: { path: normalizePath(file), id: sha256(content) },
file: { path: normalizePath(file.path), id: sha256(content) },
};
} catch (e) {
return { data: null, file: { path: file, id: null } };
return {
data: null,
file: { path: normalizePath(file.path), label: file.label, id: null },
};
}
}),
);