-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.mts
More file actions
176 lines (167 loc) · 4.07 KB
/
example.mts
File metadata and controls
176 lines (167 loc) · 4.07 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* @file Usage Example
* @module example
*/
import type {
File,
AnyParent as Parent,
Root
} from '@flex-development/fst'
import {
fromFileSystem,
type Dirent,
type FileSystem,
type Stats
} from '@flex-development/fst-util-from-fs'
import pathe from '@flex-development/pathe'
import { inspect } from '@flex-development/unist-util-inspect'
import { ok } from 'devlop'
import fs from 'node:fs'
import { size } from 'unist-util-size'
declare module '@flex-development/fst' {
interface File {
/**
* The size of the file.
*/
size?: bigint | number | undefined
}
}
declare module '@flex-development/fst-util-from-fs' {
interface Stats {
/**
* The size of the entry.
*/
size?: bigint | number | undefined
}
}
/**
* A glob pattern matching directories to search
* and files to include in the tree.
*
* @const {string} pattern
*/
const pattern: string = 'src/**/**'
/**
* The file system tree.
*
* @const {Root} tree
*/
const tree: Root = await fromFileSystem({
extensions: '.mts',
filters: {
/**
* @this {void}
*
* @param {string} path
* The path to the directory, relative to `tree.path`
* @param {number | null | undefined} depth
* The current search depth
* @param {Dirent} dirent
* The dirent representing the file system entry
* @param {Parent} parent
* The parent node
* @param {Root} tree
* The file system tree
* @return {boolean}
* `true` if node for `path` should be added, `false` otherwise
*/
directory(
this: void,
path: string,
depth: number | null | undefined,
dirent: Dirent,
parent: Parent,
tree: Root
): boolean {
return pathe.matchesGlob(path, pattern, {
cwd: tree.path,
dot: false,
ignore: ['**/__mocks__/**', '**/__snapshots__/**', '**/__tests__/**'],
noglobstar: false
})
},
/**
* @this {void}
*
* @param {string} path
* The path to the file, relative to `tree.path`
* @param {number | null | undefined} depth
* The current search depth
* @param {Dirent} dirent
* The dirent representing the file system entry
* @param {Parent} parent
* The parent node
* @param {Root} tree
* The file system tree
* @return {boolean}
* `true` if node for `path` should be added, `false` otherwise
*/
file(
this: void,
path: string,
depth: number | null | undefined,
dirent: Dirent,
parent: Parent,
tree: Root
): boolean {
return pathe.matchesGlob(path, pattern, {
cwd: tree.path,
dot: true,
ignore: ['**/.DS_Store'],
noglobstar: false
})
}
},
fs: fs.promises,
handles: {
/**
* @async
*
* @this {void}
*
* @param {File} node
* The node representing the file
* @param {Dirent} dirent
* The dirent representing the file
* @param {Parent} parent
* The parent of `node`
* @param {Root} tree
* The current file system tree
* @param {Parent[]} ancestors
* The ancestors of `node`, with the last node being `parent`
* @param {FileSystem} fs
* The file system API
* @return {Promise<undefined>}
*/
async file(
this: void,
node: File,
dirent: Dirent,
parent: Parent,
tree: Root,
ancestors: Parent[],
fs: FileSystem
): Promise<undefined> {
/**
* The list of relative ancestor paths.
*
* @const {string[]} paths
*/
const paths: string[] = [...ancestors.slice(1), node].map(n => {
ok(n.type !== 'root', 'did not expect tree')
return n.name
})
/**
* Info about the file.
*
* @const {Stats} stats
*/
const stats: Stats = await fs.stat(pathe.join(tree.path, ...paths))
return node.size = stats.size, void node
}
}
})
console.log(inspect(tree))
console.dir(size(tree))
console.dir(size(tree, node => node.type === 'directory'))
console.dir(size(tree, node => node.type === 'file'))