UNPKG

gitbook-start-github-rafadanipedro

Version:
326 lines (264 loc) 10.4 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.getGHUsername = exports.ghClient = undefined; // Función para obtener el token, si no existe el token se crea. var ghClient = exports.ghClient = function () { var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2() { return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.t0 = _octonode2.default; _context2.next = 3; return getToken(); case 3: _context2.t1 = _context2.sent; return _context2.abrupt('return', _context2.t0.client.call(_context2.t0, _context2.t1)); case 5: case 'end': return _context2.stop(); } } }, _callee2, this); })); return function ghClient() { return _ref2.apply(this, arguments); }; }(); var getToken = function () { var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3() { var dirToken, config, tokenGithub; return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: console.log('Obteniendo Token'); try { _fs2.default.mkdirSync(_path2.default.resolve(process.env.HOME, '.gitbook-start')); } catch (e) {} dirToken = _path2.default.resolve(process.env.HOME, '.gitbook-start/config.json'); config = void 0; _context3.prev = 4; config = require(dirToken); return _context3.abrupt('return', config.tokens.github); case 9: _context3.prev = 9; _context3.t0 = _context3['catch'](4); // No existe dirtoken config = {}; _context3.next = 14; return generateTokenGithub(); case 14: tokenGithub = _context3.sent; config.tokens = { github: tokenGithub }; console.log('El token ' + tokenGithub + ' se ha guardado en ' + dirToken); _fs2.default.writeFileSync(dirToken, JSON.stringify(config, undefined, 2)); return _context3.abrupt('return', tokenGithub); case 19: case 'end': return _context3.stop(); } } }, _callee3, this, [[4, 9]]); })); return function getToken() { return _ref3.apply(this, arguments); }; }(); //Función para generar un token de Github var generateTokenGithub = function () { var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4() { var username, password; return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: console.log('Generando el token de Github'); _context4.next = 3; return ask('Introduce tu nombre de usuario de Github'); case 3: username = _context4.sent; _context4.next = 6; return ask('Introduce tu contraseña de Github', true); case 6: password = _context4.sent; return _context4.abrupt('return', new Promise(function (resolve, reject) { _octonode2.default.auth.config({ username: username, password: password }).login({ scopes: ['user', 'repo'], note: 'Token para Gitbook' }, function (err, id, token) { if (err) return reject(err); return resolve(token); }); })); case 8: case 'end': return _context4.stop(); } } }, _callee4, this); })); return function generateTokenGithub() { return _ref4.apply(this, arguments); }; }(); // Función para preguntar cosas por pantalla var ask = function () { var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5(question, password) { return regeneratorRuntime.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: return _context5.abrupt('return', new Promise(function (resolve, reject) { _inquirer2.default.prompt([{ type: password ? 'password' : 'input', name: 'respuesta', message: question }]).then(function (answers) { resolve(answers.respuesta); }); })); case 1: case 'end': return _context5.stop(); } } }, _callee5, this); })); return function ask(_x2, _x3) { return _ref5.apply(this, arguments); }; }(); // Función que crea un repositorio en Github var createRepoGithub = function () { var _ref6 = _asyncToGenerator(regeneratorRuntime.mark(function _callee6(options, client) { return regeneratorRuntime.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: console.log('Creando repositorio en Github'); return _context6.abrupt('return', new Promise(function (resolve, reject) { var ghme = client.me(); ghme.repo({ name: options.name, description: options.description }, function (err, body) { if (err) { if (err.statusCode === 422) return reject(new Error('Ya existe ese repositorio en tu cuenta de Github')); if (err.statusCode === 404) return reject(new Error('No tengo permisos para crear el repositorio en Github')); if (err.statusCode === 401) return reject(new Error('No tengo permisos (has borrado el token de github?)')); return reject(err); } return resolve(body); }); })); case 2: case 'end': return _context6.stop(); } } }, _callee6, this); })); return function createRepoGithub(_x4, _x5) { return _ref6.apply(this, arguments); }; }(); //Función para obtener nombre de usuario var getGHUsername = exports.getGHUsername = function () { var _ref7 = _asyncToGenerator(regeneratorRuntime.mark(function _callee7(client) { return regeneratorRuntime.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: return _context7.abrupt('return', new Promise(function (resolve, reject) { client.me().info(function (err, data) { if (err) return reject(err); return resolve(data.login); }); })); case 1: case 'end': return _context7.stop(); } } }, _callee7, this); })); return function getGHUsername(_x6) { return _ref7.apply(this, arguments); }; }(); //Funcion para crear el repoitorio local y pushear a la master var creaRepoLocal = function () { var _ref8 = _asyncToGenerator(regeneratorRuntime.mark(function _callee8(username, nombreApp) { return regeneratorRuntime.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: return _context8.abrupt('return', new Promise(function (resolve, reject) { var repoPath = _path2.default.resolve(process.cwd(), nombreApp); console.log('Creando repositorio git local'); console.log('Inicializando repo en ' + repoPath); console.log('La direcci\xF3n del remoto es: git@github.com:' + username + '/' + nombreApp + '.git'); (0, _simpleGit2.default)(repoPath).init().addRemote('origin', 'git@github.com:' + username + '/' + nombreApp + '.git').add('.').commit('Primer Commit').push(['-uf', 'origin', 'master'], function (err, data) { if (err) return reject(err); return resolve(data); }); })); case 1: case 'end': return _context8.stop(); } } }, _callee8, this); })); return function creaRepoLocal(_x7, _x8) { return _ref8.apply(this, arguments); }; }(); var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _octonode = require('octonode'); var _octonode2 = _interopRequireDefault(_octonode); var _simpleGit = require('simple-git'); var _simpleGit2 = _interopRequireDefault(_simpleGit); var _inquirer = require('inquirer'); var _inquirer2 = _interopRequireDefault(_inquirer); 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(options) { var client; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return ghClient(); case 2: client = _context.sent; _context.next = 5; return createRepoGithub(options, client); case 5: _context.next = 7; return getGHUsername(client); case 7: _context.t0 = _context.sent; _context.t1 = options.name; _context.next = 11; return creaRepoLocal(_context.t0, _context.t1); case 11: case 'end': return _context.stop(); } } }, _callee, this); })); function crearRepo(_x) { return _ref.apply(this, arguments); } return crearRepo; }();