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,31 +4,47 @@ const fs = require('fs');
const path = require('path');
const matter = require('gray-matter');
const { getFiles } = require('../../utils/files');
const { updateFileStrings } = require('../../utils/strings');
const { getStrings, updateFileString } = require('../../utils/strings');
const createChallengeTitleLookup = (
lookup,
{ fileId, path: crowdinFilePath }
) => {
const challengeFilePath = path.join(
__dirname,
'/../../../../',
crowdinFilePath
);
try {
const challengeContent = fs.readFileSync(challengeFilePath);
const {
data: { title: challengeTitle }
} = matter(challengeContent);
return { ...lookup, [fileId]: challengeTitle };
} catch (err) {
console.log(err.name);
console.log(err.message);
}
return lookup;
};
const hideNonTranslatedStrings = async projectId => {
console.log('start hiding non-translated strings...');
console.log('hide non-translated strings...');
const crowdinFiles = await getFiles(projectId);
if (crowdinFiles && crowdinFiles.length) {
for (let { fileId, path: crowdinFilePath } of crowdinFiles) {
const challengeFilePath = path.join(
__dirname,
'/../../../../',
crowdinFilePath
);
try {
const challengeContent = fs.readFileSync(challengeFilePath);
const {
data: { title: challengeTitle }
} = matter(challengeContent);
await updateFileStrings({ projectId, fileId, challengeTitle });
} catch (err) {
console.log(err.name);
console.log(err.message);
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;

View File

@ -13,7 +13,10 @@ const shouldHide = (text, context, challengeTitle) => {
const getStrings = async ({ projectId, fileId }) => {
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 });
if (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 = {
getStrings,
changeHiddenStatus,
updateString,
updateFileStrings
updateFileStrings,
updateFileString
};