all files / common/ utils.js

72.73% Statements 24/33
66.67% Branches 12/18
100% Functions 8/8
82.76% Lines 24/29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                                                                   63×                                             12×     12×                                                     15× 15×                                                  
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) {
    Iif (!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) {
    Iif (!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) {
    Iif (!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) {
    Iif (!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) => {
        Iif (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'
            }
          }
        }
      }
    };
  }
};