UNPKG

roc

Version:

Build modern web applications easily

310 lines (274 loc) 22 kB
<!DOCTYPE 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 &apos;source-map-support/register&apos;; import fs from &apos;fs-extra&apos;; import path from &apos;path&apos;; import { spawn } from &apos;child_process&apos;; import inquirer from &apos;inquirer&apos;; import replace from &apos;replace&apos;; import chalk from &apos;chalk&apos;; import { get, getVersions } from &apos;./helpers/github&apos;; import { validRocProject } from &apos;./helpers/general&apos;; import { error as styleError, warning, important, ok } from &apos;../helpers/style&apos;; /* This should be fetched from a server! */ const templates = [{ name: &apos;Simple Roc App&apos;, description: &apos;A simple start on a generic web application&apos;, identifier: &apos;web&apos;, repo: &apos;vgno/roc-template-web&apos; }, { name: &apos;Simple Roc React App&apos;, description: &apos;A simple start on a React web application&apos;, identifier: &apos;web-react&apos;, repo: &apos;vgno/roc-template-web-react&apos; }]; /** * 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(&apos;/&apos;) === -1) { const selectedTemplate = templates.find((elem) =&gt; elem.identifier === toFetch); if (!selectedTemplate) { console.log(styleError(&apos;Invalid template name given.&apos;)); /* eslint-disable no-process-exit */ process.exit(1); /* eslint-enable */ } toFetch = selectedTemplate.repo; } return getVersions(toFetch) .then((versions) =&gt; { // If the name starts with a number we will automatically add &apos;v&apos; infront of it to match Github default if (selectVersion &amp;&amp; !isNaN(Number(selectVersion.charAt(0))) &amp;&amp; selectVersion.charAt(0) !== &apos;v&apos;) { selectVersion = `v${selectVersion}`; } const selectedVersion = versions.find((v) =&gt; v.name === selectVersion); const actualVersion = selectedVersion &amp;&amp; selectedVersion.name || versions[0] &amp;&amp; versions[0].name || &apos;master&apos;; if (!selectedVersion &amp;&amp; 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) =&gt; { if (!validRocProject(path.join(dirPath, &apos;template&apos;))) { /* eslint-disable no-process-exit */ console.log(styleError(&apos;Seems like this is not a Roc template.&apos;)); process.exit(1); /* eslint-enable */ } else { console.log(&apos;\nInstalling template setup dependencies&#x2026;&apos;); return npmInstall(dirPath); } }) .then((dirPath) =&gt; { inquirer.prompt(getPrompt(dirPath), (answers) =&gt; { replaceTemplatedValues(answers, dirPath); configureFiles(dirPath); console.log(`\nInstalling template dependencies&#x2026; ` + `${chalk.dim(&apos;(If this fails you can always try to run npm install directly)&apos;)}`); return npmInstall().then(() =&gt; { console.log(ok(&apos;\nSetup completed!\n&apos;)); console.log(`Start in dev mode by typing ${chalk.bold(&apos;roc dev&apos;)}`); }); }); }) .catch((error) =&gt; { console.log(styleError(&apos;\nAn error occured during init!\n&apos;)); console.error(error.stack); /* eslint-disable no-process-exit */ process.exit(1); /* eslint-enable */ }); } function getPrompt(dirPath) { try { return require(path.join(dirPath, &apos;roc.setup.js&apos;)).prompt; } catch (error) { return require(&apos;./helpers/default-prompt&apos;).defaultPrompt; } } function replaceTemplatedValues(answers, dirPath) { Object.keys(answers).map((key) =&gt; { replace({ regex: `{{\\s*${key}*\\s*}}`, replacement: answers[key], paths: [dirPath + &apos;/template&apos;], recursive: true, silent: true }); }); } function configureFiles(dirPath) { // Rename package.json to .roc for history purposes fs.renameSync(path.join(dirPath, &apos;package.json&apos;), path.join(dirPath, &apos;template&apos;, &apos;.roc&apos;)); // Move everything inside template to the current working directory fs.copySync(path.join(dirPath, &apos;template&apos;), process.cwd()); } function npmInstall(dirPath) { return new Promise((resolve, reject) =&gt; { dirPath = dirPath || process.cwd(); // Run npm install const npm = spawn(&apos;npm&apos;, [&apos;install&apos;], { cwd: dirPath, stdio: &apos;inherit&apos; }); npm.on(&apos;close&apos;, function(code) { if (code !== 0) { return reject(new Error(&apos;npm install failed with status code: &apos; + code)); } return resolve(dirPath); }); }); } function interativeMenu() { return new Promise((resolve) =&gt; { const choices = templates.map((elem) =&gt; ({ name: elem.name, value: elem.identifier })); inquirer.prompt([{ type: &apos;rawlist&apos;, name: &apos;option&apos;, message: &apos;Selected a type&apos;, choices: choices }], answers =&gt; { resolve(fetchTemplate(answers.option)); }); }); } function assertEmptyDir() { if (fs.readdirSync(process.cwd()).length &gt; 0) { console.log(styleError(&apos;You need to call this command from an empty directory.&apos;)); /* 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>