@spscommerce/phrase-cli
Version:
A CLI used for interacting with phraseApp to keep translations up-to-date
86 lines (78 loc) • 2.37 kB
JavaScript
import fs from 'node:fs';
import { getLocales, setLocaleBooleans } from './phraseService/index.mjs';
import { readI18nJSON } from './utils/index.mjs';
import { taskUpdateTranslations } from './tasks/index.mjs';
const { default: Listr } = await import("listr");
const writeI18nJSON = (data) => {
const i18nFile = './i18n.json';
fs.writeFileSync(i18nFile, JSON.stringify(data, null, 2));
};
const writePhraseYaml = (data) => {
const phraseYamlFile = './.phrase.yml';
fs.writeFileSync(phraseYamlFile, data);
}
const initialize = new Listr([{
title: 'Initialize configuration for localization complete',
task: async (cli) => {
try {
const resp = await getLocales(cli.flags.projectId, cli.flags.accessToken);
const setConfig = new Listr([
{
title: 'Creating and populating i18n file',
task: () => {
const data = resp.reduce((accum, locale) => {
const { id, name } = locale;
return { ...accum, [name]: id };
}, {});
data.projectId = cli.flags.projectId;
writeI18nJSON(data);
}
},
{
title: 'Creating/Updating locales with translations',
task: async () => {
await taskUpdateTranslations(cli.flags.projectId, cli.flags.woodland, resp);
}
},
{
title: 'Updating default locale proofreading booleans',
task: async () => {
const data = readI18nJSON();
await setLocaleBooleans(cli.flags.projectId, data['en-US'], cli.flags.accessToken);
}
},
{
title: 'Creating Phrase Configuration file',
task: async () => {
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);
}
},
{
title: 'Configuration complete',
task: () => {}
}
]);
setConfig.run().catch((err) => {
console.error(err);
process.exit(1);
});
} catch (error) {
console.error(`error initializing: ${error}`);
process.exit(1);
}
}
}]);
export {
initialize
};