UNPKG

valid-directory

Version:

Check whether or not a directory and its descendants are valid

16 lines (15 loc) 656 B
import { readdir } from 'node:fs/promises'; import { basename } from 'node:path'; import isValidFilename from '@bevry/valid-filename'; /** Validate a directory and its descendants */ export default async function validate(fullPath) { // https://nodejs.org/api/fs.html#fspromisesreaddirpath-options const relativePaths = await readdir(fullPath, { recursive: true }); const invalidRelativePaths = relativePaths.filter((relativePath) => !isValidFilename(basename(relativePath))); if (invalidRelativePaths.length) { return [false, invalidRelativePaths, relativePaths]; } else { return [true, [], relativePaths]; } }