-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v4.0.0 - use readdir builtin, node >18 required for recursion
- Loading branch information
Showing
17 changed files
with
149 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,6 @@ jobs: | |
- macos-latest | ||
- windows-latest | ||
node: | ||
- '10' | ||
- '12' | ||
- '14' | ||
- '16' | ||
- '18' | ||
- '20' | ||
- '21' | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,39 +1,27 @@ | ||
import readdir from 'readdir-cluster' | ||
import { readdir } from 'node:fs/promises' | ||
import { basename } from 'node:path' | ||
import isValidFilename from '@bevry/valid-filename' | ||
|
||
/** Array of paths that are invalid */ | ||
export type InvalidPaths = string[] | ||
export type Paths = string[] | ||
|
||
/** Whether or not the directory was valid */ | ||
export type ValidateResult = | ||
| [valid: false, invalidPaths: InvalidPaths] | ||
| [valid: true] | ||
| [valid: false, invalidRelativePaths: Paths, relativePaths: Paths] | ||
| [valid: true, invalidRelativePaths: [], relativePaths: Paths] | ||
|
||
/** Iterator for readdir-cluster that validates the paths using valid-filename */ | ||
export function validator( | ||
this: InvalidPaths, | ||
/** Validate a directory and its descendants */ | ||
export default async function validate( | ||
fullPath: string, | ||
relativePath: string, | ||
) { | ||
const valid = isValidFilename(relativePath) | ||
|
||
if (!valid) { | ||
this.push(fullPath) | ||
): Promise<ValidateResult> { | ||
// https://nodejs.org/api/fs.html#fspromisesreaddirpath-options | ||
const relativePaths: Paths = await readdir(fullPath, { recursive: true }) | ||
const invalidRelativePaths: Paths = relativePaths.filter( | ||
(relativePath) => !isValidFilename(basename(relativePath)), | ||
) | ||
if (invalidRelativePaths.length) { | ||
return [false, invalidRelativePaths, relativePaths] | ||
} else { | ||
return [true, [], relativePaths] | ||
} | ||
} | ||
|
||
/** Validate a directory and its descendants */ | ||
export default function validate(fullPath: string): Promise<ValidateResult> { | ||
return new Promise(function (resolve, reject) { | ||
const invalidPaths: InvalidPaths = [] | ||
readdir(fullPath, validator.bind(invalidPaths), function (err: Error) { | ||
if (err) { | ||
return reject(err) | ||
} else if (invalidPaths.length) { | ||
return resolve([false, invalidPaths]) | ||
} else { | ||
return resolve([true]) | ||
} | ||
}) | ||
}) | ||
} |
Oops, something went wrong.