Skip to content

Commit

Permalink
refactor: linStaged is an async function
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Nov 27, 2019
1 parent f88e226 commit 03ea132
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions lib/index.js
Expand Up @@ -53,10 +53,10 @@ function loadConfig(configPath) {
*
* @returns {Promise<boolean>} 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,
Expand All @@ -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
}
}

0 comments on commit 03ea132

Please sign in to comment.