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:
@ -4,31 +4,47 @@ 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 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 => {
|
const hideNonTranslatedStrings = async projectId => {
|
||||||
console.log('start hiding non-translated strings...');
|
console.log('hide non-translated strings...');
|
||||||
const crowdinFiles = await getFiles(projectId);
|
const crowdinFiles = await getFiles(projectId);
|
||||||
if (crowdinFiles && crowdinFiles.length) {
|
if (crowdinFiles && crowdinFiles.length) {
|
||||||
for (let { fileId, path: crowdinFilePath } of crowdinFiles) {
|
const challengeTitleLookup = crowdinFiles.reduce(
|
||||||
const challengeFilePath = path.join(
|
createChallengeTitleLookup,
|
||||||
__dirname,
|
{}
|
||||||
'/../../../../',
|
);
|
||||||
crowdinFilePath
|
const crowdinStrings = await getStrings({ projectId });
|
||||||
);
|
if (crowdinStrings && crowdinStrings.length) {
|
||||||
try {
|
for (let string of crowdinStrings) {
|
||||||
const challengeContent = fs.readFileSync(challengeFilePath);
|
const challengeTitle = challengeTitleLookup[string.data.fileId];
|
||||||
const {
|
await updateFileString({ projectId, string, challengeTitle });
|
||||||
data: { title: challengeTitle }
|
|
||||||
} = matter(challengeContent);
|
|
||||||
await updateFileStrings({ projectId, fileId, challengeTitle });
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err.name);
|
|
||||||
console.log(err.message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log('hiding non-translated strings complete');
|
console.log('complete');
|
||||||
};
|
};
|
||||||
|
|
||||||
const projectId = process.env.CROWDIN_PROJECT_ID;
|
const projectId = process.env.CROWDIN_PROJECT_ID;
|
||||||
|
@ -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
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user