@spscommerce/phrase-cli
Version:
A CLI used for interacting with phraseApp to keep translations up-to-date
132 lines (120 loc) • 3.19 kB
JavaScript
/* eslint-disable no-console */
import { createLocales, getLocales } from '../phraseService/index.mjs';
import { readI18nJSON } from '../utils/index.mjs';
import {
taskUpdateTranslations,
taskListTranslations,
taskCheckPR,
taskCheckBuild,
} from '../tasks/index.mjs';
import fs from 'node:fs';
const { default: Listr } = await import("listr");
const newLocales = new Listr([{
title: 'Create new locale',
task: async (cli) => {
const i18json = readI18nJSON();
const body = {
name: cli.flags.languageCode.split('-')[0],
code: cli.flags.languageCode,
unverify_new_translations: true
};
const pId = cli.flags.projectId ? cli.flags.projectId : i18json.projectId;
try {
const resp = await createLocales(pId, body, cli.flags.accessToken);
return resp;
} catch (error) {
console.error(`phraseActions.newLocales error: ${error}, ProjectId: ${pId}`);
process.exit(1);
}
}
}]);
const listTranslations = new Listr([{
title: 'Get translations from project',
task: async (cli) => {
try {
const resp = await taskListTranslations(cli);
console.log('Prase Translations: ', resp);
} catch (error) {
if (error) {
console.error(`Error getting translations: ${error}`);
}
}
process.exit(1);
}
}]);
const updateTranslations = new Listr([{
title: 'Creating/Updating locales with translations',
task: async (cli) => {
try {
const i18json = readI18nJSON();
const pId = cli.flags.projectId ? cli.flags.projectId : i18json.projectId;
const resp = await getLocales(pId, cli.flags.accessToken);
await taskUpdateTranslations(pId, resp);
} catch (error) {
if (error) {
console.error(`Error creating/updating translations: ${error}`);
}
process.exit(1);
}
}
}]);
const checkPR = new Listr([{
title: 'Checking Translations for PR',
task: (cli) => {
taskCheckPR(cli.flags).catch(() => {
process.exit(1);
});
}
}]);
const checkBuild = new Listr([{
title: 'Checking Translations for Build',
task: (cli) => {
taskCheckBuild(cli).catch(() => {
process.exit(1);
});
}
}]);
const runCheck = new Listr([{
title: 'Running checks:',
task: (cli) => {
if (process.env.MERGE_TO_MAIN && process.env.MERGE_TO_MAIN.toLowerCase() === 'true') {
checkBuild.run(cli).catch(() => {
process.exit(1);
});
} else {
checkPR.run(cli).catch(() => {
process.exit(1);
});
}
}
}]);
const addPhraseConfig = new Listr([{
title: "Creating Phrase configuration file",
task: (cli) => {
const writePhraseYaml = (data) => {
const phraseYamlFile = './.phrase.yml';
fs.writeFileSync(phraseYamlFile, data);
}
const yaml = `
phrase:
project_id: ${cli.flags.projectId}
file_format: simple_json
push:
sources:
- file: public/locales/<locale_code>/translation.json
pull:
targets:
- file: public/locales/<locale_code>/translation.json
`;
writePhraseYaml(yaml);
}
}]);
export {
checkPR,
checkBuild,
listTranslations,
newLocales,
runCheck,
updateTranslations,
addPhraseConfig
};