UNPKG

lokalise

Version:

A tool to retrieve your localization files from lokali.se

186 lines (145 loc) 5.74 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.build = undefined; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _util = require('util.promisify'); var _util2 = _interopRequireDefault(_util); var _pick = require('lodash/pick'); var _pick2 = _interopRequireDefault(_pick); var _rename = require('./object/rename'); var _rename2 = _interopRequireDefault(_rename); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } var readFile = function () { var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(file) { return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.prev = 0; _context.next = 3; return (0, _util2.default)(_fs2.default.readFile)(file || '.lokalise.json', 'utf8'); case 3: return _context.abrupt('return', _context.sent); case 6: _context.prev = 6; _context.t0 = _context['catch'](0); if (!file) { _context.next = 10; break; } throw _context.t0; case 10: case 'end': return _context.stop(); } } }, _callee, undefined, [[0, 6]]); })); return function readFile(_x) { return _ref.apply(this, arguments); }; }(); var bridgeLegacy = function bridgeLegacy(opts) { return _extends({}, (0, _rename2.default)(opts, ['api_token', 'project_id', 'output_path'], ['token', 'project', 'output']), (0, _pick2.default)(opts, ['token', 'project', 'output'])); }; var fetchFileConfig = function () { var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(file) { var contents, config; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return readFile(file); case 2: contents = _context2.sent; if (!contents) { _context2.next = 6; break; } config = JSON.parse(contents); return _context2.abrupt('return', bridgeLegacy(config)); case 6: return _context2.abrupt('return', null); case 7: case 'end': return _context2.stop(); } } }, _callee2, undefined); })); return function fetchFileConfig(_x2) { return _ref2.apply(this, arguments); }; }(); var fetchEnvironmentConfig = function fetchEnvironmentConfig() { var config = {}; if (process.env.LOKALISE_TOKEN) { config.token = process.env.LOKALISE_TOKEN; } if (process.env.LOKALISE_PROJECT) { config.project = process.env.LOKALISE_PROJECT; } if (process.env.LOKALISE_OUTPUT) { config.output = process.env.LOKALISE_OUTPUT; } return config; }; var validate = function validate(_ref3) { var token = _ref3.token, project = _ref3.project, output = _ref3.output; if (!token) throw Error('"token" is undefined'); if (!project) throw Error('"project" is undefined'); if (!output) throw Error('"output" is undefined'); }; var build = exports.build = function () { var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(file, args) { var envOptions, fileOptions, argOptions, options; return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: envOptions = fetchEnvironmentConfig(); _context3.next = 3; return fetchFileConfig(file); case 3: _context3.t0 = _context3.sent; if (_context3.t0) { _context3.next = 6; break; } _context3.t0 = {}; case 6: fileOptions = _context3.t0; argOptions = (0, _pick2.default)(args, ['token', 'project', 'output']); options = _extends({}, fileOptions, envOptions, argOptions); _context3.prev = 9; validate(options); _context3.next = 17; break; case 13: _context3.prev = 13; _context3.t1 = _context3['catch'](9); if (Object.keys(fileOptions).length === 0) { console.warn('HINT: It seems that you don\'t have a .lokalise.json file in your directory or you haven\'t specified any options in it.'); } throw _context3.t1; case 17: return _context3.abrupt('return', options); case 18: case 'end': return _context3.stop(); } } }, _callee3, undefined, [[9, 13]]); })); return function build(_x3, _x4) { return _ref4.apply(this, arguments); }; }();