Skip to content

Commit

Permalink
[fix] node v10.0 lacks fs.promises
Browse files Browse the repository at this point in the history
In this node version, fall back to `util.promisify` of the callback version.

Maybe fixes #2623. Maybe fixes #2652. Maybe fixes #2625.
  • Loading branch information
ljharb committed Feb 8, 2021
1 parent fdebb2a commit 5daf858
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: ['10.0', 10.x, '12.0', 12.x, '14.0', 14.x]
platform:
- os: ubuntu-latest
shell: bash
Expand Down
4 changes: 3 additions & 1 deletion lib/shrinkwrap.js
@@ -1,5 +1,7 @@
const { resolve, basename } = require('path')
const { promises: { unlink } } = require('fs')
const util = require('util')
const fs = require('fs')
const { unlink } = fs.promises || util.promisify(fs.unlink)
const Arborist = require('@npmcli/arborist')
const log = require('npmlog')

Expand Down
4 changes: 3 additions & 1 deletion lib/utils/reify-finish.js
@@ -1,7 +1,9 @@
const reifyOutput = require('./reify-output.js')
const npm = require('../npm.js')
const ini = require('ini')
const {writeFile} = require('fs').promises
const util = require('util')
const fs = require('fs')
const { writeFile } = fs.promises || { writeFile: util.promisify(fs.writeFile) }
const {resolve} = require('path')

