fix(tools) Improve efficiency of the Hide Non-translated Strings GitHub action (#40721)

* fix: changed update strings logic

* fix: pull all strings instead of strings by file

* fix: changed console.log message
This commit is contained in:
Randell Dawson
2021-01-18 11:20:29 -07:00
committed by GitHub
parent 8d12a0dae9
commit f37dd6ff84
2 changed files with 58 additions and 21 deletions

View File

@ -4,13 +4,12 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const matter = require('gray-matter'); const matter = require('gray-matter');
const { getFiles } = require('../../utils/files'); const { getFiles } = require('../../utils/files');
const { updateFileStrings } = require('../../utils/strings'); const { getStrings, updateFileString } = require('../../utils/strings');
const hideNonTranslatedStrings = async projectId => { const createChallengeTitleLookup = (
console.log('start hiding non-translated strings...'); lookup,
const crowdinFiles = await getFiles(projectId); { fileId, path: crowdinFilePath }
if (crowdinFiles && crowdinFiles.length) { ) => {
for (let { fileId, path: crowdinFilePath } of crowdinFiles) {
const challengeFilePath = path.join( const challengeFilePath = path.join(
__dirname, __dirname,
'/../../../../', '/../../../../',
@ -21,14 +20,31 @@ const hideNonTranslatedStrings = async projectId => {
const { const {
data: { title: challengeTitle } data: { title: challengeTitle }
} = matter(challengeContent); } = matter(challengeContent);
await updateFileStrings({ projectId, fileId, challengeTitle }); return { ...lookup, [fileId]: challengeTitle };
} catch (err) { } catch (err) {
console.log(err.name); console.log(err.name);
console.log(err.message); console.log(err.message);
} }
return lookup;
};
const hideNonTranslatedStrings = async projectId => {
console.log('hide non-translated strings...');
const crowdinFiles = await getFiles(projectId);
if (crowdinFiles && crowdinFiles.length) {
const challengeTitleLookup = crowdinFiles.reduce(
createChallengeTitleLookup,
{}
);
const crowdinStrings = await getStrings({ projectId });
if (crowdinStrings && crowdinStrings.length) {
for (let string of crowdinStrings) {
const challengeTitle = challengeTitleLookup[string.data.fileId];
await updateFileString({ projectId, string, challengeTitle });
} }
} }
console.log('hiding non-translated strings complete'); }
console.log('complete');
}; };
const projectId = process.env.CROWDIN_PROJECT_ID; const projectId = process.env.CROWDIN_PROJECT_ID;

View File

@ -13,7 +13,10 @@ const shouldHide = (text, context, challengeTitle) => {
const getStrings = async ({ projectId, fileId }) => { const getStrings = async ({ projectId, fileId }) => {
let headers = { ...authHeader }; let headers = { ...authHeader };
const endPoint = `projects/${projectId}/strings?fileId=${fileId}&limit=500`; let endPoint = `projects/${projectId}/strings?limit=500`;
if (fileId) {
endPoint += `&fileId=${fileId}`;
}
const strings = await makeRequest({ method: 'get', endPoint, headers }); const strings = await makeRequest({ method: 'get', endPoint, headers });
if (strings.data) { if (strings.data) {
return strings.data; return strings.data;
@ -66,9 +69,27 @@ const updateFileStrings = async ({ projectId, fileId, challengeTitle }) => {
} }
}; };
const updateFileString = async ({ projectId, string, challengeTitle }) => {
const {
data: { id: stringId, text, isHidden, context }
} = string;
const hideString = shouldHide(text, context, challengeTitle);
if (!isHidden && hideString) {
await changeHiddenStatus(projectId, stringId, true);
console.log(
`${challengeTitle} - stringId: ${stringId} - changed isHidden to true`
);
} else if (isHidden && !hideString) {
await changeHiddenStatus(projectId, stringId, false);
console.log(
`${challengeTitle} - stringId: ${stringId} - changed isHidden to false`
);
}
};
module.exports = { module.exports = {
getStrings, getStrings,
changeHiddenStatus,
updateString, updateString,
updateFileStrings updateFileStrings,
updateFileString
}; };