all files / lib/ init.js

97.3% Statements 36/37
100% Branches 14/14
100% Functions 6/6
97.3% Lines 36/37
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                                                                                                                                                                      
const fs = require('fs');
const prompt = require('prompt');
const colors = require('colors/safe');
const {sortBy} = require('lodash');
 
const network = require('../common/network');
const config = require('../common/config');
const utils = require('../common/utils');
const polyglotUtils = require('../common/polyglot');
 
const serializers = {
  1: 'flat',
  2: 'nested'
};
 
const namedPlaceholderFormats = {
  1: 'harmony',
  2: 'mustache'
};
 
module.exports = function(program, opts, fileSystem = fs, requestCb = () => true) {
  const err = polyglotUtils.checkInitErrors({project: false});
  if (err) {
    return err;
  }
 
  if (config.project && config.project.token) {
    return utils.error(`The project ${config.project.name} is already initialized`);
  }
 
  return network.read('/projects').then((projects) => {
    requestCb();
 
    const sortedProjects = sortBy(projects, 'name');
 
    sortedProjects.forEach((project, index) => {
      utils.log(`${index} - ${project.name}`);
    });
 
    const schema = {
      properties: {
        project: {
          description: colors.prompt('Project number'),
          format: 'number',
          required: true,
          conform(value) {
            return value >= 0 && value < sortedProjects.length;
          }
        },
        serializer: {
          description: colors.prompt('Serializer type:\n1 - Flat\n2 - Nested'),
          format: 'number',
          required: true,
          conform(value) {
            return value in serializers;
          }
        },
        locale: {
          description: colors.prompt('Locale format:\n1 - en_US\n2 - en\n3 - US'),
          format: 'number',
          required: true,
          conform(value) {
            return value > 0 && value < 4;
          }
        },
        filetype: {
          description: colors.prompt('File type:\n1 - Single\n2 - Multi'),
          format: 'number',
          required: true,
          conform(value) {
            return value > 0 && value < 3;
          }
        },
        namedPlaceholderFormats: {
          description: colors.prompt('Named placeholder:\n1 - ${name}\n2 - {{name}}'),
          format: 'number',
          required: true,
          conform(value) {
            return value > 0 && value < 2;
          }
        },
        path: {
          description: colors.prompt('Relative path to the translations folder (with filename if single file)'),
          format: 'string',
          required: true
        }
      }
    };
 
    prompt.message = '';
    prompt.start(opts);
 
    return Promise.all([sortedProjects, utils.prompt(prompt, schema)]);
  }).then(([projects, result]) => {
    const project = projects[result.project];
    const data = {
      name: project.name,
      token: project.token,
      id: project.id,
      path: result.path,
      serializer: serializers[result.serializer],
      filetype: result.filetype,
      initialized: (new Date()).toISOString(),
      lastUpdatedAt: 0,
      placeholder: {
        named: namedPlaceholderFormats[result.namedPlaceholderFormats]
      },
      locale: {
        language: result.locale !== '3',
        country: result.locale !== '2'
      }
    };
    polyglotUtils.saveProject(fileSystem, data);
    utils.info(`Project ${data.name} was initialized`);
    config.project = data;
    return data;
  }).catch((e) => utils.error(`Something went wrong: ${e}`));
};