polyglot-cli
Version:
Infinum Polyglot CLI
132 lines (119 loc) • 2.91 kB
JavaScript
const colors = require('colors/safe');
const config = require('./config');
// Define the theme colors
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'white',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
module.exports = {
/*
* Output the general log
*
* @param {Mixed[]} - Params to be outputed
* @return {undefined}
*/
log(...args) {
if (!config.silent) {
console.info(...args.map((arg) => colors.input(arg))); // eslint-disable-line no-console
}
},
/*
* Output the debug log
*
* @param {Mixed[]} - Params to be outputed
* @return {undefined}
*/
debug(...args) {
if (!config.silent && config.debug) {
console.info(...args.map((arg) => colors.debug(arg))); // eslint-disable-line no-console
}
},
/*
* Output the info log
*
* @param {Mixed[]} - Params to be outputed
* @return {undefined}
*/
info(...args) {
if (!config.silent) {
console.info(...args.map((arg) => colors.info(arg))); // eslint-disable-line no-console
}
},
/*
* Output the error log
*
* @param {Mixed[]} - Params to be outputed
* @return {undefined}
*/
error(...args) {
if (!config.silent) {
console.error(...args.map((arg) => colors.error(arg))); // eslint-disable-line no-console
}
return Promise.reject(args[0]);
},
/*
* Promise wrapper for the prompt command
* @param {Object} prompt - Prompt object
* @param {Object} schema - Schema that should be used for the prompt
* @return {Object} Data entered by the user in the prompt
*/
prompt(prompt, schema) {
return new Promise((resolve, reject) => {
prompt.get(schema, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
},
/*
* Flattens the translationKeys to a key/keyId object
*/
getTranslationKeys(translationKeys) {
const keys = {};
translationKeys.forEach((key) => {
keys[key.name] = key.id;
});
return keys;
},
getParsedLocale(locale, opts = (config.project && config.project.locale) || {language: true, country: true}) {
const [language, country] = locale.split('_');
if (opts.language && opts.country) {
return locale;
} else if (opts.language) {
return language;
}
return country;
},
constructTranslationObject(language, key, value) {
return {
data: {
attributes: {value},
relationships: {
language: {
data: {
id: language,
type: 'language'
}
},
translation_key: { // eslint-disable-line camelcase
data: {
id: key,
type: 'translation_key'
}
}
}
}
};
}
};