UNPKG

roc

Version:

Build modern web applications easily

216 lines (175 loc) 7.5 kB
'use strict'; exports.__esModule = true; exports['default'] = init; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } require('source-map-support/register'); var _fsExtra = require('fs-extra'); var _fsExtra2 = _interopRequireDefault(_fsExtra); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _child_process = require('child_process'); var _inquirer = require('inquirer'); var _inquirer2 = _interopRequireDefault(_inquirer); var _replace = require('replace'); var _replace2 = _interopRequireDefault(_replace); var _chalk = require('chalk'); var _chalk2 = _interopRequireDefault(_chalk); var _helpersGithub = require('./helpers/github'); var _helpersGeneral = require('./helpers/general'); var _helpersStyle = require('../helpers/style'); /* This should be fetched from a server! */ var templates = [{ name: 'Simple Roc App', description: 'A simple start on a generic web application', identifier: 'web', repo: 'vgno/roc-template-web' }, { name: 'Simple Roc React App', description: 'A simple start on a React web application', identifier: 'web-react', repo: 'vgno/roc-template-web-react' }]; /** * Command used to init a new Roc project. * * @param {rocCommandObject} parsedArguments - The Roc command object, uses parsedArguments from it. * * @returns {Promise} - Promise for the command. */ function init(_ref) { var parsedOptions = _ref.parsedOptions; var _parsedOptions$options = parsedOptions.options; var template = _parsedOptions$options.template; var version = _parsedOptions$options.version; // Make sure the directory is empty! assertEmptyDir(); if (!template) { return interativeMenu(); } return fetchTemplate(template, version); /* * Helpers */ function fetchTemplate(toFetch, selectVersion) { if (toFetch.indexOf('/') === -1) { var selectedTemplate = templates.find(function (elem) { return elem.identifier === toFetch; }); if (!selectedTemplate) { console.log(_helpersStyle.error('Invalid template name given.')); /* eslint-disable no-process-exit */ process.exit(1); /* eslint-enable */ } toFetch = selectedTemplate.repo; } return _helpersGithub.getVersions(toFetch).then(function (versions) { // If the name starts with a number we will automatically add 'v' infront of it to match Github default if (selectVersion && !isNaN(Number(selectVersion.charAt(0))) && selectVersion.charAt(0) !== 'v') { selectVersion = 'v' + selectVersion; } var selectedVersion = versions.find(function (v) { return v.name === selectVersion; }); var actualVersion = selectedVersion && selectedVersion.name || versions[0] && versions[0].name || 'master'; if (!selectedVersion && selectVersion) { console.log(_helpersStyle.warning('Selected template version not found, using ' + _chalk2['default'].bold(actualVersion))); } else if (!selectedVersion) { console.log(_helpersStyle.important('Using ' + _chalk2['default'].bold(actualVersion) + ' as template version')); } return _helpersGithub.get(toFetch, actualVersion); }).then(function (dirPath) { if (!_helpersGeneral.validRocProject(_path2['default'].join(dirPath, 'template'))) { /* eslint-disable no-process-exit */ console.log(_helpersStyle.error('Seems like this is not a Roc template.')); process.exit(1); /* eslint-enable */ } else { console.log('\nInstalling template setup dependencies…'); return npmInstall(dirPath); } }).then(function (dirPath) { _inquirer2['default'].prompt(getPrompt(dirPath), function (answers) { replaceTemplatedValues(answers, dirPath); configureFiles(dirPath); console.log('\nInstalling template dependencies… ' + ('' + _chalk2['default'].dim('(If this fails you can always try to run npm install directly)'))); return npmInstall().then(function () { console.log(_helpersStyle.ok('\nSetup completed!\n')); console.log('Start in dev mode by typing ' + _chalk2['default'].bold('roc dev')); }); }); })['catch'](function (error) { console.log(_helpersStyle.error('\nAn error occured during init!\n')); console.error(error.stack); /* eslint-disable no-process-exit */ process.exit(1); /* eslint-enable */ }); } function getPrompt(dirPath) { try { return require(_path2['default'].join(dirPath, 'roc.setup.js')).prompt; } catch (error) { return require('./helpers/default-prompt').defaultPrompt; } } function replaceTemplatedValues(answers, dirPath) { Object.keys(answers).map(function (key) { _replace2['default']({ regex: '{{\\s*' + key + '*\\s*}}', replacement: answers[key], paths: [dirPath + '/template'], recursive: true, silent: true }); }); } function configureFiles(dirPath) { // Rename package.json to .roc for history purposes _fsExtra2['default'].renameSync(_path2['default'].join(dirPath, 'package.json'), _path2['default'].join(dirPath, 'template', '.roc')); // Move everything inside template to the current working directory _fsExtra2['default'].copySync(_path2['default'].join(dirPath, 'template'), process.cwd()); } function npmInstall(dirPath) { return new Promise(function (resolve, reject) { dirPath = dirPath || process.cwd(); // Run npm install var npm = _child_process.spawn('npm', ['install'], { cwd: dirPath, stdio: 'inherit' }); npm.on('close', function (code) { if (code !== 0) { return reject(new Error('npm install failed with status code: ' + code)); } return resolve(dirPath); }); }); } function interativeMenu() { return new Promise(function (resolve) { var choices = templates.map(function (elem) { return { name: elem.name, value: elem.identifier }; }); _inquirer2['default'].prompt([{ type: 'rawlist', name: 'option', message: 'Selected a type', choices: choices }], function (answers) { resolve(fetchTemplate(answers.option)); }); }); } function assertEmptyDir() { if (_fsExtra2['default'].readdirSync(process.cwd()).length > 0) { console.log(_helpersStyle.error('You need to call this command from an empty directory.')); /* eslint-disable no-process-exit */ process.exit(1); /* eslint-enable */ } } } module.exports = exports['default']; //# sourceMappingURL=init.js.map