Skip to content

Commit

Permalink
Fix readdir recursive issue by switching to glob
Browse files Browse the repository at this point in the history
  • Loading branch information
mvirgil committed Nov 4, 2024
1 parent 3111261 commit 23bad93
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 61 deletions.
57 changes: 10 additions & 47 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"license": "Apache-2.0",
"dependencies": {
"chalk": "^4.1.2",
"commander": "^12.1.0"
"commander": "^12.1.0",
"glob": "^11.0.0"
},
"devDependencies": {
"@stylistic/eslint-plugin": "^2.9.0",
Expand All @@ -25,7 +26,6 @@
"@typescript-eslint/parser": "^8.11.0",
"eslint": "^8.57.1",
"eslint-plugin-header": "^3.1.1",
"glob": "^11.0.0",
"ts-node": "^10.9.2",
"typescript": "^5.6.3"
},
Expand Down
26 changes: 14 additions & 12 deletions src/utils/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import path from 'node:path';
import readline from 'node:readline';
import os from 'node:os';
import { finished } from 'node:stream/promises';
import { glob } from 'glob';

const TEMP_FILE_EXTENSION: string = '.olly.tmp';

Expand All @@ -32,18 +33,19 @@ const TEMP_FILE_EXTENSION: string = '.olly.tmp';
* - path/to/dist/nested/folder/page1.js
*/
export async function readdirRecursive(dir: string) {
const dirents = await readdir(
dir,
{
encoding: 'utf-8',
recursive: true,
withFileTypes: true
}
);
const filePaths = dirents
.filter(dirent => dirent.isFile())
.map(dirent => path.join(dirent.parentPath, dirent.name));
return filePaths;
// Using 'glob' instead of native 'readdir' due to:
// https://github.com/nodejs/node/issues/48858
// https://github.com/nodejs/node/issues/51773

// glob does not report some readdir errors (ENOTDIR, EACCES)
// that our user should know about, so try reading the dir before running glob
await readdir(dir);

const partialPaths = await glob('**/*', {
cwd: dir,
nodir: true,
});
return partialPaths.map(partialPath => path.join(dir, partialPath));
}

export function readlines(stream: ReadStream): AsyncIterable<string> {
Expand Down
Loading

0 comments on commit 23bad93

Please sign in to comment.