Skip to content

Commit

Permalink
refactor: replace normalize-path with native POSIX node:path func…
Browse files Browse the repository at this point in the history
…tionality
  • Loading branch information
iiroj committed Aug 11, 2023
1 parent a559916 commit 7dc7ae0
Show file tree
Hide file tree
Showing 37 changed files with 82 additions and 113 deletions.
5 changes: 1 addition & 4 deletions bin/lint-staged.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env node

import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { supportsColor } from 'chalk'
import { Option, program } from 'commander'
Expand All @@ -19,8 +17,7 @@ if (supportsColor) {
// Do not terminate main Listr process on SIGINT
process.on('SIGINT', () => {})

const packageJsonPath = path.join(fileURLToPath(import.meta.url), '../../package.json')
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath))
const packageJson = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url)))
const version = packageJson.version

const debugLog = debug('lint-staged:bin')
Expand Down
17 changes: 8 additions & 9 deletions lib/chunkFiles.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from 'node:path'
import path from 'node:path/posix'

import debug from 'debug'
import normalize from 'normalize-path'

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

Expand Down Expand Up @@ -34,20 +33,20 @@ const chunkArray = (arr, chunkCount) => {
* @returns {Array<Array<String>>}
*/
export const chunkFiles = ({ files, baseDir, maxArgLength = null, relative = false }) => {
const normalizedFiles = files.map((file) =>
normalize(relative || !baseDir ? file : path.resolve(baseDir, file))
const resolvedFiles = files.map((file) =>
!!relative || !baseDir ? file : path.resolve(baseDir, file)
)

if (!maxArgLength) {
debugLog('Skip chunking files because of undefined maxArgLength')
return [normalizedFiles] // wrap in an array to return a single chunk
return [resolvedFiles] // wrap in an array to return a single chunk
}

const fileListLength = normalizedFiles.join(' ').length
const fileListLength = resolvedFiles.join(' ').length
debugLog(
`Resolved an argument string length of ${fileListLength} characters from ${normalizedFiles.length} files`
`Resolved an argument string length of ${fileListLength} characters from ${resolvedFiles.length} files`
)
const chunkCount = Math.min(Math.ceil(fileListLength / maxArgLength), normalizedFiles.length)
const chunkCount = Math.min(Math.ceil(fileListLength / maxArgLength), resolvedFiles.length)
debugLog(`Creating ${chunkCount} chunks for maxArgLength of ${maxArgLength}`)
return chunkArray(normalizedFiles, chunkCount)
return chunkArray(resolvedFiles, chunkCount)
}
7 changes: 3 additions & 4 deletions lib/generateTasks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import path from 'node:path'
import path from 'node:path/posix'

import debug from 'debug'
import micromatch from 'micromatch'
import normalize from 'normalize-path'

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

Expand All @@ -19,7 +18,7 @@ const debugLog = debug('lint-staged:generateTasks')
export const generateTasks = ({ config, cwd = process.cwd(), files, relative = false }) => {
debugLog('Generating linter tasks')

const relativeFiles = files.map((file) => normalize(path.relative(cwd, file)))
const relativeFiles = files.map((file) => path.relative(cwd, file))

return Object.entries(config).map(([pattern, commands]) => {
const isParentDirPattern = pattern.startsWith('../')
Expand All @@ -42,7 +41,7 @@ export const generateTasks = ({ config, cwd = process.cwd(), files, relative = f
strictBrackets: true,
})

const fileList = matches.map((file) => normalize(relative ? file : path.resolve(cwd, file)))
const fileList = matches.map((file) => (relative ? file : path.resolve(cwd, file)))

const task = { pattern, commands, fileList }
debugLog('Generated task: \n%O', task)
Expand Down
6 changes: 2 additions & 4 deletions lib/getStagedFiles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import path from 'node:path'

import normalize from 'normalize-path'
import path from 'node:path/posix'

import { execGit } from './execGit.js'
import { getDiffCommand } from './getDiffCommand.js'
Expand All @@ -11,7 +9,7 @@ export const getStagedFiles = async ({ cwd = process.cwd(), diff, diffFilter } =
const lines = await execGit(getDiffCommand(diff, diffFilter), { cwd })
if (!lines) return []

return parseGitZOutput(lines).map((file) => normalize(path.resolve(cwd, file)))
return parseGitZOutput(lines).map((file) => path.resolve(cwd, file))
} catch {
return null
}
Expand Down
2 changes: 1 addition & 1 deletion lib/gitWorkflow.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'node:path'
import path from 'node:path/posix'

import debug from 'debug'

Expand Down
2 changes: 1 addition & 1 deletion lib/groupFilesByConfig.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'node:path'
import path from 'node:path/posix'

import debug from 'debug'

Expand Down
17 changes: 9 additions & 8 deletions lib/resolveGitRepo.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import path from 'node:path/posix'

import debug from 'debug'
import normalize from 'normalize-path'

import { execGit } from './execGit.js'
import { readFile } from './file.js'
Expand All @@ -15,7 +14,7 @@ const debugLog = debug('lint-staged:resolveGitRepo')
*/
const resolveGitConfigDir = async (gitDir) => {
// Get the real path in case it's a symlink
const defaultDir = normalize(await fs.realpath(path.join(gitDir, '.git')))
const defaultDir = await fs.realpath(path.join(gitDir, '.git'))
const stats = await fs.lstat(defaultDir)
// If .git is a directory, use it
if (stats.isDirectory()) return defaultDir
Expand All @@ -31,12 +30,14 @@ export const determineGitDir = (cwd, relativeDir) => {
if (relativeDir && relativeDir.endsWith(path.sep)) {
relativeDir = relativeDir.slice(0, -1)
}

if (relativeDir) {
// the current working dir is inside the git top-level directory
return normalize(cwd.substring(0, cwd.lastIndexOf(relativeDir)))
const parentDir = cwd.substring(0, cwd.lastIndexOf(relativeDir))
return path.resolve(parentDir)
} else {
// the current working dir is the top-level git directory
return normalize(cwd)
return path.resolve(cwd)
}
}

Expand All @@ -55,9 +56,9 @@ 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'], { cwd }))
const gitDir = determineGitDir(normalize(cwd), gitRel)
const gitConfigDir = normalize(await resolveGitConfigDir(gitDir))
const gitRel = await execGit(['rev-parse', '--show-prefix'], { cwd })
const gitDir = determineGitDir(cwd, gitRel)
const gitConfigDir = await resolveGitConfigDir(gitDir)

debugLog('Resolved git directory to be `%s`', gitDir)
debugLog('Resolved git config directory to be `%s`', gitConfigDir)
Expand Down
9 changes: 3 additions & 6 deletions lib/runAll.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/** @typedef {import('./index').Logger} Logger */

import path from 'node:path'
import path from 'node:path/posix'

import chalk from 'chalk'
import debug from 'debug'
import { Listr } from 'listr2'
import normalize from 'normalize-path'

import { chunkFiles } from './chunkFiles.js'
import { execGit } from './execGit.js'
Expand Down Expand Up @@ -160,7 +159,7 @@ export const runAll = async (
const matchedFiles = new Set()

for (const [configPath, { config, files }] of Object.entries(filesByConfig)) {
const configName = configPath ? normalize(path.relative(cwd, configPath)) : 'Config object'
const configName = configPath ? path.relative(cwd, configPath) : 'Config object'

const stagedFileChunks = chunkFiles({ baseDir: gitDir, files, maxArgLength, relative })

Expand Down Expand Up @@ -191,9 +190,7 @@ export const runAll = async (
// Make sure relative files are normalized to the
// group cwd, because other there might be identical
// relative filenames in the entire set.
const normalizedFile = path.isAbsolute(file)
? file
: normalize(path.join(groupCwd, file))
const normalizedFile = path.isAbsolute(file) ? file : path.join(groupCwd, file)

matchedFiles.add(normalizedFile)
})
Expand Down
7 changes: 3 additions & 4 deletions lib/searchConfigs.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/** @typedef {import('./index').Logger} Logger */

import path from 'node:path'
import path from 'node:path/posix'

import debug from 'debug'
import normalize from 'normalize-path'

import { execGit } from './execGit.js'
import { loadConfig, searchPlaces } from './loadConfig.js'
Expand All @@ -21,7 +20,7 @@ const numberOfLevels = (file) => file.split('/').length

const sortDeepestParth = (a, b) => (numberOfLevels(a) > numberOfLevels(b) ? -1 : 1)

const isInsideDirectory = (dir) => (file) => file.startsWith(normalize(dir))
const isInsideDirectory = (dir) => (file) => file.startsWith(dir)

/**
* Search all config files from the git repository, preferring those inside `cwd`.
Expand Down Expand Up @@ -68,7 +67,7 @@ export const searchConfigs = async (

/** Sort possible config files so that deepest is first */
const possibleConfigFiles = [...cachedFiles, ...otherFiles]
.map((file) => normalize(path.join(gitDir, file)))
.map((file) => path.join(gitDir, file))
.filter(isInsideDirectory(cwd))
.sort(sortDeepestParth)

Expand Down
2 changes: 1 addition & 1 deletion lib/validateOptions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { constants } from 'node:fs'
import fs from 'node:fs/promises'
import path from 'node:path'
import path from 'node:path/posix'

import debug from 'debug'

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"lilconfig": "2.1.0",
"listr2": "6.6.1",
"micromatch": "4.0.5",
"normalize-path": "3.0.0",
"object-inspect": "1.12.3",
"pidtree": "0.6.0",
"string-argv": "0.3.2",
Expand Down
2 changes: 1 addition & 1 deletion scripts/list-dependency-node-versions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { dirname, join } from 'node:path/posix'
import { fileURLToPath, pathToFileURL } from 'node:url'

import chalk from 'chalk'
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/__utils__/getLintStagedExecutor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'node:path'
import { resolve } from 'node:path/posix'

import { execaCommand } from 'execa'

Expand Down
9 changes: 3 additions & 6 deletions test/integration/__utils__/createTempDir.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ import fs from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path/posix'

import normalize from 'normalize-path'

/**
* Create temporary random directory and return its path
* @returns {Promise<String>}
*/
export const createTempDir = async () => {
const baseDir = await fs.realpath(
process.env.GITHUB_ACTIONS === 'true' ? process.env.RUNNER_TEMP : os.tmpdir()
)
const baseDir =
process.env.GITHUB_ACTIONS === 'true' ? process.env.RUNNER_TEMP : await fs.realpath(os.tmpdir())

const tempDir = path.join(baseDir, 'lint-staged', crypto.randomUUID())
await fs.mkdir(tempDir, { recursive: true })

return normalize(tempDir)
return tempDir
}
2 changes: 1 addition & 1 deletion test/integration/__utils__/withGitIntegration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import path from 'node:path/posix'

import makeConsoleMock from 'consolemock'

Expand Down
2 changes: 1 addition & 1 deletion test/integration/basic-functionality.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './__mocks__/resolveConfig.js'

import fs from 'node:fs/promises'
import path from 'node:path'
import path from 'node:path/posix'

import { jest } from '@jest/globals'

Expand Down
2 changes: 1 addition & 1 deletion test/integration/file-resurrection.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './__mocks__/resolveConfig.js'

import fs from 'node:fs/promises'
import path from 'node:path'
import path from 'node:path/posix'

import { jest } from '@jest/globals'

Expand Down
2 changes: 1 addition & 1 deletion test/integration/files-outside-cwd.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './__mocks__/resolveConfig.js'

import path from 'node:path'
import path from 'node:path/posix'

import { jest } from '@jest/globals'

Expand Down
2 changes: 1 addition & 1 deletion test/integration/git-submodules.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './__mocks__/resolveConfig.js'

import fs from 'node:fs/promises'
import path from 'node:path'
import path from 'node:path/posix'

import { jest } from '@jest/globals'

Expand Down
2 changes: 1 addition & 1 deletion test/integration/git-worktree.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './__mocks__/resolveConfig.js'

import path from 'node:path'
import path from 'node:path/posix'

import { jest } from '@jest/globals'

Expand Down
2 changes: 1 addition & 1 deletion test/integration/gitWorkFlow.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './__mocks__/resolveConfig.js'

import path from 'node:path'
import path from 'node:path/posix'

import { jest as jestGlobals } from '@jest/globals'

Expand Down
2 changes: 1 addition & 1 deletion test/integration/merge-conflict.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './__mocks__/resolveConfig.js'

import fs from 'node:fs'
import path from 'node:path'
import path from 'node:path/posix'

import { jest } from '@jest/globals'

Expand Down
7 changes: 3 additions & 4 deletions test/integration/multiple-config-files.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import './__mocks__/resolveConfig.js'
import './__mocks__/dynamicImport.js'

import path from 'node:path'
import path from 'node:path/posix'

import { jest as jestGlobals } from '@jest/globals'
import normalize from 'normalize-path'

import { withGitIntegration } from './__utils__/withGitIntegration.js'

Expand Down Expand Up @@ -84,11 +83,11 @@ describe('lint-staged', () => {
expect(await readFile('deeper/even/file.js')).toMatch('file.js')

// 'deeper/even/deeper/file.js' is relative to parent 'deeper/even/'
expect(await readFile('deeper/even/deeper/file.js')).toMatch(normalize('deeper/file.js'))
expect(await readFile('deeper/even/deeper/file.js')).toMatch('deeper/file.js')

// 'a/very/deep/file/path/file.js' is relative to root '.'
expect(await readFile('a/very/deep/file/path/file.js')).toMatch(
normalize('a/very/deep/file/path/file.js')
'a/very/deep/file/path/file.js'
)
})
)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/no-stash.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './__mocks__/resolveConfig.js'

import fs from 'node:fs'
import path from 'node:path'
import path from 'node:path/posix'

import { jest } from '@jest/globals'

Expand Down

0 comments on commit 7dc7ae0

Please sign in to comment.