feat: Crowdin integration scripts/actions (#40657)

This commit is contained in:
Randell Dawson
2021-01-12 11:20:54 -07:00
committed by GitHub
parent ab222e31e7
commit 0095583028
16 changed files with 670 additions and 69 deletions

View File

@@ -0,0 +1,74 @@
const authHeader = require('./auth-header');
const makeRequest = require('./make-request');
const isHeading = str => /\h\d/.test(str);
const isCode = str => /^\/pre\/code|\/code$/.test(str);
const shouldHide = (text, context, challengeTitle) => {
if (isHeading(context) || isCode(context)) {
return true;
}
return text !== challengeTitle && context.includes('id=front-matter');
};
const getStrings = async ({ projectId, fileId }) => {
let headers = { ...authHeader };
const endPoint = `projects/${projectId}/strings?fileId=${fileId}&limit=500`;
const strings = await makeRequest({ method: 'get', endPoint, headers });
if (strings.data) {
return strings.data;
} else {
const { error, errors } = strings;
console.error(error ? error : errors);
return null;
}
};
const updateString = async ({ projectId, stringId, propsToUpdate }) => {
let headers = { ...authHeader };
const endPoint = `projects/${projectId}/strings/${stringId}`;
const body = propsToUpdate.map(({ path, value }) => ({
op: 'replace',
path,
value
}));
await makeRequest({
method: 'patch',
endPoint,
headers,
body
});
};
const changeHiddenStatus = async (projectId, stringId, newStatus) => {
await updateString({
projectId,
stringId,
propsToUpdate: [{ path: '/isHidden', value: newStatus }]
});
};
const updateFileStrings = async ({ projectId, fileId, challengeTitle }) => {
const fileStrings = await getStrings({
projectId,
fileId
});
for (let {
data: { id: stringId, text, isHidden, context }
} of fileStrings) {
const hideString = shouldHide(text, context, challengeTitle);
if (!isHidden && hideString) {
changeHiddenStatus(projectId, stringId, true);
} else if (isHidden && !hideString) {
changeHiddenStatus(projectId, stringId, false);
}
}
};
module.exports = {
getStrings,
changeHiddenStatus,
updateString,
updateFileStrings
};