all files / lib/ cli.js

84.78% Statements 39/46
76.92% Branches 20/26
71.43% Functions 5/7
84.78% Lines 39/46
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                                                                                           26× 26×                                          
const store = require('dot-file-config')('.dictionary-cli-npm');
store.data.requests = store.data.requests || {};
 
const meow = require('meow');
const chalk = require('chalk');
 
const cli = meow({
  pkg: './../package.json',
  help: [
    'Usage',
    '  dictionary <lang-from> <lang-to> <input>',
		'',
		'  # if input match /[a-zA-Z]/ it will translate from english to <lang>, otherwise from <lang> to english',
    '  dictionary --en=<lang> <input>',
		'  # if input match /[а-яА-Я]/ it will translate from russian to <lang>, otherwise from <lang> to russian',
    '  dictionary --ru=<lang> <input>',
		'',
    '  dictionary <lang> --export',
    '  dictionary <lang> --export > history.txt',
    '',
    'Data',
    '  ' + store.path
  ]
});
 
const p = console.log.bind(console);
 
let fromLang;
let toLang;
let input;
 
const setupLangs = (lang, regEx) => {
	input = cli.input.join(' ');
 
	if (regEx.test(input)) {
		fromLang = lang;
		toLang = cli.flags[lang];
	} else {
		fromLang = cli.flags[lang];
		toLang = lang;
	}
};
 
if (cli.flags.en) {
	setupLangs('en', /[a-zA-Z]+/);
} else if (cli.flags.ru) {
	setupLangs('ru', /[а-яА-Я]+/);
} else {
	input = cli.input.slice(2).join(' ');
}
 
Iif (cli.flags.export && cli.input.length !== 1 || !cli.flags.export && input.length === 0) {
  cli.showHelp();
} else Iif (cli.flags.export) {
  const result = require('./exporter').default(store.data.requests, cli.input[0]);
  require('lodash/collection/shuffle')(result).forEach(function(word) {
    p(word);
  });
} else {
	fromLang = fromLang || cli.input[0];
  toLang = toLang || cli.input[1];
 
  const lib = require('./index');
	const space = require('lodash/string/repeat').bind(null, ' ');
 
  lib
    .translate(fromLang, toLang, input)
    .then(({ type, result, corrected }) => {
			if (corrected) {
				p(`\n${space(2)}nothing found by "${input}", corrected to "${corrected}"\n`)
			}
 
      if (type === 'dictionary') {
				result.forEach(r => {
					p(`${r.title}`);
          r.translations.forEach(printTranslations);
				});
 
				function printTranslations(r, i) {
					p(`${space(2)}- ${chalk.blue(r.translation)} ${r.synonyms} ${r.means}`);
					r.examples.forEach(r => p(`${space(4)}-- ${r.title}`));
				};
      } else {
        p(result);
      }
 
      store.data.requests[new Date().toString()] = {
        input,
        fromLang,
        toLang,
        result,
        type
      };
 
      store.save();
    })
    .catch(err => {
      if (err.message === 'no translation') {
        console.log('no translation');
      } else {
        throw err;
      }
    });
}