Skip to content

Commit

Permalink
Polyfill fs.readdir withFileTypes for node 10.0 - 10.9
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Feb 5, 2021
1 parent f86abfb commit 1653d37
Show file tree
Hide file tree
Showing 7 changed files with 535 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -6,7 +6,7 @@ jobs:
build:
strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]
node-version: [10.0.x, 10.x, 12.0.x, 12.x, 14.0.x, 14.x, 15.x]
os: [ubuntu-latest, macOS-latest, windows-latest]
fail-fast: false

Expand Down
13 changes: 13 additions & 0 deletions index.js
Expand Up @@ -22,6 +22,7 @@ const fs = require('fs')
const readFile = promisify(fs.readFile)
const readdir = promisify(fs.readdir)
const stat = promisify(fs.stat)
const lstat = promisify(fs.lstat)
const {relative, resolve, basename, dirname} = require('path')
const normalizePackageBin = require('npm-normalize-package-bin')

Expand Down Expand Up @@ -131,6 +132,18 @@ const pkgContents = async ({

const recursePromises = []

// if we didn't get withFileTypes support, tack that on
if (typeof dirEntries[0] === 'string') {
// use a map so we can return a promise, but we mutate dirEntries in place
// this is much slower than getting the entries from the readdir call,
// but polyfills support for node versions before 10.10
await Promise.all(dirEntries.map(async (name, index) => {
const p = resolve(path, name)
const st = await lstat(p)
dirEntries[index] = Object.assign(st, {name})
}))
}

for (const entry of dirEntries) {
const p = resolve(path, entry.name)
if (entry.isDirectory() === false) {
Expand Down
128 changes: 127 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -16,9 +16,11 @@
"postpublish": "git push origin --follow-tags"
},
"tap": {
"check-coverage": true
"check-coverage": true,
"color": true
},
"devDependencies": {
"require-inject": "^1.4.4",
"tap": "^14.11.0"
},
"dependencies": {
Expand Down

0 comments on commit 1653d37

Please sign in to comment.