-
-
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.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
8c366ce
commit 240ac3e
Showing
43 changed files
with
1,797 additions
and
33 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,37 @@ | ||
/** | ||
* @file Test Utilities - buildPath | ||
* @module tests/utils/buildPath | ||
*/ | ||
|
||
import type { Directory, DirectoryContent, Root } from '@flex-development/fst' | ||
import pathe from '@flex-development/pathe' | ||
|
||
/** | ||
* Get the path to `node`. | ||
* | ||
* @see {@linkcode DirectoryContent} | ||
* @see {@linkcode Directory} | ||
* @see {@linkcode Root} | ||
* | ||
* @this {void} | ||
* | ||
* @param {DirectoryContent} node | ||
* Current node | ||
* @param {Directory | Root} parent | ||
* Parent of `node` | ||
* @param {(Directory | Root)[]} ancestors | ||
* List of ancestor nodes where the last node is the grandparent of `node` | ||
* @return {undefined} | ||
*/ | ||
function buildPath( | ||
this: void, | ||
node: DirectoryContent, | ||
parent: Directory | Root, | ||
ancestors: (Directory | Root)[] | ||
): string { | ||
return pathe.join(...[...ancestors, parent, node].map(node => { | ||
return 'path' in node ? node.path : node.name | ||
})) | ||
} | ||
|
||
export default buildPath |
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,90 @@ | ||
/** | ||
* @file Test Utilities - readdir | ||
* @module tests/utils/readdir | ||
*/ | ||
|
||
import toPath from '#internal/to-path' | ||
import pathe from '@flex-development/pathe' | ||
import fs from 'node:fs' | ||
|
||
export { readdir as default, type ReadResult } | ||
|
||
/** | ||
* Read directory result. | ||
*/ | ||
type ReadResult = { | ||
/** | ||
* List of direcory paths. | ||
*/ | ||
directories: string[] | ||
|
||
/** | ||
* List of files. | ||
*/ | ||
files: string[] | ||
} | ||
|
||
/** | ||
* Get the contents of the directory at `dir`. | ||
* | ||
* @this {void} | ||
* | ||
* @param {string} dir | ||
* Directory URL or path to directory | ||
* @param {number | null | undefined} depth | ||
* Maximum search depth (inclusive) | ||
* @param {ReadResult | null | undefined} [ctx] | ||
* Read directory context | ||
* @return {ReadResult} | ||
* Read directory result | ||
*/ | ||
function readdir( | ||
this: void, | ||
dir: URL | string, | ||
depth?: number | null | undefined, | ||
ctx?: ReadResult | null | undefined | ||
): ReadResult { | ||
ctx ??= { directories: [], files: [] } | ||
|
||
if ( | ||
depth === null || | ||
depth === undefined || | ||
typeof depth === 'number' && depth > 0 | ||
) { | ||
dir = toPath(dir) | ||
|
||
/** | ||
* List of subdirectories. | ||
* | ||
* @const {string[]} subdirectories | ||
*/ | ||
const subdirectories: string[] = [] | ||
|
||
for (const dirent of fs.readdirSync(dir, { withFileTypes: true })) { | ||
/** | ||
* Relative path to directory or file. | ||
* | ||
* @const {string} path | ||
*/ | ||
const path: string = pathe.join(dir, dirent.name) | ||
|
||
if (dirent.isDirectory()) { | ||
subdirectories.push(path) | ||
} else { | ||
ctx.files.push(path) | ||
} | ||
} | ||
|
||
if (typeof depth === 'number') { | ||
depth-- | ||
if (depth <= 0) return ctx | ||
} | ||
|
||
for (const path of subdirectories) { | ||
ctx.directories.push(path) | ||
readdir(path, depth, ctx) | ||
} | ||
} | ||
|
||
return ctx | ||
} |
Oops, something went wrong.