roc
Version:
Build modern web applications easily
148 lines (108 loc) • 5.61 kB
JavaScript
'use strict';
exports.__esModule = true;
exports.runCli = runCli;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
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; }
require('source-map-support/register');
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 _documentationBuildDocumentationObject = require('../documentation/build-documentation-object');
var _documentationBuildDocumentationObject2 = _interopRequireDefault(_documentationBuildDocumentationObject);
var _configurationHelpers = require('../configuration/helpers');
var _helpers2 = require('./helpers');
var _helpersStyle = require('../helpers/style');
/**
* 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 extensions and
* application.
* @param {rocMetaConfig} initalMeta - The inital meta configuration, will be merged with the selected extensions.
* @param {string[]} [args=process.argv] - From where it should parse the arguments.
*
* @returns {undefined}
*/
function runCli(info, initalConfig, initalMeta) {
if (info === undefined) info = { version: 'Unknown', name: 'Unknown' };
var args = arguments.length <= 3 || arguments[3] === undefined ? process.argv : arguments[3];
var _minimist = _minimist3['default'](args.slice(2));
var _ = _minimist._;
var h = _minimist.h;
var help = _minimist.help;
var d = _minimist.d;
var debug = _minimist.debug;
var v = _minimist.v;
var version = _minimist.version;
var c = _minimist.c;
var config = _minimist.config;
var D = _minimist.D;
var directory = _minimist.directory;
var restArgs = _objectWithoutProperties(_minimist, ['_', 'h', 'help', 'd', 'debug', 'v', 'version', 'c', 'config', 'D', 'directory']);
// The first should be our command if there is one
var command = _[0];
var options = _.slice(1);
// If version is selected output that and stop
if (version || v) {
return console.log(info.version);
}
// Possibe to set a command in debug mode
var debugEnabled = debug || d ? true : false;
// Get the application configuration path
var applicationConfigPath = config || c;
// Get the directory Path
var directoryPath = _helpers.getAbsolutePath(directory || D);
// Build the complete config object
var applicationConfig = _configurationHelpers.getApplicationConfig(applicationConfigPath, directoryPath, debugEnabled);
var _buildCompleteConfig = _helpers2.buildCompleteConfig(debugEnabled, initalConfig, initalMeta, applicationConfig, undefined, directoryPath);
var extensionConfig = _buildCompleteConfig.extensionConfig;
var configObject = _buildCompleteConfig.config;
var metaObject = _buildCompleteConfig.meta;
// If we have no command we will display some help information about all possible commands
if (!command) {
return console.log(_helpers2.generateCommandsDocumentation(extensionConfig, metaObject));
}
// If the command does not exist show error
// Will ignore application configuration
if (!extensionConfig.commands || !extensionConfig.commands[command]) {
console.log(_helpersStyle.error('Invalid command'), '\n');
return console.log(_helpers2.getSuggestions([command], Object.keys(extensionConfig.commands)), '\n');
}
// Show command help information if requested
// Will ignore application configuration
if (help || h) {
return console.log(_helpers2.generateCommandDocumentation(extensionConfig, metaObject, command, info.name));
}
var parsedOptions = _helpers2.parseOptions(command, metaObject.commands, options);
// Only parse arguments if the command accepts it
if (metaObject.commands[command] && metaObject.commands[command].settings) {
// Get config from application and only parse options that this command cares about.
var filter = metaObject.commands[command].settings === true ? [] : metaObject.commands[command].settings;
var documentationObject = _documentationBuildDocumentationObject2['default'](configObject.settings, metaObject.settings, filter);
var configuration = _helpers2.parseArguments(restArgs, _helpers2.getMappings(documentationObject));
configObject = _configuration.merge(configObject, {
settings: configuration
});
// Validate configuration
_validation.validate(configObject.settings, metaObject.settings, metaObject.commands[command].settings);
}
// Set the configuration object
_configuration.appendConfig(configObject);
// If string run as shell command
if (_lodash.isString(configObject.commands[command])) {
return _execute.execute(configObject.commands[command]);
}
// Run the command
return configObject.commands[command]({
debug: debugEnabled,
configObject: configObject,
metaObject: metaObject,
extensionConfig: extensionConfig,
parsedOptions: parsedOptions
});
}
//# sourceMappingURL=index.js.map