all files / common/ polyglot.js

96% Statements 48/50
93.75% Branches 15/16
100% Functions 10/10
95.92% Lines 47/49
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                                                             19×     15×     12×                                            
const fs = require('fs');
const path = require('path');
const co = require('co');
const config = require('./config');
const network = require('./network');
const utils = require('./utils');
const serializers = require('../serializers');
 
function __createKey(name, projectId) {
  return network.create('/translation_keys', {
    data: {
      attributes: {name},
      relationships: {
        project: {
          data: {id: projectId, type: 'project'}
        }
      }
    }
  }).then((key) => key.id);
}
 
module.exports = {
  getProjectData(projectId = config.project.id) {
    return network.read(`/projects/${projectId}`);
  },
 
  getLastUpdatedAt(projectId = config.project.id) {
    return this.getProjectData(projectId)
      .then((projectData) => projectData.updated_at);
  },
 
  getDefaultLanguage(projectId = config.project.id) {
    return this.getProjectData(projectId)
      .then((projectData) => ({
        locale: projectData.default_language.locale,
        id: projectData.default_language.id
      }));
  },
 
  getRemoteTranslations(projectId = config.project.id) {
    return network.read(`/translations?filter[project_id]=${projectId}`);
  },
 
  saveProject(fileSystem = fs, project = config.project) {
    project.version = config.version;
    fileSystem.writeFileSync(config.projectFile, JSON.stringify(project, null, 2));
  },
 
  checkInitErrors({user = true, project = true} = {}) {
    if (user && (!config.auth || !config.auth.token)) {
      return utils.error('You need to log in first');
    }
 
    if (project && (!config.project || !config.project.id)) {
      return utils.error('Please initialize the project first');
    }
 
    return null;
  },
 
  loadTranslations(fileSystem, locale, project = config.project, cwd = process.cwd()) {
    let translations;
    const filePath = path.join(cwd, project.path);
    Eif (project.filetype === '1') {
      utils.debug('Loading a single file');
      translations = JSON.parse(fileSystem.readFileSync(filePath));
    } else {
      utils.debug('Loading a locale file');
      translations = {
        [locale]: JSON.parse(fileSystem.readFileSync(path.join(filePath, `${locale}.json`)))
      };
    }
 
    // Deserialize
    utils.debug(`Deserialize using the ${project.serializer} serializer`);
    return serializers.forRemote(translations, project)[locale];
  },
 
  ensureRemoteKeys: co.wrap(function* (localKeys, remoteKeys, project = config.project) {
    const keysToCreate = localKeys.filter((key) => !(key in remoteKeys));
    const map = Object.assign({}, remoteKeys);
    for (const key of keysToCreate) {
      const keyId = yield __createKey(key, project.id);
      utils.debug(`Create key ${key}: ${keyId}`);
      map[key] = keyId;
    }
    return map;
  }),
 
  sendTranslations: co.wrap(function* (language, keys, toImport = {}, localTranslations) {
    const translationKeys = Object.keys(toImport);
    for (const translationKey of translationKeys) {
      const remote = toImport[translationKey].remote;
      const key = keys[translationKey];
      const value = localTranslations[translationKey];
      const data = utils.constructTranslationObject(language, key, value);
      const method = remote ? network.update : network.create;
      const url = remote ? `/translations/${remote}` : '/translations';
      yield method(url, data);
    }
  })
};