vvc
Version:
Vivocha Command Line Tools
187 lines • 8.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const scopes_1 = require("@vivocha/scopes");
const program = require("commander");
const fs = require("fs");
const jsonpolice = require("jsonpolice");
const path_1 = require("path");
const util_1 = require("util");
const config_1 = require("./lib/config");
const startup_1 = require("./lib/startup");
const strings_1 = require("./lib/strings");
const ws_1 = require("./lib/ws");
const access = util_1.promisify(fs.access);
(async () => {
try {
await startup_1.checkLoginAndVersion();
const config = await config_1.read();
program.version(config_1.meta.version).option('-v, --verbose', 'Verbose output');
const commands = {
push: program
.command('push <strings_json_file> [other_json_files...]')
.description('Push a new version of the strings to the Vivocha servers')
.action(async (strings_json_file, other_json_files, options) => {
let exitCode = 0;
let files;
if (other_json_files) {
files = [strings_json_file, ...other_json_files];
}
else {
files = [strings_json_file];
}
try {
let schemaUrl = await ws_1.wsUrl('schemas/string');
let parser = await jsonpolice.create({
type: 'array',
items: { $ref: schemaUrl },
minItems: 2
}, {
scope: `${schemaUrl}/array`,
retriever: ws_1.retriever
});
for (let f of files) {
await access(f, fs.constants.R_OK).catch(() => {
throw 'file not found';
});
// load strings.json
const strings = await new Promise(resolve => {
const raw = fs.readFileSync(f).toString('utf8');
resolve(JSON.parse(raw));
}).catch(() => {
throw 'Failed to parse file';
});
// get the strings schema
// validate the strings
await parser.validate(strings, { context: 'write' }).catch(err => {
throw `invalid format of strings.json, ${err.message} ${err.path || ''}`;
});
await strings_1.uploadStringChanges(strings, options.global);
}
}
catch (e) {
console.error(e);
exitCode = 1;
}
process.exit(exitCode);
}),
pull: program
.command('pull')
.description('Pull strings from the Vivocha servers to stdout')
.option('-p, --prefix <strings prefix>', 'Pull only the strings starting with prefix', '')
.action(async (options) => {
let exitCode = 0;
try {
const strings = await strings_1.fetchStrings(options.prefix, options.global).catch(() => {
throw 'Failed to download the strings';
});
process.stdout.write(JSON.stringify(strings, null, 2) + '\n');
}
catch (e) {
console.error(e);
exitCode = 1;
}
finally {
process.exit(exitCode);
}
}),
import: program
.command('import <po_file> [other_po_files...]')
.description('Import gettext formatted strings and convert them into the Vivocha format')
.option('-p, --prefix <strings prefix>', 'Import only the strings starting with prefix', '')
.option('-m, --merge <json to merge>', 'Merge existing strings into the output file', '')
.action(async (po_file, other_po_files, options) => {
let exitCode = 0;
let files;
if (other_po_files) {
files = [po_file, ...other_po_files];
}
else {
files = [po_file];
}
try {
const mergeTo = options.merge ? JSON.parse(fs.readFileSync(options.merge, 'utf8')) : [];
const strings = (await strings_1.importPOFiles(files, mergeTo, options.prefix)).sort((a, b) => a.id.localeCompare(b.id));
process.stdout.write(JSON.stringify(strings, null, 2) + '\n');
}
catch (e) {
console.error(e);
exitCode = 1;
}
finally {
process.exit(exitCode);
}
}),
export: program
.command('export <source_language> <strings_json_file> [other_json_files...]')
.description('Export Vivocha strings to gettext formatted strings')
.option('-l, --language <name>', 'Export only the specified language', '')
.option('-r, --reference <language>', 'Reference translation to include in exported files', '')
.option('-p, --prefix <strings prefix>', 'Export only the strings starting with prefix', '')
.option('-P, --path <output path>', 'Use the specified path/prefix when exporting', './')
.option('-b, --basename <output basename>', 'Use the specified basename when exporting', '')
.option('-i, --project-id <id>', 'Set the project id', '')
.action(async (source_language, strings_json_file, other_json_files, options) => {
let exitCode = 0;
let files;
if (other_json_files) {
files = [strings_json_file, ...other_json_files];
}
else {
files = [strings_json_file];
}
try {
let basename = options.path;
if (options.basename) {
basename += options.basename;
}
else if (files.length === 1) {
basename += `${path_1.parse(files[0]).name}`;
}
else {
basename += 'exported';
}
let project = options.projectId;
if (!project) {
if (files.length === 1) {
project = path_1.parse(files[0]).name;
}
else {
project = 'vivocha';
}
}
await strings_1.exportPOFiles(source_language, files, project, options.language, options.prefix, basename, options.reference);
}
catch (e) {
console.error(e);
exitCode = 1;
}
finally {
process.exit(exitCode);
}
})
};
if (config.info.scopes) {
const scopes = new scopes_1.Scopes(config.info.scopes);
if (scopes.match('String.global')) {
commands.push.option('-g, --global', 'Push as global strings');
commands.pull.option('-g, --global', 'Pull a global version of the requested strings');
}
}
program.parse(process.argv);
// commander 4.0.0 uses rawArgs instead of args to read cli arguments
if (!program.rawArgs.length) {
program.help();
}
}
catch (err) {
if (program.verbose) {
console.error(err);
}
else {
console.error('Failed');
}
process.exit(1);
}
})();
//# sourceMappingURL=vvc-strings.js.map