Skip to content

Commit

Permalink
Merge 09716d7 into b5c0baa
Browse files Browse the repository at this point in the history
  • Loading branch information
npetruzzelli authored Jan 26, 2021
2 parents b5c0baa + 09716d7 commit 3a1cd83
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,26 @@ const CONFIG_SYNTAX_HELP = ' module.exports = function(config) {\n' +
' });\n' +
' };\n'

function parseConfig (configFilePath, cliOptions) {
function parseConfig (configFilePath, cliOptions, parseOptions) {
function fail () {
log.error(...arguments)
if (parseOptions && parseOptions.throwErrors === true) {
const errorMessage = Array.from(arguments).join(' ')
throw new Error(errorMessage)
} else {
const warningMessage =
'The `parseConfig()` function historically called `process.exit(1)`' +
' when it failed. This behavior is now deprecated and function will' +
' throw an error in the next major release. To suppress this warning' +
' pass `throwErrors: true` as a third argument to opt-in into the new' +
' behavior and adjust your code to respond to the exception' +
' accordingly.' +
' Example: `parseConfig(path, cliOptions, { throwErrors: true })`'
log.warn(warningMessage)
process.exit(1)
}
}

let configModule
if (configFilePath) {
try {
Expand All @@ -360,21 +379,22 @@ function parseConfig (configFilePath, cliOptions) {
configModule = configModule.default
}
} catch (e) {
log.error('Error in config file!\n ' + e.stack || e)
const errorMessageSegments = []
errorMessageSegments.push('Error in config file!\n ' + e.stack || e)

const extension = path.extname(configFilePath)
if (extension === '.coffee' && !COFFEE_SCRIPT_AVAILABLE) {
log.error('You need to install CoffeeScript.\n npm install coffeescript --save-dev')
errorMessageSegments.push('You need to install CoffeeScript.\n npm install coffeescript --save-dev')
} else if (extension === '.ls' && !LIVE_SCRIPT_AVAILABLE) {
log.error('You need to install LiveScript.\n npm install LiveScript --save-dev')
errorMessageSegments.push('You need to install LiveScript.\n npm install LiveScript --save-dev')
} else if (extension === '.ts' && !TYPE_SCRIPT_AVAILABLE) {
log.error('You need to install TypeScript.\n npm install typescript ts-node --save-dev')
errorMessageSegments.push('You need to install TypeScript.\n npm install typescript ts-node --save-dev')
}
return process.exit(1)
const configModuleImportErrorMessage = errorMessageSegments.join('\n')
return fail(configModuleImportErrorMessage)
}
if (!helper.isFunction(configModule)) {
log.error('Config file must export a function!\n' + CONFIG_SYNTAX_HELP)
return process.exit(1)
return fail('Config file must export a function!\n' + CONFIG_SYNTAX_HELP)
}
} else {
configModule = () => {} // if no config file path is passed, we define a dummy config module.
Expand All @@ -395,8 +415,7 @@ function parseConfig (configFilePath, cliOptions) {
try {
configModule(config)
} catch (e) {
log.error('Error in config file!\n', e)
return process.exit(1)
return fail('Error in config file!\n', e)
}

// merge the config from config file and cliOptions (precedence)
Expand Down

0 comments on commit 3a1cd83

Please sign in to comment.