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
4 changed files with 24 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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