roc
Version:
Build modern web applications easily
310 lines (274 loc) • 22 kB
HTML
<html>
<head>
<meta charset="utf-8">
<base data-ice="baseUrl" href="../../../">
<title data-ice="title">src/commands/init.js | API Document</title>
<link type="text/css" rel="stylesheet" href="css/style.css">
<link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
<script src="script/prettify/prettify.js"></script>
<script src="script/manual.js"></script>
</head>
<body class="layout-container" data-ice="rootContainer">
<header>
<a href="./">Home</a>
<a href="identifiers.html">Reference</a>
<a href="source.html">Source</a>
<a data-ice="repoURL" href="https://github.com/vgno/roc.git" class="repo-url-github">Repository</a>
<div class="search-box">
<span>
<img src="./image/search.png">
<span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
</span>
<ul class="search-result"></ul>
</div>
</header>
<nav class="navigation" data-ice="nav"><div>
<ul>
<li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">cli</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-execute">execute</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-buildCompleteConfig">buildCompleteConfig</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-generateCommandDocumentation">generateCommandDocumentation</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-generateCommandsDocumentation">generateCommandsDocumentation</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getMappings">getMappings</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getSuggestions">getSuggestions</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-parseArguments">parseArguments</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-parseOptions">parseOptions</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-runCli">runCli</a></span></span></li>
<li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">commands</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-init">init</a></span></span></li>
<li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">commands/helpers</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-validRocProject">validRocProject</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-get">get</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getVersions">getVersions</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-defaultPrompt">defaultPrompt</a></span></span></li>
<li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">configuration</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getApplicationConfig">getApplicationConfig</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-appendConfig">appendConfig</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-appendSettings">appendSettings</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getConfig">getConfig</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getSettings">getSettings</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-merge">merge</a></span></span></li>
<li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">documentation</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-buildDocumentationObject">buildDocumentationObject</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-generateTable">generateTable</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-addPadding">addPadding</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getDefaultValue">getDefaultValue</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-pad">pad</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-toCliFlag">toCliFlag</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-generateMarkdownDocumentation">generateMarkdownDocumentation</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-generateTextDocumentation">generateTextDocumentation</a></span></span></li>
<li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">helpers</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-fileExists">fileExists</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getAbsolutePath">getAbsolutePath</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getPackageJson">getPackageJson</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getRocDependencies">getRocDependencies</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-error">error</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-errorLabel">errorLabel</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-important">important</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-importantLabel">importantLabel</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-ok">ok</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-okLabel">okLabel</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-warning">warning</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-warningLabel">warningLabel</a></span></span></li>
<li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">typedef</div><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-infoObject">infoObject</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocCommandObject">rocCommandObject</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocConfig">rocConfig</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocDocumentationObject">rocDocumentationObject</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocMapObject">rocMapObject</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocMetaConfig">rocMetaConfig</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocMetaSettings">rocMetaSettings</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocMetaSettings">rocMetaSettings</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocOptions">rocOptions</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocSettings">rocSettings</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocTableHeader">rocTableHeader</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocTableSettings">rocTableSettings</a></span></span></li>
<li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">validation</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isValid">isValid</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-throwError">throwError</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-validate">validate</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-validateMightThrow">validateMightThrow</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isArray">isArray</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isArrayOrSingle">isArrayOrSingle</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isBoolean">isBoolean</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isInteger">isInteger</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isObject">isObject</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isPath">isPath</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isString">isString</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-oneOf">oneOf</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-required">required</a></span></span></li>
</ul>
</div>
</nav>
<div class="content" data-ice="content"><h1 data-ice="title">src/commands/init.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import 'source-map-support/register';
import fs from 'fs-extra';
import path from 'path';
import { spawn } from 'child_process';
import inquirer from 'inquirer';
import replace from 'replace';
import chalk from 'chalk';
import { get, getVersions } from './helpers/github';
import { validRocProject } from './helpers/general';
import { error as styleError, warning, important, ok } from '../helpers/style';
/* This should be fetched from a server!
*/
const 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.
*/
export default function init({ parsedOptions }) {
const { template, version } = parsedOptions.options;
// Make sure the directory is empty!
assertEmptyDir();
if (!template) {
return interativeMenu();
}
return fetchTemplate(template, version);
/*
* Helpers
*/
function fetchTemplate(toFetch, selectVersion) {
if (toFetch.indexOf('/') === -1) {
const selectedTemplate = templates.find((elem) => elem.identifier === toFetch);
if (!selectedTemplate) {
console.log(styleError('Invalid template name given.'));
/* eslint-disable no-process-exit */
process.exit(1);
/* eslint-enable */
}
toFetch = selectedTemplate.repo;
}
return getVersions(toFetch)
.then((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}`;
}
const selectedVersion = versions.find((v) => v.name === selectVersion);
const actualVersion = selectedVersion && selectedVersion.name ||
versions[0] && versions[0].name ||
'master';
if (!selectedVersion && selectVersion) {
console.log(
warning(`Selected template version not found, using ${chalk.bold(actualVersion)}`)
);
} else if (!selectedVersion) {
console.log(important(`Using ${chalk.bold(actualVersion)} as template version`));
}
return get(toFetch, actualVersion);
})
.then((dirPath) => {
if (!validRocProject(path.join(dirPath, 'template'))) {
/* eslint-disable no-process-exit */
console.log(styleError('Seems like this is not a Roc template.'));
process.exit(1);
/* eslint-enable */
} else {
console.log('\nInstalling template setup dependencies…');
return npmInstall(dirPath);
}
})
.then((dirPath) => {
inquirer.prompt(getPrompt(dirPath), (answers) => {
replaceTemplatedValues(answers, dirPath);
configureFiles(dirPath);
console.log(`\nInstalling template dependencies… ` +
`${chalk.dim('(If this fails you can always try to run npm install directly)')}`);
return npmInstall().then(() => {
console.log(ok('\nSetup completed!\n'));
console.log(`Start in dev mode by typing ${chalk.bold('roc dev')}`);
});
});
})
.catch((error) => {
console.log(styleError('\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(path.join(dirPath, 'roc.setup.js')).prompt;
} catch (error) {
return require('./helpers/default-prompt').defaultPrompt;
}
}
function replaceTemplatedValues(answers, dirPath) {
Object.keys(answers).map((key) => {
replace({
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
fs.renameSync(path.join(dirPath, 'package.json'), path.join(dirPath, 'template', '.roc'));
// Move everything inside template to the current working directory
fs.copySync(path.join(dirPath, 'template'), process.cwd());
}
function npmInstall(dirPath) {
return new Promise((resolve, reject) => {
dirPath = dirPath || process.cwd();
// Run npm install
const npm = 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((resolve) => {
const choices = templates.map((elem) => ({ name: elem.name, value: elem.identifier }));
inquirer.prompt([{
type: 'rawlist',
name: 'option',
message: 'Selected a type',
choices: choices
}], answers => {
resolve(fetchTemplate(answers.option));
});
});
}
function assertEmptyDir() {
if (fs.readdirSync(process.cwd()).length > 0) {
console.log(styleError('You need to call this command from an empty directory.'));
/* eslint-disable no-process-exit */
process.exit(1);
/* eslint-enable */
}
}
}
</code></pre>
</div>
<footer class="footer">
Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.4.3)</span></a>
</footer>
<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>