Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace fs.promises with util.promisify to suppress ExperimentalWarning on Node.js v10 #786

Merged
merged 1 commit into from Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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