Skip to content

Commit cdc7745

Browse files
authored
Use at-least-node instead of homespun version sniffing (#760)
* Use at-least-node instead of homespun version sniffing * Remove semver devDep; use at-least-node in tests
1 parent 9bfd380 commit cdc7745

File tree

4 files changed

+14
-36
lines changed

4 files changed

+14
-36
lines changed

lib/fs/__tests__/multi-param.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ const assert = require('assert')
44
const path = require('path')
55
const crypto = require('crypto')
66
const os = require('os')
7-
const semver = require('semver')
7+
const atLeastNode = require('at-least-node')
88
const fs = require('../..')
99

1010
const SIZE = 1000
1111

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

1515
describe('fs.read()', () => {
1616
let TEST_FILE

lib/util/__tests__/stat.test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const fs = require(process.cwd())
44
const os = require('os')
55
const path = require('path')
66
const assert = require('assert')
7-
const semver = require('semver')
7+
const atLeastNode = require('at-least-node')
88
const stat = require('../stat.js')
99

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

2424
describe('should use stats with bigint type for node versions >= 10.5.0 and number type for older versions', () => {
2525
it('stat.checkPaths()', () => {
26-
const nodeVersion = process.versions.node
2726
const src = path.join(TEST_DIR, 'src')
2827
const dest = path.join(TEST_DIR, 'dest')
2928
fs.ensureFileSync(src)
3029
fs.ensureFileSync(dest)
3130
stat.checkPaths(src, dest, 'copy', (err, stats) => {
3231
assert.ifError(err)
3332
const { srcStat } = stats
34-
if (semver.gte(nodeVersion, NODE_VERSION_WITH_BIGINT)) {
33+
if (atLeastNode(NODE_VERSION_WITH_BIGINT)) {
3534
assert.strictEqual(typeof srcStat.ino, 'bigint')
3635
} else {
3736
assert.strictEqual(typeof srcStat.ino, 'number')
@@ -40,13 +39,12 @@ describe('util/stat', () => {
4039
})
4140

4241
it('stat.checkPathsSync()', () => {
43-
const nodeVersion = process.versions.node
4442
const src = path.join(TEST_DIR, 'src')
4543
const dest = path.join(TEST_DIR, 'dest')
4644
fs.ensureFileSync(src)
4745
fs.ensureFileSync(dest)
4846
const { srcStat } = stat.checkPathsSync(src, dest, 'copy')
49-
if (semver.gte(nodeVersion, NODE_VERSION_WITH_BIGINT)) {
47+
if (atLeastNode(NODE_VERSION_WITH_BIGINT)) {
5048
assert.strictEqual(typeof srcStat.ino, 'bigint')
5149
} else {
5250
assert.strictEqual(typeof srcStat.ino, 'number')

lib/util/stat.js

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,12 @@
22

33
const fs = require('graceful-fs')
44
const path = require('path')
5+
const atLeastNode = require('at-least-node')
56

6-
const NODE_VERSION_MAJOR_WITH_BIGINT = 10
7-
const NODE_VERSION_MINOR_WITH_BIGINT = 5
8-
const NODE_VERSION_PATCH_WITH_BIGINT = 0
9-
const nodeVersion = process.versions.node.split('.')
10-
const nodeVersionMajor = Number.parseInt(nodeVersion[0], 10)
11-
const nodeVersionMinor = Number.parseInt(nodeVersion[1], 10)
12-
const nodeVersionPatch = Number.parseInt(nodeVersion[2], 10)
13-
14-
function nodeSupportsBigInt () {
15-
if (nodeVersionMajor > NODE_VERSION_MAJOR_WITH_BIGINT) {
16-
return true
17-
} else if (nodeVersionMajor === NODE_VERSION_MAJOR_WITH_BIGINT) {
18-
if (nodeVersionMinor > NODE_VERSION_MINOR_WITH_BIGINT) {
19-
return true
20-
} else if (nodeVersionMinor === NODE_VERSION_MINOR_WITH_BIGINT) {
21-
if (nodeVersionPatch >= NODE_VERSION_PATCH_WITH_BIGINT) {
22-
return true
23-
}
24-
}
25-
}
26-
return false
27-
}
7+
const nodeSupportsBigInt = atLeastNode('10.5.0')
288

299
function getStats (src, dest, cb) {
30-
if (nodeSupportsBigInt()) {
10+
if (nodeSupportsBigInt) {
3111
fs.stat(src, { bigint: true }, (err, srcStat) => {
3212
if (err) return cb(err)
3313
fs.stat(dest, { bigint: true }, (err, destStat) => {
@@ -54,13 +34,13 @@ function getStats (src, dest, cb) {
5434

5535
function getStatsSync (src, dest) {
5636
let srcStat, destStat
57-
if (nodeSupportsBigInt()) {
37+
if (nodeSupportsBigInt) {
5838
srcStat = fs.statSync(src, { bigint: true })
5939
} else {
6040
srcStat = fs.statSync(src)
6141
}
6242
try {
63-
if (nodeSupportsBigInt()) {
43+
if (nodeSupportsBigInt) {
6444
destStat = fs.statSync(dest, { bigint: true })
6545
} else {
6646
destStat = fs.statSync(dest)
@@ -105,7 +85,7 @@ function checkParentPaths (src, srcStat, dest, funcName, cb) {
10585
const srcParent = path.resolve(path.dirname(src))
10686
const destParent = path.resolve(path.dirname(dest))
10787
if (destParent === srcParent || destParent === path.parse(destParent).root) return cb()
108-
if (nodeSupportsBigInt()) {
88+
if (nodeSupportsBigInt) {
10989
fs.stat(destParent, { bigint: true }, (err, destStat) => {
11090
if (err) {
11191
if (err.code === 'ENOENT') return cb()
@@ -136,7 +116,7 @@ function checkParentPathsSync (src, srcStat, dest, funcName) {
136116
if (destParent === srcParent || destParent === path.parse(destParent).root) return
137117
let destStat
138118
try {
139-
if (nodeSupportsBigInt()) {
119+
if (nodeSupportsBigInt) {
140120
destStat = fs.statSync(destParent, { bigint: true })
141121
} else {
142122
destStat = fs.statSync(destParent)
@@ -153,7 +133,7 @@ function checkParentPathsSync (src, srcStat, dest, funcName) {
153133

154134
function areIdentical (srcStat, destStat) {
155135
if (destStat.ino && destStat.dev && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev) {
156-
if (nodeSupportsBigInt() || destStat.ino < Number.MAX_SAFE_INTEGER) {
136+
if (nodeSupportsBigInt || destStat.ino < Number.MAX_SAFE_INTEGER) {
157137
// definitive answer
158138
return true
159139
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"author": "JP Richardson <[email protected]>",
3838
"license": "MIT",
3939
"dependencies": {
40+
"at-least-node": "^1.0.0",
4041
"graceful-fs": "^4.2.0",
4142
"jsonfile": "^4.0.0",
4243
"universalify": "^0.1.0"
@@ -50,7 +51,6 @@
5051
"nyc": "^15.0.0",
5152
"proxyquire": "^2.0.1",
5253
"read-dir-files": "^0.1.1",
53-
"semver": "^5.3.0",
5454
"standard": "^14.1.0"
5555
},
5656
"main": "./lib/index.js",

0 commit comments

Comments
 (0)