Improve file search algorithm

This commit is contained in:
ErickSkrauch 2020-06-15 00:22:19 +03:00
parent b25e888781
commit 7bd45e7ada
2 changed files with 12 additions and 7 deletions

View File

@ -1,8 +1,6 @@
/* eslint-env node */
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
require('dotenv').config();
const { env } = process;
@ -17,7 +15,7 @@ module.exports = {
projectId: 350687,
filePath: 'accounts/site.json',
sourceLang: 'en',
basePath: path.resolve(`${__dirname}/packages/app/i18n`),
basePath: `${__dirname}/packages/app/i18n`,
minApproved: 80, // Minimal ready percent before translation can be published
},
};

View File

@ -132,12 +132,19 @@ async function findFileId(filePath: string, branchId?: number): Promise<number |
directoryId = (await findDirectoryId(dirPath, branchId)) || null;
}
// We're receiving files list without branch filter until https://github.com/crowdin/crowdin-api-client-js/issues/63
// will be resolved. But right now it doesn't matter because for each branch directories will have its own ids,
// so if the file is stored into the some directory, algorithm will find correct file.
const { data: filesResponse } = await crowdin.sourceFilesApi.listProjectFiles(PROJECT_ID /*, branchId*/);
// When the branchId and directoryId aren't specified the list returned recursively,
// but if one of this param is specified, the list contains only the specified parent.
//
// Directories ids for each branch are different, so directoryId is already ensures
// that the file will be searched in the correct branch
const { data: filesResponse } = await crowdin.sourceFilesApi.listProjectFiles(
PROJECT_ID,
directoryId === null ? branchId : undefined,
directoryId || undefined, // directory ID can't be 0, but can be null
);
const files = filesResponse.map((fileData) => fileData.data);
// Compare directoryId since when the file is placed in the root
return files.find((file) => file.directoryId === directoryId && file.name === fileName)?.id;
}