Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 21x 21x 21x 21x 21x 17x 4x 21x 1x 17x 17x 17x 17x 17x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x | import {
unfoldEffect,
type ParentEffectUnfolder,
type Tree,
type TreeEffectUnfold,
} from '#tree'
import {FileSystem, Path, type Error} from '@effect/platform'
import {Array, Effect, pipe} from 'effect'
import * as NFS from 'node:fs'
import {handleErrnoException} from './error.js'
import type {DirectoryEffect, DirectoryEntry, HasFileType} from './types.js'
/**
* @example
* import {drawTree, map, FileSystem} from 'effect-tree'
* import {Effect, pipe} from 'effect'
* import {NodeContext, NodeRuntime} from '@effect/platform-node'
* import {Path} from '@effect/platform'
*
* const effect = Effect.gen(function* () {
* const path = yield* Path.Path
*
* // Goto src/fileSystem/fixtures
* const directory = path.join(
* import.meta.dirname,
* '..',
* '..',
* '..',
* 'src',
* 'fileSystem',
* 'fixtures',
* )
*
* const tree = yield* FileSystem.readDirectoryTree(directory)
*
* const mapped = map(tree, ({name}) => name)
* const drawn = drawTree(mapped)
* expect(drawn).toEqual([
* '┬fixtures ',
* '└┬directory ',
* ' ├─file ',
* ' └┬inner-directory',
* ' └─inner-file '
* ])
* })
*
* pipe(effect, Effect.provide(NodeContext.layer), NodeRuntime.runMain)
* @category filesystem
* @function
*/
export const readDirectoryTree = (
directoryPath: string,
): DirectoryEffect<Tree<DirectoryEntry>> =>
Effect.gen(function* () {
const path = yield* Path.Path
return yield* readDirectoryTreeEntries({
name: path.basename(directoryPath),
parentPath: path.dirname(directoryPath),
type: 'Directory',
})
})
/**
* A function of the type:
*
* ```ts
* (entry: DirectoryEntry) => Effect.Effect<
* Tree<DirectoryEntry>,
* Error.PlatformError,
* FileSystem.FileSystem
* >
* ```
*
* @category filesystem
* @function
*/
export const readDirectoryTreeEntries: TreeEffectUnfold<
DirectoryEntry,
DirectoryEntry,
Error.PlatformError,
FileSystem.FileSystem | Path.Path
> = entry => unfoldEffect(readDirectoryParentUnfolder)(entry)
const readDirectoryParentUnfolder: ParentEffectUnfolder<
DirectoryEntry,
Error.PlatformError,
FileSystem.FileSystem | Path.Path
> = entry =>
Effect.gen(function* () {
const path = yield* Path.Path
const {name, parentPath, type} = entry
yield* FileSystem.FileSystem
return type === 'Directory'
? yield* pipe(path.join(parentPath, name), readDirectoryEntries)
: []
})
const readDirectoryEntries = (
path: string,
): DirectoryEffect<DirectoryEntry[]> =>
pipe(
Effect.tryPromise({
try: () => NFS.promises.readdir(path, {withFileTypes: true}),
catch: err =>
handleErrnoException('FileSystem', 'readDirectoryEntry')(
err as NodeJS.ErrnoException,
[path],
),
}),
Effect.map(
Array.map(
(entry): DirectoryEntry => ({
name: entry.name,
parentPath: entry.parentPath,
type: makeFileType(entry),
}),
),
),
)
/* v8 ignore next 15 */
// eslint-disable-next-line sonarjs/cognitive-complexity
const makeFileType = (stat: HasFileType): FileSystem.File.Type =>
stat.isFile()
? 'File'
: stat.isDirectory()
? 'Directory'
: stat.isSymbolicLink()
? 'SymbolicLink'
: stat.isSocket()
? 'Socket'
: stat.isBlockDevice()
? 'BlockDevice'
: stat.isCharacterDevice()
? 'CharacterDevice'
: 'FIFO'
|