UNPKG

venmjs

Version:

This is a tool ๐Ÿ”ง that can be installed in your terminal at any time โ›๏ธ it was made for beginners and even for experts, for his utilities, and for a simple creation process ๐Ÿงจ. Every web developer knows how frustrating is to deal with the creation of a ne

398 lines (313 loc) โ€ข 14.8 kB
'use strict'; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _execa = _interopRequireDefault(require("execa")); var _fs = _interopRequireDefault(require("fs")); var _inquirer = _interopRequireDefault(require("inquirer")); var _path = _interopRequireDefault(require("path")); var logger = _interopRequireWildcard(require("../../utils/logger")); var _validate = require("../../utils/validate"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } var template = require('../create/index'); /** * @param {String} dir - Directory of choice * @returns {Promise<void>} */ var createHerokuApp = /*#__PURE__*/function () { var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(dir) { var _yield$inquirer$promp, appName, stderr; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return _inquirer["default"].prompt({ name: 'appName', type: 'input', message: 'Enter a name for the app ๐ŸŒŒ', validate: _validate.validateInput }); case 2: _yield$inquirer$promp = _context.sent; appName = _yield$inquirer$promp.appName; _context.prev = 4; _context.next = 7; return _execa["default"].command("heroku create ".concat(appName), { cwd: dir }); case 7: _context.next = 15; break; case 9: _context.prev = 9; _context.t0 = _context["catch"](4); stderr = _context.t0.stderr; logger.error("\n".concat(stderr, "\n")); _context.next = 15; return createHerokuApp(dir); case 15: case "end": return _context.stop(); } } }, _callee, null, [[4, 9]]); })); return function createHerokuApp(_x) { return _ref.apply(this, arguments); }; }(); /** * @returns {Boolean} */ var isLoggedIn = /*#__PURE__*/function () { var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.prev = 0; _context2.next = 3; return _execa["default"].command('heroku whoami'); case 3: return _context2.abrupt("return", true); case 6: _context2.prev = 6; _context2.t0 = _context2["catch"](0); return _context2.abrupt("return", false); case 9: case "end": return _context2.stop(); } } }, _callee2, null, [[0, 6]]); })); return function isLoggedIn() { return _ref3.apply(this, arguments); }; }(); /** * @param {String} configVar - The config variable * @param {String} dir - Directory of choice * @param {String} value - Supply the value * @returns {Promise<void>} */ var setConfigVar = /*#__PURE__*/function () { var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(configVar, dir, value) { var _yield$execa$command, stdout, _yield$inquirer$promp2, uri; return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return _execa["default"].command('heroku config', { cwd: dir }); case 2: _yield$execa$command = _context3.sent; stdout = _yield$execa$command.stdout; if (stdout.includes(configVar)) { _context3.next = 13; break; } if (!(configVar === 'DB_URL')) { _context3.next = 11; break; } _context3.next = 8; return _inquirer["default"].prompt({ type: 'password', name: 'uri', message: 'Please provide the path to a cloud based MongoDB URI ๐Ÿงพ', validate: _validate.validateInput }); case 8: _yield$inquirer$promp2 = _context3.sent; uri = _yield$inquirer$promp2.uri; value = uri; case 11: _context3.next = 13; return _execa["default"].command("heroku config:set ".concat(configVar, "=").concat(value), { cwd: dir }); case 13: case "end": return _context3.stop(); } } }, _callee3); })); return function setConfigVar(_x2, _x3, _x4) { return _ref4.apply(this, arguments); }; }(); /** * Deploy the webapp to Heroku * * @param {String} templateDir - client/server * @returns {Promise<void>} */ var _default = /*#__PURE__*/function () { var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(templateDir) { var fileContent, _yield$execa$command2, _stdout, _yield$execa$command3, stdout, procFilePath, staticConfig, staticConfigPath, starterSource, pkgJson, buildCmd, postInstallScript, serverFilePath; return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: _context4.next = 2; return (0, _validate.validateInstallation)('heroku'); case 2: _context4.next = 4; return (0, _validate.validateInstallation)('git help -g'); case 4: if (_fs["default"].existsSync(_path["default"].join(templateDir, '.git'))) { _context4.next = 11; break; } _context4.next = 7; return _execa["default"].command('git init', { cwd: templateDir }); case 7: fileContent = _fs["default"].existsSync(_path["default"].join('server', '.env')) ? 'node_modules\n.env' : 'node_modules'; _fs["default"].writeFileSync(_path["default"].join(templateDir, '.gitignore'), fileContent); _context4.next = 16; break; case 11: _context4.next = 13; return _execa["default"].command('git status', { cwd: templateDir }); case 13: _yield$execa$command2 = _context4.sent; _stdout = _yield$execa$command2.stdout; if (_stdout.includes('nothing to commit ๐Ÿ“Œ')) { console.error('No changes detected! ๐Ÿ“Œ'); process.exit(1); } case 16: _context4.next = 18; return isLoggedIn(); case 18: if (_context4.sent) { _context4.next = 21; break; } _context4.next = 21; return _execa["default"].command('heroku login', { stdio: 'inherit', cwd: templateDir }); case 21: _context4.next = 23; return _execa["default"].command('git remote', { cwd: templateDir }); case 23: _yield$execa$command3 = _context4.sent; stdout = _yield$execa$command3.stdout; if (stdout.includes('heroku')) { _context4.next = 28; break; } _context4.next = 28; return createHerokuApp(templateDir); case 28: if (!(templateDir === 'server' && _fs["default"].existsSync(_path["default"].join('server', 'models')))) { _context4.next = 31; break; } _context4.next = 31; return setConfigVar('DB_URL', templateDir); case 31: if (!(templateDir === 'client')) { _context4.next = 51; break; } if (!(template === 'Nuxt 2๏ธโƒฃ')) { _context4.next = 41; break; } _context4.next = 35; return setConfigVar('HOST', templateDir, 'localhost'); case 35: _context4.next = 37; return setConfigVar('NODE_ENV', templateDir, 'production'); case 37: // Create Procfile procFilePath = _path["default"].join('client', 'Procfile'); if (!_fs["default"].existsSync(procFilePath)) { _fs["default"].writeFileSync(procFilePath, 'web: npm run dev'); } _context4.next = 51; break; case 41: staticConfig = { root: 'dist', clean_urls: true, routes: { '/**': 'index.html' } }; staticConfigPath = _path["default"].join('client', 'static.json'); if (!_fs["default"].existsSync(staticConfigPath)) { _fs["default"].writeFileSync(staticConfigPath, JSON.stringify(staticConfig, null, 2)); } starterSource = ['const express = require("express");', 'const serveStatic = require("serve-static");', 'const path = require("path");', 'const app = express();', 'app.use(serveStatic(path.join(__dirname, "dist")));', 'const port = process.env.PORT || 80;', 'app.listen(port);']; pkgJson = JSON.parse(_fs["default"].readFileSync(_path["default"].join('client', 'package.json'), 'utf8')); buildCmd = 'npm run build'; postInstallScript = "if test \"$NODE_ENV\" = \"production\" ; then ".concat(buildCmd, " ; fi "); // eslint-disable-line pkgJson = _objectSpread(_objectSpread({}, pkgJson), {}, { scripts: _objectSpread(_objectSpread({}, pkgJson.scripts), {}, { postinstall: postInstallScript, start: 'node server.js' }) }); serverFilePath = _path["default"].join('client', 'server.js'); if (!_fs["default"].existsSync(serverFilePath)) { _fs["default"].writeFileSync(serverFilePath, starterSource.join('\n')); // Add preinstall script pkgJson.scripts['preinstall'] = 'npm install --save express serve-static'; _fs["default"].writeFileSync('./client/package.json', JSON.stringify(pkgJson, null, 2)); } case 51: _context4.next = 53; return _execa["default"].command('git add .', { cwd: templateDir }); case 53: _context4.next = 55; return (0, _execa["default"])('git', ['commit', '-m', 'Add files'], { cwd: templateDir }); case 55: _context4.next = 57; return _execa["default"].command('git push heroku master', { stdio: 'inherit', cwd: templateDir }); case 57: _context4.next = 59; return _execa["default"].command('heroku open', { cwd: templateDir }); case 59: case "end": return _context4.stop(); } } }, _callee4); })); return function (_x5) { return _ref5.apply(this, arguments); }; }(); exports["default"] = _default;