From 8e11b58cf9feed3cf036fee36bdc687a9942a9f1 Mon Sep 17 00:00:00 2001 From: Honza Javorek Date: Tue, 5 Feb 2019 17:06:33 +0100 Subject: [PATCH] refactor: properly deprecate options --- lib/CLI.js | 28 +++++----- lib/configuration.js | 126 ++++++++++++++++++++++++++++--------------- 2 files changed, 96 insertions(+), 58 deletions(-) diff --git a/lib/CLI.js b/lib/CLI.js index 5df6ecd83..2372d2ecc 100644 --- a/lib/CLI.js +++ b/lib/CLI.js @@ -269,22 +269,22 @@ ${packageData.name} v${packageData.version} \ } run() { - for (const task of [ - this.setOptimistArgv, - this.parseCustomConfig, - this.runExitingActions, - this.loadDreddFile, - this.checkRequiredArgs, - this.moveBlueprintArgToPath, - ]) { - task.call(this); - if (this.finished) { return; } - } + try { + for (const task of [ + this.setOptimistArgv, + this.parseCustomConfig, + this.runExitingActions, + this.loadDreddFile, + this.checkRequiredArgs, + this.moveBlueprintArgToPath, + ]) { + task.call(this); + if (this.finished) { return; } + } - const configurationForDredd = this.initConfig(); - this.logDebuggingInfo(configurationForDredd); + const configurationForDredd = this.initConfig(); + this.logDebuggingInfo(configurationForDredd); - try { this.dreddInstance = this.initDredd(configurationForDredd); } catch (e) { this.exitWithStatus(e); diff --git a/lib/configuration.js b/lib/configuration.js index fd14d9ba9..a4cbac344 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -6,17 +6,21 @@ const reporterOutputLogger = require('./reporters/reporterOutputLogger'); function coerceToArray(value) { - if (Array.isArray(value)) { - return value; - } if (typeof value === 'string') { - return [value]; - } if ((value == null)) { - return []; - } + if (Array.isArray(value)) return value; + if (typeof value === 'string') return [value]; + if (value == null) return []; return value; } +function coerceToBoolean(value) { + if (value === 'true') return true; + if (value === 'false') return true; + if (value) return true; + return false; +} + + function applyLoggingOptions(options) { if (options.color === false) { logger.transports.console.colorize = false; @@ -36,7 +40,9 @@ function applyLoggingOptions(options) { } else if (['warn', 'error'].includes(loglevel)) { logger.transports.console.level = loglevel; } else { - throw new Error(`Unsupported logging level: '${loglevel}'`); + logger.transports.console.level = 'warn'; + throw new Error(`The logging level '${loglevel}' is unsupported, ` + + 'supported are: silent, error, warning, debug'); } } else { logger.transports.console.level = 'warn'; @@ -44,46 +50,67 @@ function applyLoggingOptions(options) { } -function errorOnRemovedOptions(config = {}) { +function coerceRemovedOptions(config = {}) { + const coercedConfig = clone(config); + + const errors = []; + const warnings = []; + if (config.options) { if (config.options.c) { - throw new Error('Dredd does not support ' + warnings.push('DEPRECATED: Dredd does not support ' + '-c anymore, use --color/--no-color instead'); + coercedConfig.options.color = coerceToBoolean(config.options.c); } if (typeof config.options.color === 'string') { - throw new Error('Dredd does not support ' + warnings.push('DEPRECATED: Dredd does not support ' + `--color=${config.options.color} anymore, use --color/--no-color instead`); + coercedConfig.options.color = coerceToBoolean(config.options.color); } if (config.options.level) { - throw new Error('Dredd does not support ' + warnings.push('DEPRECATED: Dredd does not support ' + '--level anymore, use --loglevel instead'); + if (['silly', 'debug', 'verbose'].includes(config.options.level)) { + coercedConfig.options.loglevel = 'debug'; + } else if (config.options.level === 'error') { + coercedConfig.options.loglevel = 'error'; + } else { + coercedConfig.options.loglevel = 'warn'; + } } if (config.options.timestamp || config.options.t) { - throw new Error('Dredd does not support ' + warnings.push('DEPRECATED: Dredd does not support ' + '--timestamp anymore, use --loglevel=debug instead'); + coercedConfig.options.loglevel = 'debug'; } if (config.options.silent || config.options.q) { - throw new Error('Dredd does not support ' + warnings.push('DEPRECATED: Dredd does not support ' + '-q/--silent anymore, use --loglevel=silent instead'); + coercedConfig.options.loglevel = 'silent'; } if (config.options.sandbox || config.options.b) { - throw new Error('Dredd does not support ' + errors.push('REMOVED: Dredd does not support ' + 'sandboxed JS hooks anymore, use standard JS hooks instead'); } } if (config.hooksData) { - throw new Error('Dredd does not support ' + errors.push('REMOVED: Dredd does not support ' + 'sandboxed JS hooks anymore, use standard JS hooks instead'); } if (config.blueprintPath) { - throw new Error('Dredd does not support ' + warnings.push('DEPRECATED: Dredd does not support ' + "the 'blueprintPath' option anymore, use 'path' instead"); + coercedConfig.options = coercedConfig.options || {}; + coercedConfig.options.path = coercedConfig.path || []; + coercedConfig.options.path.push(config.blueprintPath); } + + return { config: coercedConfig, errors, warnings }; } -function applyConfiguration(config) { - const configuration = { +function applyConfiguration(inConfig) { + const outConfig = { server: null, emitter: new EventEmitter(), custom: { // Used for custom settings of various APIs or reporters @@ -94,7 +121,6 @@ function applyConfiguration(config) { 'dry-run': false, reporter: null, output: null, - debug: false, header: null, user: null, 'inline-errors': false, @@ -118,41 +144,53 @@ function applyConfiguration(config) { }, }; - // Normalize options and config - for (const key of Object.keys(config || {})) { - // Copy anything except "custom" hash - const value = config[key]; + // Gracefully deal with the removed options + const coerceResult = coerceRemovedOptions(inConfig); + inConfig = coerceResult.config; + + // Normalize + Object.keys(inConfig).forEach((key) => { + // Copy anything except the "custom" object + const value = inConfig[key]; if (key !== 'custom') { - configuration[key] = value; + outConfig[key] = value; } else { - if (!configuration.custom) { configuration.custom = {}; } - const object = config.custom || {}; + if (!outConfig.custom) { outConfig.custom = {}; } + const object = inConfig.custom || {}; for (const customKey of Object.keys(object || {})) { const customVal = object[customKey]; - configuration.custom[customKey] = clone(customVal, false); + outConfig.custom[customKey] = clone(customVal, false); } } - } + }); // Coerce single/multiple options into an array - configuration.options.reporter = coerceToArray(configuration.options.reporter); - configuration.options.output = coerceToArray(configuration.options.output); - configuration.options.header = coerceToArray(configuration.options.header); - configuration.options.method = coerceToArray(configuration.options.method); - configuration.options.only = coerceToArray(configuration.options.only); - configuration.options.path = coerceToArray(configuration.options.path); - - configuration.options.method = configuration.options.method.map(method => method.toUpperCase()); - - if (configuration.options.user) { - const authHeader = `Authorization:Basic ${Buffer.from(configuration.options.user).toString('base64')}`; - configuration.options.header.push(authHeader); + outConfig.options.reporter = coerceToArray(outConfig.options.reporter); + outConfig.options.output = coerceToArray(outConfig.options.output); + outConfig.options.header = coerceToArray(outConfig.options.header); + outConfig.options.method = coerceToArray(outConfig.options.method); + outConfig.options.only = coerceToArray(outConfig.options.only); + outConfig.options.path = coerceToArray(outConfig.options.path); + + outConfig.options.method = outConfig.options.method.map(method => method.toUpperCase()); + + if (outConfig.options.user) { + const authHeader = `Authorization:Basic ${Buffer.from(outConfig.options.user).toString('base64')}`; + outConfig.options.header.push(authHeader); } - applyLoggingOptions(configuration.options); - errorOnRemovedOptions(config); + applyLoggingOptions(outConfig.options); + + // Log accumulated errors and warnings + coerceResult.errors.forEach(message => logger.error(message)); + coerceResult.warnings.forEach(message => logger.warn(message)); + + // Abort Dredd if there has been any errors + if (coerceResult.errors.length) { + throw new Error('Could not configure Dredd'); + } - return configuration; + return outConfig; }