UNPKG

roc

Version:

Build modern web applications easily

211 lines (155 loc) 7.94 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.runCli = runCli; exports.initCli = initCli; var _minimist2 = require('minimist'); var _minimist3 = _interopRequireDefault(_minimist2); var _lodash = require('lodash'); var _execute = require('./execute'); var _helpers = require('../helpers'); var _validation = require('../validation'); var _configuration = require('../configuration'); var _buildDocumentationObject = require('../documentation/build-documentation-object'); var _buildDocumentationObject2 = _interopRequireDefault(_buildDocumentationObject); var _helpers2 = require('../configuration/helpers'); var _helpers3 = require('./helpers'); var _getSuggestions = require('../helpers/get-suggestions'); var _getSuggestions2 = _interopRequireDefault(_getSuggestions); var _style = require('../helpers/style'); var _verbose = require('../helpers/verbose'); var _hooks = require('../hooks'); var _actions = require('../hooks/actions'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } /** * Invokes the Roc cli. * * @param {{version: string, name: string}} info - Information about the cli. * @param {rocConfig} initalConfig - The inital configuration, will be merged with the selected packages and * application. * @param {rocMetaConfig} initalMeta - The inital meta configuration, will be merged with the selected packages. * @param {string[]} [argv=process.argv] - From where it should parse the arguments. * @param {boolean} [invoke=true] - If the a command should be invoked after initing the configuration. * * @returns {object} - Returns what the command is returning. If the command is a string command a promise will be * returned that is resolved when the command has been completed. */ function runCli() { let info = arguments.length <= 0 || arguments[0] === undefined ? { version: 'Unknown', name: 'Unknown' } : arguments[0]; let initalConfig = arguments[1]; let initalMeta = arguments[2]; let argv = arguments.length <= 3 || arguments[3] === undefined ? process.argv : arguments[3]; let invoke = arguments.length <= 4 || arguments[4] === undefined ? true : arguments[4]; var _minimist = (0, _minimist3.default)(argv.slice(2)); const _ = _minimist._; const h = _minimist.h; const help = _minimist.help; const V = _minimist.V; const verbose = _minimist.verbose; const v = _minimist.v; const version = _minimist.version; const c = _minimist.c; const config = _minimist.config; const d = _minimist.d; const directory = _minimist.directory; const restOptions = _objectWithoutProperties(_minimist, ['_', 'h', 'help', 'V', 'verbose', 'v', 'version', 'c', 'config', 'd', 'directory']); // The first should be our command if there is one var _ref = _toArray(_); const command = _ref[0]; const args = _ref.slice(1); // If version is selected output that and stop if (version || v) { return console.log(info.version); } // Possible to set a command in verbose mode const verboseMode = !!(verbose || V); (0, _verbose.setVerbose)(verboseMode); // Get the application configuration path const applicationConfigPath = c || config; // Get the directory path const dirPath = (0, _helpers.getAbsolutePath)(directory || d); // Build the complete config object const applicationConfig = (0, _helpers2.getApplicationConfig)(applicationConfigPath, dirPath, verboseMode); var _buildCompleteConfig = (0, _helpers3.buildCompleteConfig)(verboseMode, applicationConfig, undefined, initalConfig, initalMeta, dirPath, true); let packageConfig = _buildCompleteConfig.packageConfig; let configObject = _buildCompleteConfig.config; let metaObject = _buildCompleteConfig.meta; // If we have no command we will display some help information about all possible commands if (!command) { return console.log((0, _helpers3.generateCommandsDocumentation)(packageConfig, metaObject)); } // If the command does not exist show error // Will ignore application configuration if (!packageConfig.commands || !packageConfig.commands[command]) { return console.log((0, _style.feedbackMessage)((0, _style.errorLabel)('Error', 'Invalid Command'), (0, _getSuggestions2.default)([command], Object.keys(packageConfig.commands)))); } // Show command help information if requested // Will ignore application configuration if (help || h) { return console.log((0, _helpers3.generateCommandDocumentation)(packageConfig, metaObject, command, info.name)); } const parsedArguments = (0, _helpers3.parseArguments)(command, metaObject.commands, args); let documentationObject; // Only parse arguments if the command accepts it if (metaObject.commands && metaObject.commands[command] && metaObject.commands[command].settings) { // Get config from application and only parse options that this command cares about. const filter = metaObject.commands[command].settings === true ? [] : metaObject.commands[command].settings; documentationObject = (0, _buildDocumentationObject2.default)(configObject.settings, metaObject.settings, filter); } var _parseOptions = (0, _helpers3.parseOptions)(restOptions, (0, _helpers3.getMappings)(documentationObject), command, metaObject.commands); const settings = _parseOptions.settings; const parsedOptions = _parseOptions.parsedOptions; configObject = (0, _configuration.merge)(configObject, { settings }); // Validate configuration if (metaObject.commands && metaObject.commands[command] && metaObject.commands[command].settings) { (0, _validation.validate)(configObject.settings, metaObject.settings, metaObject.commands[command].settings); } // Set the configuration object (0, _configuration.appendConfig)(configObject); // Run hook to make it possible for extensions to update the settings before anything other uses them (0, _hooks.runHookDirectly)({ extension: 'roc', name: 'update-settings' }, [configObject.settings], newSettings => (0, _configuration.appendSettings)(newSettings)); if (invoke) { // If string run as shell command if ((0, _lodash.isString)(configObject.commands[command])) { return (0, _execute.execute)(configObject.commands[command]).catch(process.exit); } // Run the command return configObject.commands[command]({ verbose: verboseMode, directory: dirPath, info, configObject, metaObject, packageConfig, parsedArguments, parsedOptions, hooks: (0, _hooks.getHooks)(), actions: (0, _actions.getActions)() }); } } /** * Small helper for convenience to init the Roc cli, wraps {@link runCli}. * * Will enable source map support and better error handling for promises. * * @param {string} version - The version to be used when requested for information. * @param {string} name - The name to be used when display feedback to the user. * * @returns {object} - Returns what the command is returning. If the command is a string command a promise will be * returned that is resolved when the command has been completed. */ function initCli(version, name) { require('source-map-support').install(); require('loud-rejection')(); return runCli({ version: version, name: name }); } //# sourceMappingURL=index.js.map