Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: replace fs.promises with util.promisify (#786)
This suppresses the ExperimentalWarning on Node.js v10
  • Loading branch information
iiroj committed Jan 31, 2020
1 parent 6bfbe6c commit f71c1c9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
16 changes: 11 additions & 5 deletions lib/file.js
@@ -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.
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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 = {
Expand Down
14 changes: 9 additions & 5 deletions lib/resolveGitRepo.js
@@ -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
Expand All @@ -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
Expand All @@ -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.