Skip to content

Commit

Permalink
Use at-least-node instead of homespun version sniffing (#760)
Browse files Browse the repository at this point in the history
* Use at-least-node instead of homespun version sniffing

* Remove semver devDep; use at-least-node in tests
  • Loading branch information
RyanZim committed Feb 13, 2020
1 parent 9bfd380 commit cdc7745
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 36 deletions.
4 changes: 2 additions & 2 deletions lib/fs/__tests__/multi-param.test.js
Expand Up @@ -4,13 +4,13 @@ const assert = require('assert')
const path = require('path')
const crypto = require('crypto')
const os = require('os')
const semver = require('semver')
const atLeastNode = require('at-least-node')
const fs = require('../..')

const SIZE = 1000

// Used for tests on Node 12.9.0+ only
const describeNode12 = semver.gte(process.version, '12.9.0') ? describe : describe.skip
const describeNode12 = atLeastNode('12.9.0') ? describe : describe.skip

describe('fs.read()', () => {
let TEST_FILE
Expand Down
8 changes: 3 additions & 5 deletions lib/util/__tests__/stat.test.js
Expand Up @@ -4,7 +4,7 @@ const fs = require(process.cwd())
const os = require('os')
const path = require('path')
const assert = require('assert')
const semver = require('semver')
const atLeastNode = require('at-least-node')
const stat = require('../stat.js')

const NODE_VERSION_WITH_BIGINT = '10.5.0'
Expand All @@ -23,15 +23,14 @@ describe('util/stat', () => {

describe('should use stats with bigint type for node versions >= 10.5.0 and number type for older versions', () => {
it('stat.checkPaths()', () => {
const nodeVersion = process.versions.node
const src = path.join(TEST_DIR, 'src')
const dest = path.join(TEST_DIR, 'dest')
fs.ensureFileSync(src)
fs.ensureFileSync(dest)
stat.checkPaths(src, dest, 'copy', (err, stats) => {
assert.ifError(err)
const { srcStat } = stats
if (semver.gte(nodeVersion, NODE_VERSION_WITH_BIGINT)) {
if (atLeastNode(NODE_VERSION_WITH_BIGINT)) {
assert.strictEqual(typeof srcStat.ino, 'bigint')
} else {
assert.strictEqual(typeof srcStat.ino, 'number')
Expand All @@ -40,13 +39,12 @@ describe('util/stat', () => {
})

it('stat.checkPathsSync()', () => {
const nodeVersion = process.versions.node
const src = path.join(TEST_DIR, 'src')
const dest = path.join(TEST_DIR, 'dest')
fs.ensureFileSync(src)
fs.ensureFileSync(dest)
const { srcStat } = stat.checkPathsSync(src, dest, 'copy')
if (semver.gte(nodeVersion, NODE_VERSION_WITH_BIGINT)) {
if (atLeastNode(NODE_VERSION_WITH_BIGINT)) {
assert.strictEqual(typeof srcStat.ino, 'bigint')
} else {
assert.strictEqual(typeof srcStat.ino, 'number')
Expand Down
36 changes: 8 additions & 28 deletions lib/util/stat.js
Expand Up @@ -2,32 +2,12 @@

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

const NODE_VERSION_MAJOR_WITH_BIGINT = 10
const NODE_VERSION_MINOR_WITH_BIGINT = 5
const NODE_VERSION_PATCH_WITH_BIGINT = 0
const nodeVersion = process.versions.node.split('.')
const nodeVersionMajor = Number.parseInt(nodeVersion[0], 10)
const nodeVersionMinor = Number.parseInt(nodeVersion[1], 10)
const nodeVersionPatch = Number.parseInt(nodeVersion[2], 10)

function nodeSupportsBigInt () {
if (nodeVersionMajor > NODE_VERSION_MAJOR_WITH_BIGINT) {
return true
} else if (nodeVersionMajor === NODE_VERSION_MAJOR_WITH_BIGINT) {
if (nodeVersionMinor > NODE_VERSION_MINOR_WITH_BIGINT) {
return true
} else if (nodeVersionMinor === NODE_VERSION_MINOR_WITH_BIGINT) {
if (nodeVersionPatch >= NODE_VERSION_PATCH_WITH_BIGINT) {
return true
}
}
}
return false
}
const nodeSupportsBigInt = atLeastNode('10.5.0')

function getStats (src, dest, cb) {
if (nodeSupportsBigInt()) {
if (nodeSupportsBigInt) {
fs.stat(src, { bigint: true }, (err, srcStat) => {
if (err) return cb(err)
fs.stat(dest, { bigint: true }, (err, destStat) => {
Expand All @@ -54,13 +34,13 @@ function getStats (src, dest, cb) {

function getStatsSync (src, dest) {
let srcStat, destStat
if (nodeSupportsBigInt()) {
if (nodeSupportsBigInt) {
srcStat = fs.statSync(src, { bigint: true })
} else {
srcStat = fs.statSync(src)
}
try {
if (nodeSupportsBigInt()) {
if (nodeSupportsBigInt) {
destStat = fs.statSync(dest, { bigint: true })
} else {
destStat = fs.statSync(dest)
Expand Down Expand Up @@ -105,7 +85,7 @@ function checkParentPaths (src, srcStat, dest, funcName, cb) {
const srcParent = path.resolve(path.dirname(src))
const destParent = path.resolve(path.dirname(dest))
if (destParent === srcParent || destParent === path.parse(destParent).root) return cb()
if (nodeSupportsBigInt()) {
if (nodeSupportsBigInt) {
fs.stat(destParent, { bigint: true }, (err, destStat) => {
if (err) {
if (err.code === 'ENOENT') return cb()
Expand Down Expand Up @@ -136,7 +116,7 @@ function checkParentPathsSync (src, srcStat, dest, funcName) {
if (destParent === srcParent || destParent === path.parse(destParent).root) return
let destStat
try {
if (nodeSupportsBigInt()) {
if (nodeSupportsBigInt) {
destStat = fs.statSync(destParent, { bigint: true })
} else {
destStat = fs.statSync(destParent)
Expand All @@ -153,7 +133,7 @@ function checkParentPathsSync (src, srcStat, dest, funcName) {

function areIdentical (srcStat, destStat) {
if (destStat.ino && destStat.dev && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev) {
if (nodeSupportsBigInt() || destStat.ino < Number.MAX_SAFE_INTEGER) {
if (nodeSupportsBigInt || destStat.ino < Number.MAX_SAFE_INTEGER) {
// definitive answer
return true
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -37,6 +37,7 @@
"author": "JP Richardson <jprichardson@gmail.com>",
"license": "MIT",
"dependencies": {
"at-least-node": "^1.0.0",
"graceful-fs": "^4.2.0",
"jsonfile": "^4.0.0",
"universalify": "^0.1.0"
Expand All @@ -50,7 +51,6 @@
"nyc": "^15.0.0",
"proxyquire": "^2.0.1",
"read-dir-files": "^0.1.1",
"semver": "^5.3.0",
"standard": "^14.1.0"
},
"main": "./lib/index.js",
Expand Down

0 comments on commit cdc7745

Please sign in to comment.