UNPKG

gitbook-start-github-rafadanipedro

Version:
202 lines (151 loc) 5.58 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _npmi = require('npmi'); var _npmi2 = _interopRequireDefault(_npmi); var _anadirTareaAlGulpfile = require('./anadirTareaAlGulpfile'); var _anadirTareaAlGulpfile2 = _interopRequireDefault(_anadirTareaAlGulpfile); var _anadirConfig = require('./anadirConfig'); var _anadirConfig2 = _interopRequireDefault(_anadirConfig); var _utils = require('./utils'); 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"); }); }; } exports.default = function () { var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(nombrePlugin, userArgs) { var pluginPath, plugin, pluginConfig, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, key, prom; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.prev = 0; !estamosEnUnGitbook() ? process.exit(1) : ''; existeEnPluginJSON(nombrePlugin) ? process.exit(1) : ''; (0, _anadirTareaAlGulpfile2.default)(nombrePlugin); _context.next = 6; return npmInstall(nombrePlugin); case 6: console.info('Instalado el nuevo plugin!!'); pluginPath = _path2.default.resolve(process.cwd(), 'node_modules', nombrePlugin); plugin = require(pluginPath); pluginConfig = plugin.config(); console.log(pluginConfig.then); if (!pluginConfig.then) { _context.next = 15; break; } _context.next = 14; return pluginConfig; case 14: pluginConfig = _context.sent; case 15: _iteratorNormalCompletion = true; _didIteratorError = false; _iteratorError = undefined; _context.prev = 18; for (_iterator = Object.keys(pluginConfig)[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { key = _step.value; if (userArgs[key]) { pluginConfig[key] = userArgs[key]; } } _context.next = 26; break; case 22: _context.prev = 22; _context.t0 = _context['catch'](18); _didIteratorError = true; _iteratorError = _context.t0; case 26: _context.prev = 26; _context.prev = 27; if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } case 29: _context.prev = 29; if (!_didIteratorError) { _context.next = 32; break; } throw _iteratorError; case 32: return _context.finish(29); case 33: return _context.finish(26); case 34: (0, _anadirConfig2.default)(pluginConfig, nombrePlugin.replace(/-(.)/g, function (m) { return m[1].toUpperCase(); })); if (!plugin.start) { _context.next = 40; break; } prom = plugin.start(pluginConfig); if (!prom.then) { _context.next = 40; break; } _context.next = 40; return prom; case 40: _context.next = 45; break; case 42: _context.prev = 42; _context.t1 = _context['catch'](0); console.error(_context.t1); case 45: case 'end': return _context.stop(); } } }, _callee, this, [[0, 42], [18, 22, 26, 34], [27,, 29, 33]]); })); function deploy(_x, _x2) { return _ref.apply(this, arguments); } return deploy; }(); function estamosEnUnGitbook() { try { require(_path2.default.resolve(process.cwd(), 'book.json')); return true; } catch (err) { console.error('No se reconoce tu directorio actual como un gitbook'); return false; } } function npmInstall(plugin) { return new Promise(function (resolve, reject) { var options = { name: plugin, npmLoad: { loglevel: 'warn', 'save-dev': true } }; (0, _npmi2.default)(options, function (err, result) { if (err) { if (err.code === _npmi2.default.LOAD_ERR) console.error('npm load error');else if (err.code === _npmi2.default.INSTALL_ERR) console.error('npm install error'); return reject(err); } resolve(); }); }); } function existeEnPluginJSON(plugin) { var plugins = void 0; try { plugins = new _utils.loadJSON('plugin.json'); } catch (err) { return false; } if (plugins.json[plugin]) { console.error('Ya tienes el plugin ' + plugin); return true; } return false; }