1- /** @typedef {import('./index').Logger } Logger */
2-
31import fs , { constants } from 'node:fs/promises'
42import path from 'node:path'
53
@@ -13,15 +11,14 @@ import { validateConfig } from './validateConfig.js'
1311
1412const debugLog = createDebug ( 'lint-staged:searchConfigs' )
1513
16- const EXEC_GIT = [ 'ls-files' , '-z' , '--full-name' , '-t' ]
17-
1814const CONFIG_PATHSPEC = CONFIG_FILE_NAMES . map ( ( f ) => `:(glob)**/${ f } ` )
1915
2016const numberOfLevels = ( file ) => file . split ( '/' ) . length
2117
22- const sortAlphabetically = ( a , b ) => a . localeCompare ( b )
18+ // Sort deepest first, then alphabetically
19+ const compareConfigPaths = ( a , b ) => numberOfLevels ( b ) - numberOfLevels ( a ) || a . localeCompare ( b )
2320
24- const sortDeepestParth = ( a , b ) => ( numberOfLevels ( a ) > numberOfLevels ( b ) ? - 1 : 1 )
21+ const GIT_STATUS_REGEXP = / ^ [ H S M R C K ? U ] /
2522
2623/**
2724 * Get all possible config files from git
@@ -34,7 +31,10 @@ const sortDeepestParth = (a, b) => (numberOfLevels(a) > numberOfLevels(b) ? -1 :
3431const listConfigFilesFromGit = async ( { cwd, topLevelDir } ) =>
3532 execGit (
3633 [
37- ...EXEC_GIT ,
34+ 'ls-files' ,
35+ '-z' ,
36+ '--full-name' ,
37+ '-t' ,
3838 '--cached' , // show all tracked files
3939 '--others' , // show untracked files
4040 '--exclude-standard' , // apply standard git exclusions (.gitignore, etc.)
@@ -55,7 +55,7 @@ const listConfigFilesFromGit = async ({ cwd, topLevelDir }) =>
5555 return [ ]
5656 }
5757
58- const relativePath = line . replace ( / ^ [ H S M R C K ? U ] / , '' )
58+ const relativePath = line . replace ( GIT_STATUS_REGEXP , '' )
5959 const absolutePath = normalizePath ( path . join ( topLevelDir , relativePath ) )
6060 return [ absolutePath ]
6161 } )
@@ -110,7 +110,7 @@ export const listConfigFilesFromFs = async ({ cwd }) => {
110110 * @param {string } [options.configPath] - Explicit path to a config file
111111 * @param {string } [options.cwd] - Current working directory
112112 * @param {string } [options.topLevelDir] - Top-level directory of the git repo
113- * @param {Logger } logger
113+ * @param {import('./index'). Logger } logger
114114 *
115115 * @returns {Promise<{ [key: string]: { config: *, files: string[] } }> } found configs with filepath as key, and config as value
116116 */
@@ -151,9 +151,8 @@ export const searchConfigs = async (
151151 ] )
152152
153153 /** Create object with key as config file, and value as null */
154- const configs = Array . from ( possibleConfigFiles )
155- . sort ( sortAlphabetically )
156- . sort ( sortDeepestParth )
154+ const configs = [ ...possibleConfigFiles ]
155+ . toSorted ( compareConfigPaths )
157156 . reduce ( ( acc , configPath ) => Object . assign ( acc , { [ configPath ] : null } ) , { } )
158157
159158 /** Load and validate all configs to the above object */
0 commit comments