@uitranslator/cli
Version:
The UI Translator npm cli
592 lines (591 loc) • 33.8 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const yargs = __importStar(require("yargs"));
const fs = __importStar(require("fs"));
const axios = __importStar(require("axios"));
const watcher = __importStar(require("node-watch"));
const child_process_1 = require("child_process");
const Colors = require("colors.ts");
const figlet_1 = __importDefault(require("figlet"));
const prompts_1 = require("@inquirer/prompts");
const packageInfo = require('../package.json');
const configFilePath = 'uit.config.json';
const projectConfigFilePath = 'uit.project.json';
let configFileExists = fs.existsSync(configFilePath);
let config = {};
let draftSourceFilePath = '';
let draftSourceFileExists = false;
let sourceTranslationFilePath = '';
let sourceTranslationExists = false;
let seflSavingNoSync = false;
Colors.colors("", "");
let watching = false;
const argv = yargs
.usage('Usage: uit Command [Options]'.green)
.command('setup'.blue, 'This command helps you getting setup to sync your content on the UI Translator.\n')
.command('sync'.blue, 'Helps you sync the local project with the remote one, mainly tought for bringing new remote content locally.\n')
.command('watch'.blue, 'Keep syncing your content as you create it with the UI Translator, mainly tought for when you are actively developing.\n')
.options('locale', { alias: 'l', type: 'string', desc: 'The locale to load for testing purpose, should be part of the project translations' })
.options('time', { alias: 't', type: 'string', desc: 'The time from which to sync content in if it was added by the current user' })
.options('skip-approval', { alias: 's', type: 'boolean', desc: 'Allow release of unapproved content.' })
.command('test'.blue + ' <locale>'.yellow, "Load a translation from the server to test the layout, this will load the translation even if it's not reviewed\n")
.command('release'.blue, 'Create a frozen version of the current content.\n')
.help()
.parseSync();
if (argv._.length == 0) {
withHeader(() => {
yargs.showHelp();
});
}
else if (argv._[0] == 'setup') {
init();
}
else if (argv._[0] == 'sync') {
sync();
}
else if (argv._[0] == 'watch') {
watch();
}
else if (argv._[0] == 'test') {
if (configFileExists) {
if (!argv.locale) {
getDraft(null);
}
else {
getDraft(argv.locale);
}
}
else {
withHeader(() => {
console.log(` Please run 'uit setup' first. `.bg_red + '\n\n');
});
}
}
else if (argv._[0] == 'release') {
if (configFileExists) {
deploy(argv.skipApproval);
}
else {
withHeader(() => {
console.log(` Please run 'uit setup' first. `.bg_red + '\n\n');
});
}
}
else {
withHeader(() => {
unrecognized();
});
}
function withHeader(cb) {
(0, figlet_1.default)('UI Translator', (err, data) => {
console.log(data);
console.log("Welcome to a new era of software localization!".blue + " version: " + packageInfo.version + "\n\n");
if (cb)
cb();
});
}
function unrecognized() {
console.log(argv._[0].toString().red + " is not a uit command. type " + "uit --help ".blue + "to see the available ones.\n");
}
function watch() {
watching = true;
if (configFileExists) {
checkConfiguration(true, () => {
console.log(`\nListening to changes on your content to update the project content base on UIT...`.bg_blue);
watcher.default('automatic' == config.extractionMethod ? config.sourceDirectory : draftSourceFilePath, { recursive: true }, () => {
syncProject();
});
});
}
else {
withHeader(() => {
console.log(` Please run 'uit setup' first. `.bg_red + '\n\n');
});
}
}
function sync() {
if (configFileExists) {
checkConfiguration(true, () => {
syncProject(true);
});
}
else {
withHeader(() => {
console.log(` Please run 'uit setup' first. `.bg_red + '\n\n');
});
}
}
function init(showHeader = true) {
if (showHeader)
withHeader(() => {
if (configFileExists) {
console.log(`The UI Translator config file already exists at `.green + `${configFilePath}`.blue + `, now let's make sure it has valid settings...\n`.yellow);
checkConfiguration(true, () => {
if (config.extractionMethod == 'automatic')
console.log(`\nCongratulations, your settings are valid! you can run `.green + `uit sync `.blue + `or ` + `uit watch`.blue + ` to automatically extract and sync your local content with the UI Translator\n`.green);
else
console.log(`\nCongratulations, your settings are valid! You can run `.green + `uit sync `.blue + `or ` + `uit watch`.blue + ` to sync your local content with the UI Translator\n`.green);
});
}
else {
console.log(`Creating the config file at ${configFilePath}, next, let's fill it with your UIT project dev informations...\n`.blue + `See here for help ` + `${config.helpUrl}`.blue.underline);
createConfigFile(true, () => {
checkConfiguration(false, () => {
if (config.extractionMethod == 'automatic')
console.log(`\nCongratulations! You are now ready to run `.green + `uit sync `.blue + `or ` + `uit watch`.blue + ` to automatically extract and sync your local content with the UI Translator\n`.green);
else
console.log(`\nCongratulations, you are all set! Run `.green + `uit sync `.blue + `or ` + `uit watch`.blue + ` to sync your local content with the UI Translator\n`.green);
});
});
}
});
else {
if (configFileExists) {
checkConfiguration(true, () => {
if (config.extractionMethod == 'automatic')
console.log(`\nCongratulations, your settings are valid! you can run `.green + `uit sync `.blue + `or ` + `uit watch`.blue + ` to automatically extract and sync your local content with the UI Translator\n`.green);
else
console.log(`\nCongratulations, your settings are valid! You can run `.green + `uit sync `.blue + `or ` + `uit watch`.blue + ` to sync your local content with the UI Translator\n`.green);
});
}
else {
createConfigFile(false, () => {
checkConfiguration(false, () => {
if (config.extractionMethod == 'automatic')
console.log(`\nCongratulations! You are now ready to run `.green + `uit sync `.blue + `or ` + `uit watch`.blue + ` to automatically extract and sync your local content with the UI Translator\n`.green);
else
console.log(`\nCongratulations, you are all set! Run `.green + `uit sync `.blue + `or ` + `uit watch`.blue + ` to sync your local content with the UI Translator\n`.green);
});
});
}
}
}
async function checkConfiguration(showError = false, cb) {
const configContent = fs.readFileSync(configFilePath, 'utf-8');
config = JSON.parse(configContent);
const apiKey = config.apiKey;
const projectId = config.projectId;
const extractionMethod = config.extractionMethod;
const extractionCommand = config.extractionCommand;
sourceTranslationFilePath = config.sourceTranslationFilePath;
let hasError = false;
if ("" === apiKey) {
if (showError) {
console.error(`The API Key is missing.`.red + ` See here ` + `${config.helpUrl}`.blue.underline);
hasError = true;
}
config.apiKey = await (0, prompts_1.password)({ message: "Enter your UIT dev API Key:", mask: true });
}
if ("" === projectId) {
if (showError) {
console.error(`The Project ID is missing.`.red + ` See here ` + `${config.helpUrl}`.blue.underline);
hasError = true;
}
config.projectId = await (0, prompts_1.input)({ message: "Enter this UIT project ID:", required: true });
}
if ("" === sourceTranslationFilePath) {
if (showError) {
console.error(`The Source Translation File path is missing.`.red + ` See here ` + `${config.helpUrl}`.blue.underline);
hasError = true;
}
config.sourceTranslationFilePath = await (0, prompts_1.input)({ message: "Enter the project source translation file path:", required: true });
draftSourceFilePath = config.sourceTranslationFilePath.slice(0, config.sourceTranslationFilePath.lastIndexOf('.')) + '.draft'
+ config.sourceTranslationFilePath.slice(config.sourceTranslationFilePath.lastIndexOf('.'));
}
if ("" === extractionCommand && 'automatic' == extractionMethod) {
if (showError) {
console.error(`Then Extraction command is missing. Please provide it or set the \'extractionMethod\' to \'Manually\' if you don't have one in your project.`.red + ` See here ` + `${config.helpUrl}`.blue.underline);
hasError = true;
}
config.extractionMethod = await (0, prompts_1.select)({
message: "How do you extract your i18n content? See the description of each choice below:", choices: [
{ name: 'Manually', value: 'manual', description: `You will need to manually write your content in the draft content file`.yellow },
{ name: 'Automatically', value: 'automatic', description: 'You will need to provide your i18n extraction command so that your content can be automatically extracted.'.yellow }
]
});
if (config.extractionMethod == 'automatic') {
config.extractionCommand = await (0, prompts_1.input)({ message: "Enter your project i18n extraction command" + " the output file should be ".yellow.bold + ` ${draftSourceFilePath} `.bg_red.bold, required: true });
}
}
if ("" === config.sourceDirectory && 'automatic' == config.extractionMethod) {
if (showError) {
console.error(`The source directory path is missing.`.red + ` See here ` + `${config.helpUrl}`.blue.underline);
hasError = true;
}
config.sourceDirectory = await (0, prompts_1.input)({ message: "Enter your project source directory(Enter . for the project root directory):", required: true });
}
if (hasError) {
await writeJSONFileSync(configFilePath, config);
configFileExists = fs.existsSync(configFilePath);
init(false);
return;
}
sourceTranslationExists = fs.existsSync(config.sourceTranslationFilePath);
draftSourceFilePath = config.sourceTranslationFilePath.slice(0, config.sourceTranslationFilePath.lastIndexOf('.')) + '.draft'
+ config.sourceTranslationFilePath.slice(config.sourceTranslationFilePath.lastIndexOf('.'));
draftSourceFileExists = fs.existsSync(draftSourceFilePath);
if (!draftSourceFileExists && !sourceTranslationExists) {
console.error("The 'sourceTranslationFilePath' provided does not contain any file. Please fix that to continue.".red + ` See here ` + `${config.helpUrl}`.blue.underline);
config.sourceTranslationFilePath = await (0, prompts_1.input)({ message: "Enter a valid source translation file path:", required: true });
await writeJSONFileSync(configFilePath, config);
init(false);
return;
}
else {
if (!draftSourceFileExists) {
fs.copyFileSync(config.sourceTranslationFilePath, draftSourceFilePath);
}
if (config.extractionMethod == 'automatic' && !config.extractorOK) {
console.log("\nLet's check your source directory next...".blue);
try {
const src = fs.readdirSync(config.sourceDirectory);
if (src.length == 0) {
console.error(`The directory provided as source directory is empty.`.red + ` See here ` + `${config.helpUrl}`.blue.underline);
config.sourceDirectory = await (0, prompts_1.input)({ message: "Enter a valid source directory(Enter . for the project root directory):", required: true });
await writeJSONFileSync(configFilePath, config);
init(false);
return;
}
else
console.log('Source directory OK.\n'.green);
}
catch (_) {
console.error(`The directory provided as source directory does not exist.`.red + ` See here ` + `${config.helpUrl}`.blue.underline);
config.sourceDirectory = await (0, prompts_1.input)({ message: "Enter a valid source directory(Enter . for the project root directory):", required: true });
await writeJSONFileSync(configFilePath, config);
init(false);
return;
}
console.log("Next, let's try the extraction...".blue);
console.log("Save one of your source files so we can check that the extraction works".yellow);
const w = watcher.default(config.sourceDirectory, { persistent: true, recursive: true, filter: (f, s) => !/uit\.config\.json/.test(f) });
w.once('change', async (evt, file) => {
console.log(' Running your content extractor'.blue);
let oldContent = '';
try {
oldContent = readJSONFileSync(draftSourceFilePath);
await writeJSONFileSync(draftSourceFilePath, '');
const result = (0, child_process_1.execSync)(`${config.extractionCommand}`);
const newContent = readJSONFileSync(draftSourceFilePath);
if (newContent.length == 2) {
await writeJSONFileSync(draftSourceFilePath, JSON.parse(oldContent));
console.log('The extraction command failed, no content was added by the extractor\n'.red);
console.log('Your command should extract content from your code to '.yellow + ' ' + draftSourceFilePath.blue + ". \nIf you don't know it enter 'none' to go back to the method selection and pick 'Manually'\n".yellow);
config.extractionCommand = await (0, prompts_1.input)({ message: "Please enter your project i18n extraction command" + " the output file should be ".yellow.bold + ` ${draftSourceFilePath} `.bg_red.bold, required: true });
if (config.extractionCommand == 'none')
config.extractionCommand = '';
await writeJSONFileSync(configFilePath, config);
w.close();
init(false);
}
else {
config.extractorOK = true;
await writeJSONFileSync(configFilePath, config);
const json = JSON.parse(newContent);
await writeJSONFileSync(draftSourceFilePath, json);
config.format = whatFormat(json);
writeConfig();
serverCheck(cb);
w.close();
}
}
catch (_) {
console.log('The extraction command failed. Your command broke the flow\n'.red);
writeJSONFileSync(draftSourceFilePath, JSON.parse(oldContent));
console.log('Your command should extract content from your code to '.yellow + ' ' + draftSourceFilePath.blue + ". \nIf you don't know it enter 'none' to go back to the method selection and pick 'Manually'\n".yellow);
config.extractionCommand = await (0, prompts_1.input)({ message: "Please enter your project i18n extraction command" + " the output file should be ".yellow.bold + ` ${draftSourceFilePath} `.bg_red.bold, required: true });
if (config.extractionCommand == 'none')
config.extractionCommand = '';
await writeJSONFileSync(configFilePath, config);
w.close();
init(false);
}
});
}
else {
writeConfig();
serverCheck(cb);
}
}
}
function deploy(skipApproval = false) {
checkConfiguration(false, () => {
if (config.extractionMethod == 'automatic') {
console.log('1. Extracting your data...'.blue);
(0, child_process_1.execSync)(`${config.extractionCommand}`);
}
readJSONFile(draftSourceFilePath, (error, data) => {
if (error) {
console.log('Unexpected error trying to read the source file, please retry and if the issue persists rerun the '.red + 'uit setup'.blue + ' command.');
return;
}
if ('automatic' == config.extractionMethod)
console.log('2. Checking the project content for release...\n'.blue);
else
console.log('Checking the project content for release...\n'.blue);
const json = JSON.parse(data);
config.format = whatFormat(json);
const r = `${config.hostBackend}/deployContent/${config.projectId}?skipApproval=${skipApproval}&format=${config.format}`;
try {
axios.default.post(r, json, { headers: { "X-api-key": config.apiKey } }).then((result) => {
if (result.data.uit_error_interruption) {
if (result.data.uit_error_code == 'ENOCONTENT') {
console.log('\nFailed: ENOCONTENT'.red.bold + ` There is nothing to release!. \nFirst add some content to your project, then run uit sync, then add one or more translations at `.red + `https://app.uitranslator.com/cp/projects/${config.projectId}`.blue.underline + ' and retry.\n'.red);
return;
}
if (result.data.uit_error_code == 'EMISSINGCONTENT') {
console.log('\nFailed: EMISSINGCONTENT'.red.bold + ` The project's source content has some empty texts. \nPlease fix it at `.red + `https://app.uitranslator.com/cp/projects/${config.projectId}`.blue.underline + ' then retry.\n'.red);
return;
}
if (result.data.uit_error_code == 'EMISSINGTRANSLATIONS') {
console.log('\nFailed: EMISSINGTRANSLATIONS'.red.bold + ` Some of your content translations are missing. \nPlease fix them at `.red + `https://app.uitranslator.com/cp/projects/${config.projectId}`.blue.underline + ' then retry.\n'.red);
return;
}
if (result.data.uit_error_code == 'EUNAPPROVEDCONTENT') {
console.log('\nFailed: EUNAPROVEDCONTENT'.red.bold + ` The content of this project is not approved yet. \nPlease approve it or make sure the content is approved at `.red + `https://app.uitranslator.com/cp/projects/${config.projectId}`.blue.underline + ` \nUse the `.red + ' --skipp-approval '.bg_red + ' flag to allow the release of unapproved content\n'.red);
return;
}
console.log(`\nServer rejection: '${result.data.uit_error_code}: ${result.data.uit_error_message}'. Please fix that and try again.\n`.red + `Review the project here `.yellow + `https://app.uitranslator.com/cp/projects/${config.projectId}\n`.blue.underline);
return;
}
else {
console.log(`Deploying ${result.data.uit_translations.length} content translations for this project...\n`.blue);
result.data.uit_translations.forEach((translation) => {
const fileName = sourceTranslationFilePath.indexOf('/') >= 0 ? sourceTranslationFilePath.slice(sourceTranslationFilePath.lastIndexOf('/') + 1) : sourceTranslationFilePath;
const hasLocaleAsName = fileName.slice(0, fileName.lastIndexOf('.')).length == 2 || (fileName.slice(0, fileName.lastIndexOf('.')).length == 5 && fileName.indexOf('-') == 2);
const finalName = 'original' != translation.lang && hasLocaleAsName ? translation.lang : `${sourceTranslationFilePath.slice(0, sourceTranslationFilePath.lastIndexOf('.'))}.${translation.lang}`;
const filePath = ('original' != translation.lang) ? (sourceTranslationFilePath.indexOf('/') >= 0 ? `${sourceTranslationFilePath.slice(0, sourceTranslationFilePath.lastIndexOf('/'))}/${finalName}.json` : `${finalName}.json`) : sourceTranslationFilePath;
writeJSONFileSync(filePath, translation.texts);
console.log(`Deployed ${translation.lang} content in ${filePath} \n`.green);
});
}
}, (error) => console.log(`Connection failed to the server. Please check your internet and try again\n`.red));
}
catch (error) {
console.error('Failed to save your changes, please make sure that your file is still a valid JSON file and retry.\n'.bg_red);
console.log(`\nListening to changes on your content to update the project content base on UIT...`.bg_blue);
}
});
});
}
function getDraft(locale = null) {
checkConfiguration(false, () => {
if (config.extractionMethod == 'automatic') {
console.log('1. Extracting your data...'.blue);
(0, child_process_1.execSync)(`${config.extractionCommand}`);
}
readJSONFile(draftSourceFilePath, (error, data) => {
if (error) {
console.log('Unexpected error trying to read the source file, please retry and if the issue persists rerun the '.red + 'uit setup'.blue + ' command.');
return;
}
if ('automatic' == config.extractionMethod)
console.log(`2. Requesting the project ${!locale ? 'original' : locale} content...`.blue);
else
console.log(`Requesting the project ${!locale ? 'original' : locale} content...`.blue);
const json = JSON.parse(data);
config.format = whatFormat(json);
const r = `${config.hostBackend}/draftContent/${config.projectId}?lang=${locale}&format=${config.format}`;
try {
axios.default.post(r, json, { headers: { "X-api-key": config.apiKey } }).then((result) => {
if (result.data.uit_error_interruption) {
if (result.data.uit_error_code == 'ENOTRANSLATION')
console.log(`\nServer rejection: '${result.data.uit_error_code}: ${result.data.uit_error_message}'. The locale should be a language your project is translated in.\n`.red + `Review the project here ` + `${config.helpUrl}\n`.blue.underline);
else
console.log(`\nServer rejection: '${result.data.uit_error_code}: ${result.data.uit_error_message}'. We were not expecting this to happen please try again later.\n`.red + `Review the project here ` + `${config.helpUrl}\n`.blue.underline);
return;
}
else {
console.log("Processing the server response...\n".green);
delete result.data.uit_error_code;
delete result.data.uit_error_message;
delete result.data.uit_error_interruption;
delete result.data.uit_request_echo;
const fileName = sourceTranslationFilePath.indexOf('/') >= 0 ? sourceTranslationFilePath.slice(sourceTranslationFilePath.lastIndexOf('/') + 1) : sourceTranslationFilePath;
const hasLocaleAsName = fileName.slice(0, fileName.lastIndexOf('.')).length == 2 || (fileName.slice(0, fileName.lastIndexOf('.')).length == 5 && fileName.indexOf('-') == 2);
const finalName = null != locale && hasLocaleAsName ? locale : `${sourceTranslationFilePath.slice(0, sourceTranslationFilePath.lastIndexOf('.'))}.${locale}`;
const filePath = (null != locale) ? (sourceTranslationFilePath.indexOf('/') >= 0 ? `${sourceTranslationFilePath.slice(0, sourceTranslationFilePath.lastIndexOf('/'))}/${finalName}.json` : `${finalName}.json`) : sourceTranslationFilePath;
writeJSONFileSync(filePath, result.data);
console.log(`Content written to ${filePath} \n`.green);
}
}, (error) => console.log(`Connection failed to the server. Please check your internet and try again\n`.red));
}
catch (error) {
console.error('Failed to save your changes, please make sure that your file is still a valid JSON file and retry.\n'.bg_red);
console.log(`\nListening to changes on your content to update the project content base on UIT...`.bg_blue);
}
});
});
}
function serverCheck(cb) {
console.log(`Let\'s submit your settings to the UIT server...`.blue);
const r = `${config.hostBackend}/draftContent/${config.projectId}?ui=uit_params_verification`;
axios.default.get(r, { headers: { "X-api-key": config.apiKey } }).then(async (result) => {
if (result.data.uit_error_interruption) {
console.log(`\nServer rejection: '${result.data.uit_error_code}: ${result.data.uit_error_message}'. Please check your access and make sure you have dev access on this project.\n`.red + `Review the documentaion here ` + `${config.helpUrl}\n`.blue.underline);
if (result.data.uit_error_code == 'EUSERNOTFOUND')
config.apiKey = await (0, prompts_1.password)({ message: "Enter a valid UIT API key:", mask: true });
else if (result.data.uit_error_code == 'EPROJECTNOTFOUND')
config.projectId = await (0, prompts_1.input)({ message: "Enter a valid UIT Project ID:", required: true });
await writeJSONFileSync(configFilePath, config);
init(false);
return;
}
else {
console.log("The server approved your settings!\n".green);
await writeJSONFileSync(configFilePath, config);
if (!draftSourceFileExists) {
fs.copyFileSync(config.sourceTranslationFilePath, draftSourceFilePath);
if (config.extractionMethod == 'manual')
console.log(`Source content draft created at `.yellow + `${draftSourceFilePath}`.blue + `. ` + `** Please do your content updates on it from now on **`.yellow.bold);
else
console.log(`Source content draft created at `.yellow + `${draftSourceFilePath}`.blue);
}
cb();
}
}, error => {
console.log(error);
});
}
async function syncProject(showTip = false) {
if (seflSavingNoSync) {
seflSavingNoSync = false;
console.log('Self save not taken seriously');
return;
}
if (config.extractionMethod == 'automatic') {
console.log('1. Extracting your data...'.blue);
(0, child_process_1.execSync)(`${config.extractionCommand}`);
}
readJSONFile(draftSourceFilePath, (error, data) => {
if (error) {
console.log('Unexpected error trying to read the source file, please retry and if the issue persists rerun the '.red + 'uit setup'.blue + ' command.');
return;
}
if ('automatic' == config.extractionMethod)
console.log('2. Syncing the project...'.blue);
else
console.log('Syncing the project...'.blue);
const json = JSON.parse(data);
config.format = whatFormat(json);
const sessionTime = argv.time ? argv.time : undefined;
const r = `${config.hostBackend}/syncContent/${config.projectId}?format=${config.format}&only_watch=${watching}&sessionTime=${sessionTime}&extraction=${config.extractionMethod}`;
try {
axios.default.post(r, json, { headers: { "X-api-key": config.apiKey } }).then((result) => {
if (!watching) {
seflSavingNoSync = true;
writeJSONFile(draftSourceFilePath, result.data);
}
else {
console.log('Content synced...'.bg_green);
console.log(`\nListening to changes on your content to update the project content base on UIT...`.bg_blue);
}
}, (error) => console.log(`Connection failed to the server. Please check your internet and try again\n`.red));
}
catch (error) {
console.error('Failed to save your changes, please make sure that your file is still a valid JSON file and retry.\n'.bg_red);
console.log(`\nListening to changes on your content to update the project content base on UIT...`.bg_blue);
}
});
}
function readJSONFileSync(filename) {
return fs.readFileSync(filename, "utf8");
}
function whatFormat(object) {
let result = '';
if (!hasBranches(object))
result = 'json-flat';
else if (undefined != object.locale
&& isLeaf(object.locale)
&& undefined != object.translations
&& !hasBranches(object.translations))
result = 'json-ng';
else
result = 'json-deep';
return result;
}
function isLeaf(object) {
return typeof object != typeof new Object();
}
function hasBranches(object) {
return Object.keys(object).map(it => isLeaf(object[it])).some(it => it == false);
}
async function readJSONFile(filename, cb) {
fs.readFile(filename, "utf8", cb);
}
function createConfigFile(sayYay = true, cb = {}) {
let projectConfig = null;
const ignored = fs.readFileSync('.gitignore', 'utf-8');
if (ignored.indexOf(configFilePath) < 0)
(0, child_process_1.execSync)(`echo "\n${configFilePath}" >> .gitignore`);
if (fs.existsSync(projectConfigFilePath)) {
projectConfig = JSON.parse(fs.readFileSync(projectConfigFilePath, "utf8"));
}
const configTemplate = {
apiKey: "",
projectId: projectConfig ? projectConfig.projectId : "",
extractionMethod: projectConfig ? projectConfig.extractionMethod : "automatic",
extractionCommand: projectConfig ? projectConfig.extractionCommand : "",
sourceTranslationFilePath: projectConfig ? projectConfig.sourceTranslationFilePath : "",
sourceDirectory: projectConfig ? projectConfig.sourceDirectory : "",
hostBackend: projectConfig ? projectConfig.hostBackend : "http://localhost:8092/translator-interface/us-central1",
helpUrl: projectConfig ? projectConfig.helpUrl : "https://app.uitranslator.com/login"
};
fs.writeFileSync(configFilePath, JSON.stringify(configTemplate));
if (sayYay)
console.log(`Config file created at `.green + `${configFilePath}\n`.blue);
cb();
}
function writeConfig() {
const json = JSON.stringify(config, null, 2);
fs.writeFileSync(configFilePath, json);
const projectJson = JSON.stringify({
projectId: config.projectId,
extractionMethod: config.extractionMethod,
extractionCommand: config.extractionCommand,
sourceTranslationFilePath: config.sourceTranslationFilePath,
sourceDirectory: config.sourceDirectory,
hostBackend: config.hostBackend,
helpUrl: config.helpUrl
}, null, 2);
fs.writeFileSync(projectConfigFilePath, projectJson);
}
async function writeJSONFileSync(file, jsonData) {
const json = JSON.stringify(jsonData, null, 2);
fs.writeFileSync(file, json);
}
async function writeJSONFile(file, jsonData) {
const json = JSON.stringify(jsonData, null, 2);
fs.writeFile(file, json, () => {
console.log('Content synced...'.bg_green);
if (watching)
console.log(`\nListening to changes on your content to update the project content base on UIT...`.bg_blue);
});
}