Skip to content

Commit

Permalink
test: add debug logging to getConfigGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Jan 19, 2022
1 parent 7c58331 commit 0f68ed1
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/getConfigGroups.js
Expand Up @@ -2,10 +2,15 @@

import path from 'path'

import debug from 'debug'
import objectInspect from 'object-inspect'

import { loadConfig } from './loadConfig.js'
import { ConfigNotFoundError } from './symbols.js'
import { validateConfig } from './validateConfig.js'

const debugLog = debug('lint-staged:getConfigGroups')

/**
* Return matched files grouped by their configuration.
*
Expand All @@ -16,14 +21,19 @@ import { validateConfig } from './validateConfig.js'
* @param {Logger} logger
*/
export const getConfigGroups = async ({ configObject, configPath, files }, logger = console) => {
debugLog('Grouping configuration files...')

// Return explicit config object from js API
if (configObject) {
debugLog('Using single direct configuration object...')
const config = validateConfig(configObject, 'config object', logger)
return { '': { config, files } }
}

// Use only explicit config path instead of discovering multiple
if (configPath) {
debugLog('Using single configuration path...')

const { config, filepath } = await loadConfig({ configPath }, logger)

if (!config) {
Expand All @@ -35,6 +45,8 @@ export const getConfigGroups = async ({ configObject, configPath, files }, logge
return { [configPath]: { config: validatedConfig, files } }
}

debugLog('Grouping staged files by their directories...')

// Group files by their base directory
const filesByDir = files.reduce((acc, file) => {
const dir = path.normalize(path.dirname(file))
Expand All @@ -48,22 +60,33 @@ export const getConfigGroups = async ({ configObject, configPath, files }, logge
return acc
}, {})

debugLog('Grouped staged files into %d directories:', Object.keys(filesByDir).length)
debugLog(objectInspect(filesByDir, { indent: 2 }))

// Group files by their discovered config
// { '.lintstagedrc.json': { config: {...}, files: [...] } }
const configGroups = {}

debugLog('Discovering config files...')

await Promise.all(
Object.entries(filesByDir).map(([dir, files]) => {
// Discover config from the base directory of the file
return loadConfig({ cwd: dir }, logger).then(({ config, filepath }) => {
if (!config) return
if (!config) {
debugLog('Found no config from "%s"!', dir)
return
}

if (filepath in configGroups) {
debugLog('Found existing config "%s" from "%s"!', filepath, dir)
// Re-use cached config and skip validation
configGroups[filepath].files.push(...files)
return
}

debugLog('Found new config "%s" from "%s"!', filepath, dir)

const validatedConfig = validateConfig(config, filepath, logger)
configGroups[filepath] = { config: validatedConfig, files }
})
Expand All @@ -72,9 +95,12 @@ export const getConfigGroups = async ({ configObject, configPath, files }, logge

// Throw if no configurations were found
if (Object.keys(configGroups).length === 0) {
debugLog('Found no config groups!')
logger.error(`${ConfigNotFoundError.message}.`)
throw ConfigNotFoundError
}

debugLog('Grouped staged files into %d groups!', Object.keys(configGroups).length)

return configGroups
}

0 comments on commit 0f68ed1

Please sign in to comment.