Skip to content

Commit

Permalink
refactor: move config file name constants to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Dec 2, 2023
1 parent 78a0c23 commit 8db4a15
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
24 changes: 24 additions & 0 deletions lib/configFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const CONFIG_NAME = 'lint-staged'

export const PACKAGE_JSON_FILE = 'package.json'

export const PACKAGE_YAML_FILES = ['package.yaml', 'package.yml']

/**
* The list of files `lint-staged` will read configuration
* from, in the declared order.
*/
export const CONFIG_FILE_NAMES = [
PACKAGE_JSON_FILE,
...PACKAGE_YAML_FILES,
'.lintstagedrc',
'.lintstagedrc.json',
'.lintstagedrc.yaml',
'.lintstagedrc.yml',
'.lintstagedrc.mjs',
'.lintstagedrc.js',
'.lintstagedrc.cjs',
'lint-staged.config.mjs',
'lint-staged.config.js',
'lint-staged.config.cjs',
]
33 changes: 7 additions & 26 deletions lib/loadConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,15 @@ import YAML from 'yaml'

import { dynamicImport } from './dynamicImport.js'
import { resolveConfig } from './resolveConfig.js'
import {
CONFIG_FILE_NAMES,
CONFIG_NAME,
PACKAGE_JSON_FILE,
PACKAGE_YAML_FILES,
} from './configFiles.js'

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

const CONFIG_NAME = 'lint-staged'

const PACKAGE_JSON_FILE = 'package.json'

const PACKAGE_YAML_FILES = ['package.yaml', 'package.yml']

/**
* The list of files `lint-staged` will read configuration
* from, in the declared order.
*/
export const searchPlaces = [
PACKAGE_JSON_FILE,
...PACKAGE_YAML_FILES,
'.lintstagedrc',
'.lintstagedrc.json',
'.lintstagedrc.yaml',
'.lintstagedrc.yml',
'.lintstagedrc.mjs',
'.lintstagedrc.js',
'.lintstagedrc.cjs',
'lint-staged.config.mjs',
'lint-staged.config.js',
'lint-staged.config.cjs',
]

const jsonParse = (filePath, content) => {
const isPackageFile = PACKAGE_JSON_FILE.includes(path.basename(filePath))
try {
Expand Down Expand Up @@ -122,7 +103,7 @@ export const loadConfig = async ({ configPath, cwd }, logger) => {
} else {
debugLog('Searching for configuration from `%s`...', cwd)
const { lilconfig } = await import('lilconfig')
const explorer = lilconfig(CONFIG_NAME, { searchPlaces, loaders })
const explorer = lilconfig(CONFIG_NAME, { searchPlaces: CONFIG_FILE_NAMES, loaders })
result = await explorer.search(cwd)
}

Expand Down
5 changes: 3 additions & 2 deletions lib/searchConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import path from 'node:path'
import debug from 'debug'

import { execGit } from './execGit.js'
import { loadConfig, searchPlaces } from './loadConfig.js'
import { loadConfig } from './loadConfig.js'
import { normalizePath } from './normalizePath.js'
import { parseGitZOutput } from './parseGitZOutput.js'
import { validateConfig } from './validateConfig.js'
import { CONFIG_FILE_NAMES } from './configFiles.js'

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

const EXEC_GIT = ['ls-files', '-z', '--full-name']

const filterPossibleConfigFiles = (files) =>
files.filter((file) => searchPlaces.includes(path.basename(file)))
files.filter((file) => CONFIG_FILE_NAMES.includes(path.basename(file)))

const numberOfLevels = (file) => file.split('/').length

Expand Down

0 comments on commit 8db4a15

Please sign in to comment.