i18nexus-cli
Version:
Command line interface (CLI) for accessing the i18nexus API
291 lines (274 loc) • 7.88 kB
JavaScript
#!/usr/bin/env node
const pkg = require('../package.json');
const program = require('commander');
const pull = require('../commands/pull');
const addString = require('../commands/addString');
const updateString = require('../commands/updateString');
const deleteString = require('../commands/deleteString');
const importJson = require('../commands/importJson');
const addNamespace = require('../commands/addNamespace');
const listen = require('../commands/listen');
// Using Next's env variable loader because
// Next supports more than just one .env file
const { loadEnvConfig } = require('@next/env');
if (process.env.I18NEXUS_NO_DOT_ENV !== 'true') {
loadEnvConfig(process.cwd());
}
program.version(pkg.version);
program
.command('pull')
.description('Download all translations as .json files')
.requiredOption(
'-k, --api-key <apiKey>',
'The API key for your project',
process.env.I18NEXUS_API_KEY
)
.requiredOption(
'-v, --ver <version>',
'The version of the translations to be downloaded.',
process.env.I18NEXUS_VERSION || 'latest'
)
.option(
'-p, --path <path>',
'The path to the destination folder in which translation files will be downloaded'
)
.option(
'--confirmed',
'Only downloads confirmed translations (Cannot be used with `version`)',
false
)
.option(
'--clean',
'Removes and rebuilds destination folder before download',
false
)
.option(
'--compact',
'Output JSON without extra whitespace (default is pretty-printed)',
false
)
.action(options => {
pull({
apiKey: options.apiKey,
version: options.ver,
path: options.path,
clean: options.clean,
compact: options.compact,
confirmed: options.confirmed
});
});
program
.command('add-string')
.alias('a')
.description('Add a new base string to a namespace')
.requiredOption(
'-k, --api-key <apiKey>',
'The API key for your project',
process.env.I18NEXUS_API_KEY
)
.requiredOption(
'-t, --pat <personalAccessToken>',
'A personal access token generated for your account in i18nexus.',
process.env.I18NEXUS_PERSONAL_ACCESS_TOKEN
)
.requiredOption('-K, --key <stringKey>', 'The key of the string to create')
.requiredOption(
'-v, --value <stringValue>',
'The value of the string to create'
)
.option(
'-ns, --namespace <stringNamespace>',
'The namespace in which to create the string (Only required if your project uses namespaces and has more than one namespace)'
)
.option(
'-n, --notes <teamNotes>',
'Team notes about the string to create (optional)'
)
.option(
'-ai, --ai-instructions <stringAiInstructions>',
'Instructions/Context for AI machine translator (optional)'
)
.action(options => {
addString({
key: options.key,
value: options.value,
notes: options.notes,
namespace: options.namespace,
apiKey: options.apiKey,
pat: options.pat,
aiInstructions: options.aiInstructions
});
});
program
.command('update-string <namespace:key>')
.alias('u')
.description('Update a base string through PATCH request')
.requiredOption(
'-k, --api-key <apiKey>',
'The API key for your project',
process.env.I18NEXUS_API_KEY
)
.requiredOption(
'-t, --pat <personalAccessToken>',
'A personal access token generated for your account in i18nexus.',
process.env.I18NEXUS_PERSONAL_ACCESS_TOKEN
)
.option('-K, --key <stringKey>', 'The new key of the string')
.option('-v, --value <stringValue>', 'The new value of the string')
.option(
'-ns, --namespace <stringNamespace>',
'The new namespace of the string'
)
.option('-n, --notes <teamNotes>', 'The new team notes of the string')
.option(
'-ai, --ai-instructions <stringAiInstructions>',
'Instructions/Context for AI machine translator (optional)'
)
.option(
'--reset-confirmed',
'Reset confirmed translations of this string with machine translations.'
)
.option(
'--retain-confirmed',
'Do not reset confirmed translations of this string with machine translations.'
)
.action((nsKey, options) => {
let ns, key;
const split = nsKey.split(':');
if (split.length === 1) {
key = nsKey;
} else {
ns = split[0];
// in case key contains :
key = split.slice(1).join(':');
}
updateString({
id: {
namespace: ns,
key: key
},
key: options.key,
value: options.value,
notes: options.notes,
aiInstructions: options.aiInstructions,
namespace: options.namespace,
apiKey: options.apiKey,
pat: options.pat,
resetConfirmed: options.resetConfirmed,
retainConfirmed: options.retainConfirmed
});
});
program
.command('delete-string <namespace:key>')
.alias('d')
.description('Delete a base string and its translations')
.requiredOption(
'-k, --api-key <apiKey>',
'The API key for your project',
process.env.I18NEXUS_API_KEY
)
.requiredOption(
'-t, --pat <personalAccessToken>',
'A personal access token generated for your account in i18nexus.',
process.env.I18NEXUS_PERSONAL_ACCESS_TOKEN
)
.action((nsKey, options) => {
let ns, key;
const split = nsKey.split(':');
if (split.length === 1) {
key = nsKey;
} else {
ns = split[0];
// in case key contains :
key = split.slice(1).join(':');
}
deleteString({
id: {
namespace: ns,
key: key
},
apiKey: options.apiKey,
pat: options.pat
});
});
program
.command('import <filePath>')
.description(
'Import base strings into your i18nexus project from a local JSON file.'
)
.requiredOption(
'-k, --api-key <apiKey>',
'The API key for your project',
process.env.I18NEXUS_API_KEY
)
.requiredOption(
'-t, --pat <personalAccessToken>',
'A personal access token generated for your account in i18nexus.',
process.env.I18NEXUS_PERSONAL_ACCESS_TOKEN
)
.option(
'-ns, --namespace <namespace>',
'The namespace in which your strings will be imported. (Only required if your project uses namespaces and has more than one namespace)'
)
.option(
'--overwrite',
'If any keys already exist in the target namespace, overwrite the values with the imported values.',
false
)
.action((filePath, options) => {
importJson({
apiKey: options.apiKey,
path: filePath,
overwrite: options.overwrite,
namespace: options.namespace,
pat: options.pat
});
});
program
.command('add-namespace <namespaceTitle>')
.alias('a-ns')
.description('Add a new namespace to your project')
.requiredOption(
'-k, --api-key <apiKey>',
'The API key for your project',
process.env.I18NEXUS_API_KEY
)
.requiredOption(
'-t, --pat <personalAccessToken>',
'A personal access token generated for your account in i18nexus',
process.env.I18NEXUS_PERSONAL_ACCESS_TOKEN
)
.action((namespaceTitle, options) => {
addNamespace({
title: namespaceTitle,
apiKey: options.apiKey,
pat: options.pat
});
});
program
.command('listen')
.description(
'Register a live webhook for local development and setup tunnel to listen for updates to translations.'
)
.requiredOption(
'-k, --api-key <apiKey>',
'The API key for your project',
process.env.I18NEXUS_API_KEY
)
.option(
'-p, --path <path>',
'The path to the destination folder in which translation files will be downloaded'
)
.option(
'--compact',
'Output JSON without extra whitespace (default is pretty-printed)',
false
)
.action(options => {
listen({
apiKey: options.apiKey,
path: options.path,
compact: options.compact
});
});
program.parse(process.argv);