-
Notifications
You must be signed in to change notification settings - Fork 22
/
index-buffer.ts
59 lines (58 loc) · 1.93 KB
/
index-buffer.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
import { StringReader } from "https://deno.land/std@0.147.0/io/mod.ts";
import { CodeEntry } from "../index.ts";
import * as directory from "../../misc/directory.ts";
export interface IndexBuffer {
reExport(from: string, item: string, as: string): void;
[Symbol.iterator](): Generator<CodeEntry>;
}
export interface CreateIndexBufferConfig {
indexFilename?: string;
}
export function createIndexBuffer(
config?: CreateIndexBufferConfig,
): IndexBuffer {
interface Items {
[as: string]: string;
}
let root: directory.Folder<Items> = directory.empty;
const indexFilename = config?.indexFilename || "index";
const folderEntryToImportStmt = ([name]: [string, unknown]) => {
return `import * as ${name} from "./${name}/${indexFilename}.ts";\n`;
};
return {
reExport(from, item, as) {
const path = directory.textToPath(from);
const items = directory.get(root, path)?.value;
root = directory.set(root, path, { ...items, [item]: as });
},
*[Symbol.iterator]() {
for (const [path, folder] of directory.walkFolders(root)) {
const entries = Object.entries(folder.children);
const folders = entries.filter(
([name, node]) => !name.startsWith("(") && !node.value,
);
const files = entries.filter(([, node]) => node.value);
const codes = [
...folders.map(folderEntryToImportStmt),
folders.length
? `export type {\n${
folders.map(([name]) => ` ${name},\n`).join("")
}};\n`
: "",
...files.flatMap(
([name, { value }]) =>
Object.entries(value!).map(
([item, as]) => (
`export type { ${item} as ${as} } from "./${name}.ts";\n`
),
),
),
];
yield [
[...path, `${indexFilename}.ts`].join("/"),
new StringReader(codes.join("")),
];
}
},
};
}