From cdef03d8c17cbfc9eece3678f7ef7d7dec38876e Mon Sep 17 00:00:00 2001 From: Ryan Zimmerman Date: Thu, 13 Feb 2020 10:56:01 -0500 Subject: [PATCH 1/2] Use at-least-node instead of homespun version sniffing --- lib/util/stat.js | 36 ++++++++---------------------------- package.json | 1 + 2 files changed, 9 insertions(+), 28 deletions(-) diff --git a/lib/util/stat.js b/lib/util/stat.js index 1361858e..14fb00c7 100644 --- a/lib/util/stat.js +++ b/lib/util/stat.js @@ -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) => { @@ -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) @@ -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() @@ -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) @@ -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 } diff --git a/package.json b/package.json index 96275eae..da59c0aa 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "author": "JP Richardson ", "license": "MIT", "dependencies": { + "at-least-node": "^1.0.0", "graceful-fs": "^4.2.0", "jsonfile": "^4.0.0", "universalify": "^0.1.0" From c4a25ed3a8fcaf079b21ae59bc5218ce8d2c6a7c Mon Sep 17 00:00:00 2001 From: Ryan Zimmerman Date: Thu, 13 Feb 2020 11:03:26 -0500 Subject: [PATCH 2/2] Remove semver devDep; use at-least-node in tests --- lib/fs/__tests__/multi-param.test.js | 4 ++-- lib/util/__tests__/stat.test.js | 8 +++----- package.json | 1 - 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/fs/__tests__/multi-param.test.js b/lib/fs/__tests__/multi-param.test.js index b23efc07..6b0cccb4 100644 --- a/lib/fs/__tests__/multi-param.test.js +++ b/lib/fs/__tests__/multi-param.test.js @@ -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 diff --git a/lib/util/__tests__/stat.test.js b/lib/util/__tests__/stat.test.js index df436501..8d0e85de 100644 --- a/lib/util/__tests__/stat.test.js +++ b/lib/util/__tests__/stat.test.js @@ -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' @@ -23,7 +23,6 @@ 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) @@ -31,7 +30,7 @@ describe('util/stat', () => { 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') @@ -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') diff --git a/package.json b/package.json index da59c0aa..20e47623 100644 --- a/package.json +++ b/package.json @@ -51,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",