-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFileSystem.ts
60 lines (54 loc) · 1.52 KB
/
FileSystem.ts
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
export type FileSystemEntryType = "File" | "Folder";
export interface FileSystemEntry {
name: string;
path: string;
type: FileSystemEntryType;
}
export interface File extends FileSystemEntry {}
export interface Folder extends FileSystemEntry {
entries: FolderEntries;
}
export type FolderEntries = Array<File | Folder>;
/**
* Flattens the "Folder" hierarchy and returns an array of folder paths.
*
* @param entries - The entries of the root folder.
* @param basePath - The base path of the root folder.
* @returns An array of folder paths.
*/
export function flatFolders(
entries: Folder["entries"],
basePath: string,
): string[] {
return entries.reduce<string[]>((acc, entry) => {
if (entry.type === "Folder") {
return acc.concat([
basePath + "/" + entry.name,
...flatFolders((entry as Folder).entries, basePath + "/" + entry.name),
]);
} else {
return acc;
}
}, []);
}
/**
* Flattens the files in a "Folder" hierarchy and returns an array of file paths.
*
* @param entries - The entries of the root folder.
* @param basePath - The base path of the root folder.
* @returns An array of file paths.
*/
export function flatFiles(
entries: Folder["entries"],
basePath: string,
): string[] {
return entries.reduce<string[]>((acc, entry) => {
if (entry.type === "Folder") {
return acc.concat(
flatFiles((entry as Folder).entries, basePath + "/" + entry.name),
);
} else {
return acc.concat(basePath + "/" + entry.name);
}
}, []);
}