Skip to content

Commit

Permalink
Refactor internal getStats() util
Browse files Browse the repository at this point in the history
Fixes #762
  • Loading branch information
RyanZim committed Feb 14, 2020
1 parent cdc7745 commit efa796b
Showing 1 changed file with 11 additions and 25 deletions.
36 changes: 11 additions & 25 deletions lib/util/stat.js
@@ -1,35 +1,21 @@
'use strict'

const fs = require('graceful-fs')
const fs = require('../fs')
const path = require('path')
const util = require('util')
const atLeastNode = require('at-least-node')

const nodeSupportsBigInt = atLeastNode('10.5.0')
const stat = (file) => nodeSupportsBigInt ? fs.stat(file, { bigint: true }) : fs.stat(file)

function getStats (src, dest, cb) {
if (nodeSupportsBigInt) {
fs.stat(src, { bigint: true }, (err, srcStat) => {
if (err) return cb(err)
fs.stat(dest, { bigint: true }, (err, destStat) => {
if (err) {
if (err.code === 'ENOENT') return cb(null, { srcStat, destStat: null })
return cb(err)
}
return cb(null, { srcStat, destStat })
})
})
} else {
fs.stat(src, (err, srcStat) => {
if (err) return cb(err)
fs.stat(dest, (err, destStat) => {
if (err) {
if (err.code === 'ENOENT') return cb(null, { srcStat, destStat: null })
return cb(err)
}
return cb(null, { srcStat, destStat })
})
function getStats (src, dest) {
return Promise.all([
stat(src),
stat(dest).catch(err => {
if (err.code === 'ENOENT') return null
throw err
})
}
]).then(([srcStat, destStat]) => ({ srcStat, destStat }))
}

function getStatsSync (src, dest) {
Expand All @@ -53,7 +39,7 @@ function getStatsSync (src, dest) {
}

function checkPaths (src, dest, funcName, cb) {
getStats(src, dest, (err, stats) => {
util.callbackify(getStats)(src, dest, (err, stats) => {
if (err) return cb(err)
const { srcStat, destStat } = stats
if (destStat && areIdentical(srcStat, destStat)) {
Expand Down

0 comments on commit efa796b

Please sign in to comment.