-
-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: search all configs regardless of staged files
Make sure lint-staged doesn't exit with error when staging files not covered by any config, as long as there exists at least a single config.
- Loading branch information
Showing
6 changed files
with
135 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** @typedef {import('./index').Logger} Logger */ | ||
|
||
import { basename, join } from 'path' | ||
|
||
import normalize from 'normalize-path' | ||
|
||
import { execGit } from './execGit.js' | ||
import { loadConfig, searchPlaces } from './loadConfig.js' | ||
import { validateConfig } from './validateConfig.js' | ||
|
||
const EXEC_GIT = ['ls-files', '-z', '--full-name'] | ||
|
||
const filterPossibleConfigFiles = (file) => searchPlaces.includes(basename(file)) | ||
|
||
const numberOfLevels = (file) => file.split('/').length | ||
|
||
const sortDeepestParth = (a, b) => (numberOfLevels(a) > numberOfLevels(b) ? -1 : 1) | ||
|
||
/** | ||
* Search all config files from the git repository | ||
* | ||
* @param {string} gitDir | ||
* @param {Logger} logger | ||
* @returns {Promise<{ [key: string]: * }>} found configs with filepath as key, and config as value | ||
*/ | ||
export const searchConfigs = async (gitDir = process.cwd(), logger) => { | ||
/** Get all possible config files known to git */ | ||
const cachedFiles = (await execGit(EXEC_GIT, { cwd: gitDir })) | ||
// eslint-disable-next-line no-control-regex | ||
.replace(/\u0000$/, '') | ||
.split('\u0000') | ||
.filter(filterPossibleConfigFiles) | ||
|
||
/** Get all possible config files from uncommitted files */ | ||
const otherFiles = ( | ||
await execGit([...EXEC_GIT, '--others', '--exclude-standard'], { cwd: gitDir }) | ||
) | ||
// eslint-disable-next-line no-control-regex | ||
.replace(/\u0000$/, '') | ||
.split('\u0000') | ||
.filter(filterPossibleConfigFiles) | ||
|
||
/** Sort possible config files so that deepest is first */ | ||
const possibleConfigFiles = [...cachedFiles, ...otherFiles] | ||
.map((file) => join(gitDir, file)) | ||
.map((file) => normalize(file)) | ||
.sort(sortDeepestParth) | ||
|
||
/** Create object with key as config file, and value as null */ | ||
const configs = possibleConfigFiles.reduce( | ||
(acc, configPath) => Object.assign(acc, { [configPath]: null }), | ||
{} | ||
) | ||
|
||
/** Load and validate all configs to the above object */ | ||
await Promise.all( | ||
possibleConfigFiles | ||
.map((configPath) => loadConfig({ configPath }, logger)) | ||
.map((promise) => | ||
promise.then(({ config, filepath }) => { | ||
if (config) { | ||
configs[filepath] = validateConfig(config, filepath, logger) | ||
} | ||
}) | ||
) | ||
) | ||
|
||
/** Get validated configs from the above object, without any `null` values (not found) */ | ||
const foundConfigs = Object.entries(configs) | ||
.filter(([, value]) => !!value) | ||
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}) | ||
|
||
return foundConfigs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters