From 03ea13242cd9db34b563b7b45376d0789185393e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iiro=20Ja=CC=88ppinen?= Date: Wed, 27 Nov 2019 08:10:39 +0200 Subject: [PATCH] refactor: linStaged is an async function --- lib/index.js | 92 ++++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/lib/index.js b/lib/index.js index 53827a493..1e76a61ac 100644 --- a/lib/index.js +++ b/lib/index.js @@ -53,10 +53,10 @@ function loadConfig(configPath) { * * @returns {Promise} Promise of whether the linting passed or failed */ -module.exports = function lintStaged( +module.exports = async function lintStaged( { configPath, - config, + config: configObject, maxArgLength, relative = false, shell = false, @@ -65,54 +65,54 @@ module.exports = function lintStaged( } = {}, logger = console ) { - debugLog('Loading config using `cosmiconfig`') - - return (config ? Promise.resolve({ config, filepath: '(input)' }) : loadConfig(configPath)) - .then(result => { - if (result == null) throw errConfigNotFound + try { + debugLog('Loading config using `cosmiconfig`') - debugLog('Successfully loaded config from `%s`:\n%O', result.filepath, result.config) - // result.config is the parsed configuration object - // result.filepath is the path to the config file that was found - const config = validateConfig(result.config) - if (debug) { - // Log using logger to be able to test through `consolemock`. - logger.log('Running lint-staged with the following config:') - logger.log(stringifyObject(config, { indent: ' ' })) - } else { - // We might not be in debug mode but `DEBUG=lint-staged*` could have - // been set. - debugLog('lint-staged config:\n%O', config) - } + const resolved = configObject + ? { config: configObject, filepath: '(input)' } + : await loadConfig(configPath) + if (resolved == null) throw errConfigNotFound - return runAll({ config, maxArgLength, relative, shell, quiet, debug }, logger) - .then(() => { - debugLog('tasks were executed successfully!') - return Promise.resolve(true) - }) - .catch(error => { - printErrors(error, logger) - return Promise.resolve(false) - }) - }) - .catch(err => { - if (err === errConfigNotFound) { - logger.error(`${err.message}.`) - } else { - // It was probably a parsing error - logger.error(dedent` - Could not parse lint-staged config. + debugLog('Successfully loaded config from `%s`:\n%O', resolved.filepath, resolved.config) + // resolved.config is the parsed configuration object + // resolved.filepath is the path to the config file that was found + const config = validateConfig(resolved.config) + if (debug) { + // Log using logger to be able to test through `consolemock`. + logger.log('Running lint-staged with the following config:') + logger.log(stringifyObject(config, { indent: ' ' })) + } else { + // We might not be in debug mode but `DEBUG=lint-staged*` could have + // been set. + debugLog('lint-staged config:\n%O', config) + } - ${err} - `) - } - logger.error() // empty line - // Print helpful message for all errors + try { + await runAll({ config, maxArgLength, relative, shell, quiet, debug }, logger) + debugLog('tasks were executed successfully!') + return true + } catch (runAllError) { + printErrors(runAllError, logger) + return false + } + } catch (lintStagedError) { + if (lintStagedError === errConfigNotFound) { + logger.error(`${lintStagedError.message}.`) + } else { + // It was probably a parsing error logger.error(dedent` - Please make sure you have created it correctly. - See https://github.com/okonet/lint-staged#configuration. + Could not parse lint-staged config. + + ${lintStagedError} `) + } + logger.error() // empty line + // Print helpful message for all errors + logger.error(dedent` + Please make sure you have created it correctly. + See https://github.com/okonet/lint-staged#configuration. + `) - return Promise.reject(err) - }) + throw lintStagedError + } }