UNPKG

roc

Version:

Build modern web applications easily

603 lines (526 loc) 32 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../../"> <title data-ice="title">src/cli/helpers.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/cli/helpers.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 chalk from &apos;chalk&apos;; import { isPlainObject, isBoolean, isString, set, difference } from &apos;lodash&apos;; import resolve from &apos;resolve&apos;; import leven from &apos;leven&apos;; import trimNewlines from &apos;trim-newlines&apos;; import redent from &apos;redent&apos;; import { merge } from &apos;../configuration&apos;; import buildDocumentationObject from &apos;../documentation/build-documentation-object&apos;; import generateTable from &apos;../documentation/generate-table&apos;; import { getDefaultValue } from &apos;../documentation/helpers&apos;; import { fileExists, getRocDependencies, getPackageJson } from &apos;../helpers&apos;; import { throwError } from &apos;../validation&apos;; import { isValid } from &apos;../validation&apos;; import { warning, importantLabel, errorLabel, warningLabel } from &apos;../helpers/style&apos;; /** * Builds the complete configuration objects. * * @param {boolean} debug - If debug mode should be enabled, logs some extra information. * @param {rocConfig} config - The base configuration. * @param {rocMetaConfig} meta - The base meta configuration. * @param {rocConfig} newConfig - The new configuration to base the merge on. * @param {rocMetaConfig} newMeta - The new meta configuration to base the merge on. * @param {string} [directory=process.cwd()] - The directory to resolve relative paths from. * @param {boolean} [validate=true] - If the newConfig and the newMeta structure should be validated. * * @returns {Object} - The result of with the built configurations. * @property {rocConfig} extensionConfig - The extensions merged configurations * @property {rocConfig} config - The final configuration, with application configuration. * @property {rocMetaConfig} meta - The merged meta configuration. */ export function buildCompleteConfig( debug, config = {}, meta = {}, newConfig = {}, newMeta = {}, directory = process.cwd(), validate = true ) { let finalConfig = { ...config }; let finalMeta = { ...meta }; let usedExtensions = []; const mergeExtension = (extensionName) =&gt; { const { baseConfig, metaConfig = {} } = getExtension(extensionName, directory); if (baseConfig) { usedExtensions.push(extensionName); finalConfig = merge(finalConfig, baseConfig); finalMeta = merge(finalMeta, metaConfig); } }; if (fileExists(&apos;package.json&apos;, directory)) { // If extensions are defined we will use them to merge the configurations if (newConfig.extensions &amp;&amp; newConfig.extensions.length) { newConfig.extensions.forEach(mergeExtension); } else { const packageJson = getPackageJson(directory); getRocDependencies(packageJson) .forEach(mergeExtension); } if (usedExtensions.length &amp;&amp; debug) { console.log(importantLabel(&apos;The following Roc extensions will be used:&apos;), usedExtensions, &apos;\n&apos;); } // Check for a mismatch between application configuration and extensions. if (validate) { if (Object.keys(newConfig).length) { console.log(validateConfigurationStructure(finalConfig, newConfig)); } if (Object.keys(newMeta).length) { console.log(validateConfigurationStructure(finalMeta, newMeta)); } } } return { extensionConfig: finalConfig, config: merge(finalConfig, newConfig), meta: merge(finalMeta, newMeta) }; } function getExtension(extensionName, directory) { try { const { baseConfig, metaConfig } = require(resolve.sync(extensionName, { basedir: directory })); return { baseConfig, metaConfig }; } catch (err) { console.log( errorLabel( &apos;Failed to load Roc extension &apos; + chalk.bold(extensionName) + &apos;. &apos; + &apos;Make sure you have it installed. Try running:&apos; ) + &apos; &apos; + chalk.underline(&apos;npm install --save &apos; + extensionName) , &apos;\n&apos;); return {}; } } function validateConfigurationStructure(config, applicationConfig) { const getKeys = (obj, oldPath = &apos;&apos;, allKeys = []) =&gt; { Object.keys(obj).forEach((key) =&gt; { const value = obj[key]; const newPath = oldPath + key; if (isPlainObject(value)) { getKeys(value, newPath + &apos;.&apos;, allKeys); } else { allKeys.push(newPath); } }); return allKeys; }; const info = []; const keys = getKeys(config); const diff = difference(getKeys(applicationConfig), keys); if (diff.length &gt; 0) { info.push(errorLabel(&apos;Configuration problem&apos;) + &apos; There was a mismatch in the application configuration structure, make sure this is correct.\n&apos;); info.push(getSuggestions(diff, keys)); info.push(&apos;&apos;); } // } return info.join(&apos;\n&apos;); } /** * Will create a string with suggestions for possible typos. * * @param {string[]} current - The current values that might be incorrect. * @param {string[]} possible - All the possible correct values. * @param {boolean} [command=false] - If the suggestion should be managed as a command. * * @returns {string} - A string with possible suggestions for typos. */ export function getSuggestions(current, possible, command = false) { const info = []; current.forEach((currentKey) =&gt; { let shortest = 0; let closest; for (let key of possible) { let distance = leven(currentKey, key); if (distance &lt;= 0 || distance &gt; 4) { continue; } if (shortest &amp;&amp; distance &gt;= shortest) { continue; } closest = key; shortest = distance; } const extra = command ? &apos;--&apos; : &apos;&apos;; if (closest) { info.push(&apos;Did not understand &apos; + chalk.underline(extra + currentKey) + &apos; - Did you mean &apos; + chalk.underline(extra + closest)); } else { info.push(&apos;Did not understand &apos; + chalk.underline(extra + currentKey)); } }); return info.join(&apos;\n&apos;); } /** * Generates a string with information about all the possible commands. * * @param {rocConfig} commands - The Roc config object, uses commands from it. * @param {rocMetaConfig} commandsmeta - The Roc meta config object, uses commands from it. * * @returns {string} - A string with documentation based on the available commands. */ export function generateCommandsDocumentation({ commands }, { commands: commandsMeta }) { const header = { name: true, description: true }; const noCommands = {&apos;No commands available.&apos;: &apos;&apos;}; commandsMeta = commandsMeta || {}; let body = [{ name: &apos;Commands&apos;, objects: Object.keys(commands || noCommands).map((command) =&gt; { const options = commandsMeta[command] ? &apos; &apos; + getCommandOptionsAsString(commandsMeta[command]) : &apos;&apos;; const description = commandsMeta[command] &amp;&amp; commandsMeta[command].description ? commandsMeta[command].description : &apos;&apos;; return { name: (command + options), description }; }) }]; return generateCommandDocsHelper(body, header, &apos;Options&apos;, &apos;name&apos;); } function getCommandOptionsAsString(command = {}) { let options = &apos;&apos;; (command.options || []).forEach((option) =&gt; { options += option.required ? `&lt;${option.name}&gt; ` : `[${option.name}] `; }); return options; } /** * Generates a string with information about a specific command. * * @param {rocConfig} settings - The Roc config object, uses settings from it. * @param {rocMetaConfig} commands+meta - The Roc meta config object, uses commands and settings from it. * @param {string} command - The selected command. * @param {string} name - The name of the cli. * * @returns {string} - A string with documentation based on the selected commands. */ export function generateCommandDocumentation({ settings }, { commands = {}, settings: meta }, command, name) { const rows = []; rows.push(&apos;Usage: &apos; + name + &apos; &apos; + command + &apos; &apos; + getCommandOptionsAsString(commands[command])); rows.push(&apos;&apos;); if (commands[command] &amp;&amp; commands[command].help) { rows.push(redent(trimNewlines(commands[command].help))); rows.push(&apos;&apos;); } let body = []; // Generate the options table if (commands[command] &amp;&amp; commands[command].settings) { rows.push(&apos;Options:&apos;); rows.push(&apos;&apos;); const filter = commands[command].settings === true ? [] : commands[command].settings; body = buildDocumentationObject(settings, meta, filter); } const header = { cli: true, description: { name: &apos;Description&apos;, padding: false }, defaultValue: { name: &apos;Default&apos;, renderer: (input) =&gt; { input = getDefaultValue(input); if (input === undefined) { return &apos;&apos;; } if (!input) { return warning(&apos;No default value&apos;); } return chalk.cyan(input); } } }; rows.push(generateCommandDocsHelper(body, header, &apos;CLI options&apos;, &apos;cli&apos;)); return rows.join(&apos;\n&apos;); } function generateCommandDocsHelper(body, header, options, name) { body.push({ name: options, objects: [{ [name]: &apos;-h, --help&apos;, description: &apos;Output usage information.&apos; }, { [name]: &apos;-v, --version&apos;, description: &apos;Output version number.&apos; }, { [name]: &apos;-d, --debug&apos;, description: &apos;Enable debug mode.&apos; }, { [name]: &apos;-c, --config&apos;, description: `Path to configuration file, will default to ${chalk.bold(&apos;roc.config.js&apos;)} in current ` + `working directory.` }, { [name]: &apos;-D, --directory&apos;, description: &apos;Path to working directory, will default to the current working directory. Can be either &apos; + &apos;absolute or relative.&apos; }] }); return generateTable(body, header, { compact: true, titleWrapper: (input) =&gt; input + &apos;:&apos;, cellDivider: &apos;&apos;, rowWrapper: (input) =&gt; `${input}`, header: false, groupTitleWrapper: (input) =&gt; input + &apos;:&apos; }); } /** * Parses options and validates them. * * @param {string} command - The command to parse options for. * @param {Object} commands - commands from {@link rocMetaConfig}. * @param {Object[]} options - Options parsed by minimist. * * @returns {Object} - Parsed options. * @property {object[]} options - The parsed options that was matched against the meta configuration for the command. * @property {object[]} rest - The rest of the options that could not be matched against the configuration. */ export function parseOptions(command, commands, options) { // If the command supports options if (commands[command] &amp;&amp; commands[command].options) { let parsedOptions = {}; commands[command].options.forEach((option, index) =&gt; { const value = options[index]; if (option.required &amp;&amp; !value) { throw new Error(`Required option &quot;${option.name}&quot; was not provided.`); } if (value &amp;&amp; option.validation) { const validationResult = isValid(value, option.validation); if (validationResult !== true) { try { throwError(option.name, validationResult, value, &apos;option&apos;); } catch (err) { /* eslint-disable no-process-exit, no-console */ console.log(errorLabel(&apos;Arguments problem&apos;) + &apos; An option was not valid.\n&apos;); console.log(err.message); process.exit(1); /* eslint-enable */ } } } parsedOptions[option.name] = value; }); return { options: parsedOptions, rest: options.splice(Object.keys(parsedOptions).length) }; } return { options: undefined, rest: options }; } /** * Creates mappings between cli commands to their &quot;path&quot; in the configuration structure, their validator and type * convertor. * * @param {rocDocumentationObject} documentationObject - Documentation object to create mappings for. * * @returns {Object} - Properties are the cli command without leading dashes that maps to a {@link rocMapObject}. */ export function getMappings(documentationObject) { const recursiveHelper = (groups) =&gt; { let mappings = {}; groups.forEach((group) =&gt; { group.objects.forEach((element) =&gt; { // Remove the two dashes in the beginning to match correctly mappings[element.cli.substr(2)] = { name: element.cli, path: element.path, convertor: getConvertor(element.defaultValue, element.cli), validator: element.validator }; }); mappings = Object.assign({}, mappings, recursiveHelper(group.children)); }); return mappings; }; return recursiveHelper(documentationObject); } // Convert values based on their default value function getConvertor(value, name) { if (isBoolean(value)) { return (input) =&gt; { if (isBoolean(input)) { return input; } if (input === &apos;true&apos; || input === &apos;false&apos;) { return input === &apos;true&apos;; } console.log( warningLabel(`Invalid value given for ${chalk.bold(name)}.`), `Will use the default ${chalk.bold(value)}.` ); return value; }; } else if (Array.isArray(value)) { return (input) =&gt; { let parsed; try { parsed = JSON.parse(input); } catch (err) { // Ignore this case } if (Array.isArray(parsed)) { return parsed; } return input.toString().split(&apos;,&apos;); }; } else if (Number.isInteger(value)) { return (input) =&gt; parseInt(input, 10); } else if (!isString(value) &amp;&amp; (!value || Object.keys(value).length === 0)) { return (input) =&gt; JSON.parse(input); } return (input) =&gt; input; } /** * Converts a set of arguments to {@link rocConfigSettings} object. * * @param {Object} args - Arguments parsed from minimist. * @param {Object} mappings - Result from {@link getMappings}. * * @returns {Object} - The mapped Roc configuration settings object. */ export function parseArguments(args, mappings) { const config = {}; const info = []; Object.keys(args).forEach((key) =&gt; { if (mappings[key]) { const value = convert(args[key], mappings[key]); set(config, mappings[key].path, value); } else { // We did not find a match info.push(getSuggestions([key], Object.keys(mappings), true)); } }); if (info.length &gt; 0) { console.log(errorLabel(&apos;CLI problem&apos;), &apos;Some commands were not understood.\n&apos;); console.log(info.join(&apos;\n&apos;) + &apos;\n&apos;); } return config; } function convert(value, mapping) { const val = mapping.convertor(value); const validationResult = isValid(val, mapping.validator); if (validationResult === true) { return val; } console.log( warning(`There was a problem when trying to automatically convert ${chalk.bold(mapping.name)}. This ` + `value will be ignored.`) ); console.log( `Received ${chalk.underline(value)} and it was converted to ${chalk.underline(val)}.`, validationResult, &apos;\n&apos; ); } </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>