Skip to content

Commit

Permalink
Showing 2 changed files with 20 additions and 10 deletions.
16 changes: 11 additions & 5 deletions lib/file.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
'use strict'

const debug = require('debug')('lint-staged:file')
const fs = require('fs').promises
const fs = require('fs')
const { promisify } = require('util')

const fsAccess = promisify(fs.access)
const fsReadFile = promisify(fs.readFile)
const fsUnlink = promisify(fs.unlink)
const fsWriteFile = promisify(fs.writeFile)

/**
* Check if a file exists. Returns the filename if exists.
@@ -10,7 +16,7 @@ const fs = require('fs').promises
*/
const exists = async filename => {
try {
await fs.access(filename)
await fsAccess(filename)
return filename
} catch {
return false
@@ -26,7 +32,7 @@ const exists = async filename => {
const readFile = async (filename, ignoreENOENT = true) => {
debug('Reading file `%s`', filename)
try {
return await fs.readFile(filename)
return await fsReadFile(filename)
} catch (error) {
if (ignoreENOENT && error.code === 'ENOENT') {
debug("File `%s` doesn't exist, ignoring...", filename)
@@ -46,7 +52,7 @@ const unlink = async (filename, ignoreENOENT = true) => {
if (filename) {
debug('Unlinking file `%s`', filename)
try {
await fs.unlink(filename)
await fsUnlink(filename)
} catch (error) {
if (ignoreENOENT && error.code === 'ENOENT') {
debug("File `%s` doesn't exist, ignoring...", filename)
@@ -64,7 +70,7 @@ const unlink = async (filename, ignoreENOENT = true) => {
*/
const writeFile = async (filename, buffer) => {
debug('Writing file `%s`', filename)
await fs.writeFile(filename, buffer)
await fsWriteFile(filename, buffer)
}

module.exports = {
14 changes: 9 additions & 5 deletions lib/resolveGitRepo.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
'use strict'

const normalize = require('normalize-path')
const fs = require('fs').promises
const path = require('path')

const debugLog = require('debug')('lint-staged:resolveGitRepo')
const fs = require('fs')
const path = require('path')
const { promisify } = require('util')

const execGit = require('./execGit')
const { readFile } = require('./file')

const fsLstat = promisify(fs.lstat)

/**
* Resolve path to the .git directory, with special handling for
* submodules and worktrees
*/
const resolveGitConfigDir = async gitDir => {
const defaultDir = path.resolve(gitDir, '.git')
const stats = await fs.lstat(defaultDir)
const stats = await fsLstat(defaultDir)
// If .git is a directory, use it
if (stats.isDirectory()) return defaultDir
// Otherwise .git is a file containing path to real location
@@ -26,7 +28,7 @@ const resolveGitConfigDir = async gitDir => {
/**
* Resolve git directory and possible submodule paths
*/
module.exports = async function resolveGitRepo(cwd) {
const resolveGitRepo = async cwd => {
try {
debugLog('Resolving git repo from `%s`', cwd)
// git cli uses GIT_DIR to fast track its response however it might be set to a different path
@@ -46,3 +48,5 @@ module.exports = async function resolveGitRepo(cwd) {
return { error, gitDir: null, gitConfigDir: null }
}
}

module.exports = resolveGitRepo

0 comments on commit f71c1c9

Please sign in to comment.