Skip to content

Commit

Permalink
v4.0.0 - use readdir builtin, node >18 required for recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Oct 30, 2023
1 parent e3243fe commit a91fcb0
Show file tree
Hide file tree
Showing 17 changed files with 162 additions and 94 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/bevry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ jobs:
- macos-latest
- windows-latest
node:
- '10'
- '12'
- '14'
- '16'
- '18'
- '20'
- '21'
Expand Down
7 changes: 7 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# History

## v4.0.0 2023 October 30

- API now returns all paths as the third argument
- Make the tests work on Windows
- Swapped out [readdir-cluster](https://github.com/bevry/readdir-cluster) for `recursive: true` in [`readdir` builtin](https://github.com/bevry/readdir-cluster) to solve `ERR_IPC_CHANNEL_CLOSED` errors
- This requires Node.js versions >= 18 for recursive support

## v3.10.0 2023 October 30

- Updated dependencies, [base files](https://github.com/bevry/base), and [editions](https://editions.bevry.me) using [boundation](https://github.com/bevry/boundation)
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Check whether or not a directory and its descendants are valid

Validity determined via the [`@bevry/valid-filename` package](https://github.com/bevry/valid-filename).

Requires Node.js version >= 18 for recursive directory support.

## Usage

[Complete API Documentation.](http://master.valid-directory.bevry.surge.sh/docs/)
Expand Down Expand Up @@ -63,7 +65,7 @@ validate(path)

Run `npm install -g valid-directory` then run against the current working directory with `valid-directory` or a specified directory via `valid-directory <path>`.

Exit code will be `1` if validation failed to execute, `2` if validation failed, and `0` if validation passed.
Exit code will be `1` if a path is invalid, `2` if something went wrong, and `0` if validation passed.

### Packages

Expand Down
38 changes: 20 additions & 18 deletions package-lock.json

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

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "valid-directory",
"version": "3.10.0",
"version": "4.0.0",
"description": "Check whether or not a directory and its descendants are valid",
"homepage": "https://github.com/bevry/valid-directory",
"license": "Artistic-2.0",
Expand Down Expand Up @@ -72,7 +72,7 @@
"url": "https://github.com/bevry/valid-directory.git"
},
"engines": {
"node": ">=10"
"node": ">=18"
},
"editions": [
{
Expand All @@ -97,7 +97,7 @@
"require"
],
"engines": {
"node": "10 || 12 || 14 || 16 || 18 || 20 || 21",
"node": "18 || 20 || 21",
"browsers": false
}
},
Expand All @@ -112,7 +112,7 @@
"import"
],
"engines": {
"node": "12 || 14 || 16 || 18 || 20 || 21",
"node": "18 || 20 || 21",
"browsers": false
}
}
Expand All @@ -129,7 +129,7 @@
},
"dependencies": {
"@bevry/valid-filename": "^1.0.0",
"readdir-cluster": "^3.17.0"
"fdir": "^6.1.0"
},
"devDependencies": {
"@bevry/update-contributors": "^1.22.0",
Expand All @@ -155,21 +155,21 @@
"our:clean": "rm -Rf ./docs ./edition* ./es2015 ./es5 ./out ./.next",
"our:compile": "npm run our:compile:deno && npm run our:compile:edition-es2017 && npm run our:compile:edition-es2017-esm && npm run our:compile:types",
"our:compile:deno": "make-deno-edition --attempt",
"our:compile:edition-es2017": "tsc --module commonjs --target ES2017 --outDir ./edition-es2017 --project tsconfig.json && ( test ! -d edition-es2017/source || ( mv edition-es2017/source edition-temp && rm -Rf edition-es2017 && mv edition-temp edition-es2017 ) ) && echo '{\"type\": \"commonjs\"}' > edition-es2017/package.json",
"our:compile:edition-es2017-esm": "tsc --module ESNext --target ES2017 --outDir ./edition-es2017-esm --project tsconfig.json && ( test ! -d edition-es2017-esm/source || ( mv edition-es2017-esm/source edition-temp && rm -Rf edition-es2017-esm && mv edition-temp edition-es2017-esm ) ) && echo '{\"type\": \"module\"}' > edition-es2017-esm/package.json",
"our:compile:edition-es2017": "tsc --module commonjs --target ES2017 --outDir ./edition-es2017 --project tsconfig.json && ( test ! -d edition-es2017/source || ( mv edition-es2017/source edition-temp && rm -Rf edition-es2017 && mv edition-temp edition-es2017 ) ) && printf '%s' '{\"type\": \"commonjs\"}' > edition-es2017/package.json",
"our:compile:edition-es2017-esm": "tsc --module ESNext --target ES2017 --outDir ./edition-es2017-esm --project tsconfig.json && ( test ! -d edition-es2017-esm/source || ( mv edition-es2017-esm/source edition-temp && rm -Rf edition-es2017-esm && mv edition-temp edition-es2017-esm ) ) && printf '%s' '{\"type\": \"module\"}' > edition-es2017-esm/package.json",
"our:compile:types": "tsc --project tsconfig.json --emitDeclarationOnly --declaration --declarationMap --declarationDir ./compiled-types && ( test ! -d compiled-types/source || ( mv compiled-types/source edition-temp && rm -Rf compiled-types && mv edition-temp compiled-types ) )",
"our:deploy": "echo no need for this project",
"our:deploy": "printf '%s\n' 'no need for this project'",
"our:meta": "npm run our:meta:contributors && npm run our:meta:docs && npm run our:meta:projectz",
"our:meta:contributors": "update-contributors",
"our:meta:docs": "npm run our:meta:docs:typedoc",
"our:meta:docs:typedoc": "rm -Rf ./docs && typedoc --exclude '**/+(*test*|node_modules)' --excludeExternals --out ./docs ./source",
"our:meta:projectz": "projectz compile",
"our:release": "npm run our:release:prepare && npm run our:release:check-changelog && npm run our:release:check-dirty && npm run our:release:tag && npm run our:release:push",
"our:release:check-changelog": "cat ./HISTORY.md | grep v$npm_package_version || (echo add a changelog entry for v$npm_package_version && exit -1)",
"our:release:check-changelog": "cat ./HISTORY.md | grep v$npm_package_version || (printf '%s\n' 'add a changelog entry for v$npm_package_version' && exit -1)",
"our:release:check-dirty": "git diff --exit-code",
"our:release:prepare": "npm run our:clean && npm run our:compile && npm run our:test && npm run our:meta",
"our:release:push": "git push origin && git push origin --tags",
"our:release:tag": "export MESSAGE=$(cat ./HISTORY.md | sed -n \"/## v$npm_package_version/,/##/p\" | sed 's/## //' | awk 'NR>1{print buf}{buf = $0}') && test \"$MESSAGE\" || (echo 'proper changelog entry not found' && exit -1) && git tag v$npm_package_version -am \"$MESSAGE\"",
"our:release:tag": "export MESSAGE=$(cat ./HISTORY.md | sed -n \"/## v$npm_package_version/,/##/p\" | sed 's/## //' | awk 'NR>1{print buf}{buf = $0}') && test \"$MESSAGE\" || (printf '%s\n' 'proper changelog entry not found' && exit -1) && git tag v$npm_package_version -am \"$MESSAGE\"",
"our:setup": "npm run our:setup:install",
"our:setup:install": "npm install",
"our:test": "npm run our:verify && npm test",
Expand Down
1 change: 1 addition & 0 deletions source/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ validate(path)
`${path} is invalid, due to the following paths:\n`,
invalidPaths,
)
process.exitCode = 1
}
})
.catch((err) => {
Expand Down
46 changes: 17 additions & 29 deletions source/index.ts
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])
}
})
})
}
Loading

0 comments on commit a91fcb0

Please sign in to comment.