@spscommerce/phrase-cli
Version:
A CLI used for interacting with phraseApp to keep translations up-to-date
140 lines (128 loc) • 3.5 kB
JavaScript
/* eslint-disable no-console */
import dotenv from 'dotenv';
import meow from 'meow';
import { initialize } from './init.mjs';
import {
listTranslations,
newLocales,
checkPR,
checkBuild,
runCheck,
updateTranslations,
addPhraseConfig
} from './phraseActions/index.mjs';
dotenv.config();
const getCommand = (type) => {
const actionMap = {
init: initialize,
checkPR,
checkBuild,
listTranslations,
newLocales,
runCheck,
updateTranslations,
addPhraseConfig
};
return actionMap[type];
};
const cli = meow(
`
Usage
$ pnpm exec @spscommerce/phrase-cli <command>
Available Commands:
init Creates i18n.json file with project_id and locale Ids to work with phraseApp
runCheck Check runs checkPR by default, if MERGE_TO_MAIN=true, runs checkBuild
checkPR Check current translations on PR, checks for new and updated keys
checkBuild Check current translations, checks for new and updated keys, pushes new keys on merge to main
newLocales Create a new locale
listTranslations List translations from project
updateTranslations Update locale translation.json files
addPhraseConfig Creates .phrase.yml file
Flags:
-p, --project_id string phrase app project ID
-t, --access_token access token used for authentication
-c, --language_code language code for creating new locale
-l, --locale locale name for language in project: en-US
--tag tag for the phrase translations
-w, --woodland changes the file locations in commands (should only be used by Woodland)
Examples
$ pnpm exec @spscommerce/phrase-cli init
🌈 generated docs/toc.json 🌈
`,
{
importMeta: import.meta,
flags: {
projectId: {
type: 'string',
shortFlag: 'p',
description: 'PhraseApp project ID',
isRequired: (flags, input) => {
if (input[0] === 'init') {
return true;
}
return false;
}
},
accessToken: {
type: 'string',
shortFlag: 't'
},
languageCode: {
type: 'string',
shortFlag: 'c',
isRequired: (flags, input) => {
if (input[0] === 'newLocales') {
return true;
}
return false;
}
},
localeName: {
type: 'string',
shortFlag: 'l',
isRequired: (flags, input) => {
if (input[0] === 'listTranslations') {
return true;
}
return false;
}
},
tag: {
type: 'string'
},
woodland: {
type: 'boolean',
shortFlag: 'w',
description: 'use Woodland specific file locations',
}
}
}
);
if (!cli.input[0]) {
cli.showHelp();
}
if (cli.flags && cli.flags.tag) {
process.env.PHRASE_TAG = cli.flags.tag;
}
const commands = [
'init',
'runCheck',
'checkPR',
'checkBuild',
'newLocales',
'listTranslations',
'updateTranslations',
'addPhraseConfig'
];
if (commands.indexOf(cli.input[0]) > -1) {
const action = getCommand(cli.input[0]);
if (action) {
action.run(cli);
console.log(`Successfully handled action: ${cli.input[0]}`);
} else {
console.log(`Action not being captured: ${cli.input[0]}`);
}
} else {
cli.showHelp();
}