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 20, 2022
1 parent 4afcda5 commit dff8141
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/getConfigGroups.js
Original file line number Diff line number Diff line change
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 @@ -20,14 +25,20 @@ export const getConfigGroups = async (
{ configObject, configPath, cwd, 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 @@ -39,6 +50,8 @@ export const getConfigGroups = async (
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 @@ -52,18 +65,29 @@ export const getConfigGroups = async (
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('Searching config files...')

const searchConfig = async (cwd, files = []) => {
const { config, filepath } = await loadConfig({ cwd }, logger)
if (!config) return
if (!config) {
debugLog('Found no config from "%s"!', cwd)
return
}

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

const validatedConfig = validateConfig(config, filepath, logger)
configGroups[filepath] = { config: validatedConfig, files }
}
Expand All @@ -77,9 +101,12 @@ export const getConfigGroups = async (

// 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 dff8141

Please sign in to comment.