forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.utils.ts
More file actions
61 lines (54 loc) · 1.46 KB
/
filesystem.utils.ts
File metadata and controls
61 lines (54 loc) · 1.46 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
/*!
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {normalizePath} from './navigation.utils';
import {FileAndContent} from '../interfaces';
interface DirEnt<T> {
name: T;
isFile(): boolean;
isDirectory(): boolean;
}
interface FileSystemAPI {
readdir(
path: string,
options: {
encoding?:
| 'ascii'
| 'utf8'
| 'utf-8'
| 'utf16le'
| 'ucs2'
| 'ucs-2'
| 'base64'
| 'base64url'
| 'latin1'
| 'binary'
| 'hex'
| null;
withFileTypes: true;
},
): Promise<DirEnt<string>[]>;
readFile(path: string, encoding?: string): Promise<string>;
}
export const checkFilesInDirectory = async (
dir: string,
fs: FileSystemAPI,
filterFoldersPredicate: (path?: string) => boolean = () => true,
files: FileAndContent[] = [],
) => {
const entries = (await fs.readdir(dir, {withFileTypes: true})) ?? [];
for (const entry of entries) {
const fullPath = normalizePath(`${dir}/${entry.name}`);
if (entry.isFile()) {
const content = await fs.readFile(fullPath, 'utf-8');
files.push({content, path: fullPath});
} else if (entry.isDirectory() && filterFoldersPredicate(entry.name)) {
await checkFilesInDirectory(fullPath, fs, filterFoldersPredicate, files);
}
}
return files;
};