-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b421815
commit 260f1bd
Showing
7 changed files
with
463 additions
and
569 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { lstatSync, readdirSync, Dirent } from 'fs'; | ||
import { join, basename, resolve, dirname } from 'path'; | ||
|
||
export type WalkFunc = (pathName: string, dirent: Dirent | Error) => Boolean | void; | ||
|
||
function lstatSafe(pathName: string): Dirent | Error { | ||
try { const result = lstatSync(pathName) as unknown as Dirent; result.name = basename(resolve(pathName)); return result; } | ||
catch (err) { return err } | ||
} | ||
|
||
function readdirSafe(pathName: string): Dirent[] | Error[] { | ||
try { return readdirSync(pathName, { withFileTypes: true }); } | ||
catch (err) { return [err] } | ||
} | ||
|
||
function walkDeep(pathName: string, walkFunc: WalkFunc, dirent: Dirent | Error) { | ||
// If the callback returns false, the Dirent errored, or entry is a file, we can skip. Otherwise, walk our directory. | ||
if (walkFunc(dirname(pathName), dirent) === false || dirent instanceof Error || !dirent.isDirectory()) { return; } | ||
for (const entry of readdirSafe(pathName)) { walkDeep(join(pathName, entry.name), walkFunc, entry) } | ||
} | ||
|
||
// Get the very first file or folder an walk deep. | ||
const walk = (pathName: string, walkFunc: WalkFunc) => walkDeep(pathName, walkFunc, lstatSafe(pathName)); | ||
export default walk; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.