Skip to content

Commit

Permalink
Merge branch 'master' into fix/react-native-metro-warning
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Jan 8, 2022
2 parents 080a52c + f9f6538 commit 6acaed1
Show file tree
Hide file tree
Showing 18 changed files with 1,742 additions and 2,048 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
@@ -1,9 +1,9 @@
version: 2

updates:
# Production dependencies
- package-ecosystem: 'npm'
directory: '/'
versioning-strategy: 'increase'
schedule:
interval: 'weekly'
day: 'monday'
Expand Down
9 changes: 5 additions & 4 deletions bin/lint-staged.js
Expand Up @@ -44,10 +44,13 @@ cmdline
)
.parse(process.argv)

const debugLog = debug('lint-staged:bin')
if (cmdline.debug) {
const cmdlineOptions = cmdline.opts()

if (cmdlineOptions.debug) {
debug.enable('lint-staged*')
}

const debugLog = debug('lint-staged:bin')
debugLog('Running `lint-staged@%s`', version)

/**
Expand All @@ -68,8 +71,6 @@ const getMaxArgLength = () => {
}
}

const cmdlineOptions = cmdline.opts()

const options = {
allowEmpty: !!cmdlineOptions.allowEmpty,
concurrent: JSON.parse(cmdlineOptions.concurrent),
Expand Down
7 changes: 6 additions & 1 deletion jest.config.js
@@ -1,6 +1,11 @@
const config = {
collectCoverage: true,
collectCoverageFrom: ['lib/**/*.js'],
collectCoverageFrom: [
'lib/**/*.js',
// Avoid ESM import.meta parse error.
// (Can't measure coverage anyway, it's always mocked)
'!lib/resolveConfig.js',
],
moduleDirectories: ['node_modules'],
setupFiles: ['./testSetup.js'],
snapshotSerializers: ['jest-snapshot-serializer-ansi'],
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Expand Up @@ -58,7 +58,7 @@ const lintStaged = async (
) => {
await validateOptions({ shell }, logger)

const inputConfig = configObject || (await loadConfig(configPath, logger))
const inputConfig = configObject || (await loadConfig({ configPath, cwd }, logger))

if (!inputConfig) {
logger.error(`${ConfigNotFoundError.message}.`)
Expand Down
27 changes: 13 additions & 14 deletions lib/loadConfig.js
@@ -1,9 +1,13 @@
/** @typedef {import('./index').Logger} Logger */

import { pathToFileURL } from 'url'

import debug from 'debug'
import { lilconfig } from 'lilconfig'
import YAML from 'yaml'

import { resolveConfig } from './resolveConfig.js'

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

/**
Expand Down Expand Up @@ -47,29 +51,24 @@ const loaders = {
noExt: yamlParse,
}

const resolveConfig = (configPath) => {
try {
return require.resolve(configPath)
} catch {
return configPath
}
}

/**
* @param {string} [configPath]
* @param {Logger} [logger]
* @param {object} options
* @param {string} [options.configPath] - Explicit path to a config file
* @param {string} [options.cwd] - Current working directory
*/
export const loadConfig = async (configPath, logger) => {
export const loadConfig = async ({ configPath, cwd }, logger) => {
try {
if (configPath) {
debugLog('Loading configuration from `%s`...', configPath)
} else {
debugLog('Searching for configuration...')
debugLog('Searching for configuration from `%s`...', cwd)
}

const explorer = lilconfig('lint-staged', { searchPlaces, loaders })

const result = await (configPath ? explorer.load(resolveConfig(configPath)) : explorer.search())
const result = await (configPath
? explorer.load(resolveConfig(configPath))
: explorer.search(cwd))
if (!result) return null

// config is a promise when using the `dynamicImport` loader
Expand All @@ -79,7 +78,7 @@ export const loadConfig = async (configPath, logger) => {

return config
} catch (error) {
debugLog('Failed to load configuration from `%s`', configPath)
debugLog('Failed to load configuration!')
logger.error(error)
return null
}
Expand Down
5 changes: 3 additions & 2 deletions lib/makeCmdTasks.js
Expand Up @@ -28,13 +28,14 @@ const getTitleLength = (renderer, columns = process.stdout.columns) => {
*
* @param {object} options
* @param {Array<string|Function>|string|Function} options.commands
* @param {string} options.cwd
* @param {Array<string>} options.files
* @param {string} options.gitDir
* @param {string} options.renderer
* @param {Boolean} shell
* @param {Boolean} verbose
*/
export const makeCmdTasks = async ({ commands, files, gitDir, renderer, shell, verbose }) => {
export const makeCmdTasks = async ({ commands, cwd, files, gitDir, renderer, shell, verbose }) => {
debugLog('Creating listr tasks for commands %o', commands)
const commandArray = Array.isArray(commands) ? commands : [commands]
const cmdTasks = []
Expand All @@ -61,7 +62,7 @@ export const makeCmdTasks = async ({ commands, files, gitDir, renderer, shell, v

// Truncate title to single line based on renderer
const title = cliTruncate(command, getTitleLength(renderer))
const task = resolveTaskFn({ command, files, gitDir, isFn, shell, verbose })
const task = resolveTaskFn({ command, cwd, files, gitDir, isFn, shell, verbose })
cmdTasks.push({ title, command, task })
}
}
Expand Down
15 changes: 15 additions & 0 deletions lib/resolveConfig.js
@@ -0,0 +1,15 @@
import { createRequire } from 'module'

/**
* require() does not exist for ESM, so we must create it to use require.resolve().
* @see https://nodejs.org/api/module.html#modulecreaterequirefilename
*/
const require = createRequire(import.meta.url)

export function resolveConfig(configPath) {
try {
return require.resolve(configPath)
} catch {
return configPath
}
}
2 changes: 1 addition & 1 deletion lib/resolveGitRepo.js
Expand Up @@ -54,7 +54,7 @@ export const resolveGitRepo = async (cwd = process.cwd()) => {

// read the path of the current directory relative to the top-level directory
// don't read the toplevel directly, it will lead to an posix conform path on non posix systems (cygwin)
const gitRel = normalize(await execGit(['rev-parse', '--show-prefix']))
const gitRel = normalize(await execGit(['rev-parse', '--show-prefix'], { cwd }))
const gitDir = determineGitDir(normalize(cwd), gitRel)
const gitConfigDir = normalize(await resolveGitConfigDir(gitDir))

Expand Down
15 changes: 8 additions & 7 deletions lib/resolveTaskFn.js
Expand Up @@ -68,35 +68,36 @@ const makeErr = (command, result, ctx) => {
*
* @param {Object} options
* @param {string} options.command — Linter task
* @param {string} [options.cwd]
* @param {String} options.gitDir - Current git repo path
* @param {Boolean} options.isFn - Whether the linter task is a function
* @param {Array<string>} options.files — Filepaths to run the linter task against
* @param {Boolean} [options.relative] — Whether the filepaths should be relative
* @param {Boolean} [options.shell] — Whether to skip parsing linter task for better shell support
* @param {Boolean} [options.verbose] — Always show task verbose
* @returns {function(): Promise<Array<string>>}
*/
export const resolveTaskFn = ({
command,
cwd = process.cwd(),
files,
gitDir,
isFn,
relative,
shell = false,
verbose = false,
}) => {
const [cmd, ...args] = parseArgsStringToArgv(command)
debugLog('cmd:', cmd)
debugLog('args:', args)

const execaOptions = { preferLocal: true, reject: false, shell }
if (relative) {
execaOptions.cwd = process.cwd()
} else if (/^git(\.exe)?/i.test(cmd) && gitDir !== process.cwd()) {
const execaOptions = {
// Only use gitDir as CWD if we are using the git binary
// e.g `npm` should run tasks in the actual CWD
execaOptions.cwd = gitDir
cwd: /^git(\.exe)?/i.test(cmd) ? gitDir : cwd,
preferLocal: true,
reject: false,
shell,
}

debugLog('execaOptions:', execaOptions)

return async (ctx = getInitialState()) => {
Expand Down
1 change: 1 addition & 0 deletions lib/runAll.js
Expand Up @@ -135,6 +135,7 @@ export const runAll = async (
for (const task of chunkTasks) {
const subTasks = await makeCmdTasks({
commands: task.commands,
cwd,
files: task.fileList,
gitDir,
renderer: listrOptions.renderer,
Expand Down

0 comments on commit 6acaed1

Please sign in to comment.