all files / lib/ import.js

100% Statements 33/33
100% Branches 8/8
100% Functions 3/3
100% Lines 32/32
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                                                                                                    
const fs = require('fs');
const {mapValues} = require('lodash');
const co = require('co');
 
const polyglotUtils = require('../common/polyglot');
const utils = require('../common/utils');
const network = require('../common/network');
const config = require('../common/config');
 
const importTranslations = co.wrap(function* (fileSystem, remoteTranslations = [], cwd, project = config.project) {
 
  /*
   * Mapping of remote keys in the following format:
   * @example
   * {
   *   key: "remoteKeyId"
   * }
   */
  const remoteKeysObj = yield network.read(`/translation_keys?filter[project_id]=${project.id}`);
  const remoteKeys = utils.getTranslationKeys(remoteKeysObj);
  utils.debug(`Number of remote keys: ${Object.keys(remoteKeys).length}`);
 
  /*
   * The default project language
   *
   * @prop {String} locale - The language locale
   * @prop {String} id - The remote language id
   */
  const language = yield polyglotUtils.getDefaultLanguage();
  const locale = utils.getParsedLocale(language.locale);
  utils.debug(`Default language: ${language.locale} / ${locale}`);
 
  /*
   * Key/value local translations for the default project language
   */
  const localTranslations = polyglotUtils.loadTranslations(fileSystem, locale, config.project, cwd);
 
  /*
   * A list of local keys
   */
  const localKeys = Object.keys(localTranslations);
  utils.debug(`Number of local keys: ${localKeys.length}`);
 
  /*
   * Mapping of all (existing and new) keys in the following format:
   * @example
   * {
   *   key: "remoteKeyId"
   * }
   */
  const keys = yield polyglotUtils.ensureRemoteKeys(localKeys, remoteKeys);
  utils.debug(`Number of total keys: ${Object.keys(keys).length}`);
 
  /*
   * Translations that need to be sent to the server. If remote is defined, the translation needs to be updated
   */
  const translations = mapValues(localTranslations, function(value, key) {
    const remote = remoteTranslations.find((item) => item.translation_key.name === key);
    return {
      value,
      remote: remote && remote.id
    };
  });
 
  utils.debug(JSON.stringify(translations));
  yield polyglotUtils.sendTranslations(language.id, keys, translations, localTranslations);
});
 
module.exports = function(program, fileSystem = fs, cwd) {
  const err = polyglotUtils.checkInitErrors();
  if (err) {
    return err;
  }
 
  return polyglotUtils.getRemoteTranslations().then((translations) => {
    if (translations.length && !program.force) {
      return utils.error('Can\'t push if remote translations already exist. Use --force to override.');
    }
    return importTranslations(fileSystem, translations, cwd);
  });
};