const reifyFinish = async arb => {
Expand Down
26 changes: 13 additions & 13 deletions test/lib/doctor.js
Expand Up @@ -167,7 +167,7 @@ test('npm doctor checks ok', t => {
}, 'trackers all finished')
t.match(output, /npm ping\s*ok/, 'ping output is ok')
t.match(output, /npm -v\s*ok/, 'npm -v output is ok')
t.match(output, /node -v\s*ok/, 'node -v output is ok')
t.match(output, /node -v\s*(ok|not ok Use node v)/, 'node -v output is ok')
t.match(output, /npm config get registry\s*ok\s*using default/, 'npm config get registry output is ok')
t.match(output, /which git\s*ok/, 'which git output is ok')
t.match(output, /cached files\s*ok/, 'cached files are ok')
Expand Down Expand Up @@ -256,7 +256,7 @@ test('npm doctor supports color', t => {
}, 'trackers all finished')
t.match(output, /npm ping.*not ok/, 'ping output is ok')
t.match(output, /npm -v.*ok/, 'npm -v output is ok')
t.match(output, /node -v.*ok/, 'node -v output is ok')
t.match(output, /node -v\s*(ok|not ok Use node v)/, 'node -v output is ok')
t.match(output, /npm config get registry.*ok.*using default/, 'npm config get registry output is ok')
t.match(output, /which git.*ok/, 'which git output is ok')
t.match(output, /cached files.*ok/, 'cached files are ok')
Expand Down Expand Up @@ -317,7 +317,7 @@ test('npm doctor skips some tests in windows', t => {
}, 'trackers all finished')
t.match(output, /npm ping\s*ok/, 'ping output is ok')
t.match(output, /npm -v\s*ok/, 'npm -v output is ok')
t.match(output, /node -v\s*ok/, 'node -v output is ok')
t.match(output, /node -v\s*(ok|not ok Use node v)/, 'node -v output is ok')
t.match(output, /npm config get registry\s*ok\s*using default/, 'npm config get registry output is ok')
t.match(output, /which git\s*ok/, 'which git output is ok')
t.match(output, /cache contents\s*ok/, 'cache contents is ok')
Expand Down Expand Up @@ -361,7 +361,7 @@ test('npm doctor ping error E{3}', t => {
}, 'trackers all finished')
t.match(output, /npm ping\s*not ok\s*111 this error is 111/, 'ping output contains trimmed error')
t.match(output, /npm -v\s*ok/, 'npm -v output is ok')
t.match(output, /node -v\s*ok/, 'node -v output is ok')
t.match(output, /node -v\s*(ok|not ok Use node v)/, 'node -v output is ok')
t.match(output, /npm config get registry\s*ok\s*using default/, 'npm config get registry output is ok')
t.match(output, /which git\s*ok/, 'which git output is ok')
t.match(output, /cached files\s*ok/, 'cached files are ok')
Expand Down Expand Up @@ -410,7 +410,7 @@ test('npm doctor generic ping error', t => {
}, 'trackers all finished')
t.match(output, /npm ping\s*not ok\s*generic error/, 'ping output contains trimmed error')
t.match(output, /npm -v\s*ok/, 'npm -v output is ok')
t.match(output, /node -v\s*ok/, 'node -v output is ok')
t.match(output, /node -v\s*(ok|not ok Use node v)/, 'node -v output is ok')
t.match(output, /npm config get registry\s*ok\s*using default/, 'npm config get registry output is ok')
t.match(output, /which git\s*ok/, 'which git output is ok')
t.match(output, /cached files\s*ok/, 'cached files are ok')
Expand Down Expand Up @@ -459,7 +459,7 @@ test('npm doctor outdated npm version', t => {
}, 'trackers all finished')
t.match(output, /npm ping\s*ok/, 'ping output is ok')
t.match(output, /npm -v\s*not ok/, 'npm -v output is not ok')
t.match(output, /node -v\s*ok/, 'node -v output is ok')
t.match(output, /node -v\s*(ok|not ok Use node v)/, 'node -v output is ok')
t.match(output, /npm config get registry\s*ok\s*using default/, 'npm config get registry output is ok')
t.match(output, /which git\s*ok/, 'which git output is ok')
t.match(output, /cached files\s*ok/, 'cached files are ok')
Expand Down Expand Up @@ -508,7 +508,7 @@ test('npm doctor outdated nodejs version', t => {
}, 'trackers all finished')
t.match(output, /npm ping\s*ok/, 'ping output is ok')
t.match(output, /npm -v\s*ok/, 'npm -v output is ok')
t.match(output, /node -v\s*not ok/, 'node -v output is not ok')
t.match(output, /node -v\s*(ok|not ok Use node v)/, 'node -v output is ok')
t.match(output, /npm config get registry\s*ok\s*using default/, 'npm config get registry output is ok')
t.match(output, /which git\s*ok/, 'which git output is ok')
t.match(output, /cached files\s*ok/, 'cached files are ok')
Expand Down Expand Up @@ -654,7 +654,7 @@ test('npm doctor file permission checks', t => {
}, 'trackers all finished')
t.match(output, /npm ping\s*ok/, 'ping output is ok')
t.match(output, /npm -v\s*ok/, 'npm -v output is ok')
t.match(output, /node -v\s*ok/, 'node -v output is ok')
t.match(output, /node -v\s*(ok|not ok Use node v)/, 'node -v output is ok')
t.match(output, /npm config get registry\s*ok\s*using default/, 'npm config get registry output is ok')
t.match(output, /which git\s*ok/, 'which git output is ok')
t.match(output, /cached files\s*not ok/, 'cached files are not ok')
Expand Down Expand Up @@ -703,7 +703,7 @@ test('npm doctor missing git', t => {
}, 'trackers all finished')
t.match(output, /npm ping\s*ok/, 'ping output is ok')
t.match(output, /npm -v\s*ok/, 'npm -v output is ok')
t.match(output, /node -v\s*ok/, 'node -v output is ok')
t.match(output, /node -v\s*(ok|not ok Use node v)/, 'node -v output is ok')
t.match(output, /npm config get registry\s*ok\s*using default/, 'npm config get registry output is ok')
t.match(output, /which git\s*not ok/, 'which git output is not ok')
t.match(output, /cached files\s*ok/, 'cached files are ok')
Expand Down Expand Up @@ -761,7 +761,7 @@ test('npm doctor cache verification showed bad content', t => {
}, 'trackers all finished')
t.match(output, /npm ping\s*ok/, 'ping output is ok')
t.match(output, /npm -v\s*ok/, 'npm -v output is ok')
t.match(output, /node -v\s*ok/, 'node -v output is ok')
t.match(output, /node -v\s*(ok|not ok Use node v)/, 'node -v output is ok')
t.match(output, /npm config get registry\s*ok\s*using default/, 'npm config get registry output is ok')
t.match(output, /which git\s*ok/, 'which git output is ok')
t.match(output, /cached files\s*ok/, 'cached files are ok')
Expand Down Expand Up @@ -820,7 +820,7 @@ test('npm doctor cache verification showed reclaimed content', t => {
}, 'trackers all finished')
t.match(output, /npm ping\s*ok/, 'ping output is ok')
t.match(output, /npm -v\s*ok/, 'npm -v output is ok')
t.match(output, /node -v\s*ok/, 'node -v output is ok')
t.match(output, /node -v\s*(ok|not ok Use node v)/, 'node -v output is ok')
t.match(output, /npm config get registry\s*ok\s*using default/, 'npm config get registry output is ok')
t.match(output, /which git\s*ok/, 'which git output is ok')
t.match(output, /cached files\s*ok/, 'cached files are ok')
Expand Down Expand Up @@ -878,7 +878,7 @@ test('npm doctor cache verification showed missing content', t => {
}, 'trackers all finished')
t.match(output, /npm ping\s*ok/, 'ping output is ok')
t.match(output, /npm -v\s*ok/, 'npm -v output is ok')
t.match(output, /node -v\s*ok/, 'node -v output is ok')
t.match(output, /node -v\s*(ok|not ok Use node v)/, 'node -v output is ok')
t.match(output, /npm config get registry\s*ok\s*using default/, 'npm config get registry output is ok')
t.match(output, /which git\s*ok/, 'which git output is ok')
t.match(output, /cached files\s*ok/, 'cached files are ok')
Expand Down Expand Up @@ -929,7 +929,7 @@ test('npm doctor not using default registry', t => {
}, 'trackers all finished')
t.match(output, /npm ping\s*ok/, 'ping output is ok')
t.match(output, /npm -v\s*ok/, 'npm -v output is ok')
t.match(output, /node -v\s*ok/, 'node -v output is ok')
t.match(output, /node -v\s*(ok|not ok Use node v)/, 'node -v output is ok')
t.match(output, /npm config get registry\s*not ok/, 'npm config get registry output is not ok')
t.match(output, /which git\s*ok/, 'which git output is ok')
t.match(output, /cached files\s*ok/, 'cached files are ok')
Expand Down
10 changes: 9 additions & 1 deletion test/lib/utils/reify-finish.js
Expand Up @@ -20,7 +20,7 @@ let expectWrite = false
const realFs = require('fs')
const fs = {
...realFs,
promises: {
promises: realFs.promises && {
...realFs.promises,
writeFile: async (path, data) => {
if (!expectWrite)
Expand Down Expand Up @@ -78,3 +78,11 @@ t.test('should write if everything above passes', async t => {
const data = fs.readFileSync(`${path}/npmrc`, 'utf8').replace(/\r\n/g, '\n')
t.matchSnapshot(data, 'written config')
})

t.test('works without fs.promises', async t => {
t.doesNotThrow(() => requireInject('../../../lib/utils/reify-finish.js', {
fs: { ...fs, promises: null },
'../../../lib/npm.js': npm,
'../../../lib/utils/reify-output.js': reifyOutput,
}))
})

0 comments on commit 5daf858

Please sign in to comment.