-
Notifications
You must be signed in to change notification settings - Fork 57
/
putTypesInIndex.js
39 lines (32 loc) · 1.04 KB
/
putTypesInIndex.js
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
const fs = require("fs/promises");
const path = require("path");
const exportRegex = /export\s+(?:enum|interface|type)\s+([A-Za-z0-9_]+)/g;
const baseExports = [
'export * from "./http";',
'export {UploadImage,UploadImageResponse,ImageFile,DeleteImage} from "./other_types";',
];
async function putTypesInIndex() {
const typeFiles = await fs.readdir(path.resolve("./src/types"));
const exports = [...baseExports];
for (const filename of typeFiles) {
const localExports = [];
const fileText = await fs.readFile(path.resolve(`./src/types/${filename}`));
for (
let match = exportRegex.exec(fileText);
match;
match = exportRegex.exec(fileText)
) {
localExports.push(match[1]);
}
const spacer = localExports.length > 1 ? "\n" : " ";
exports.push(
`export {${spacer}${localExports.join(
",\n",
)}${spacer}} from "./types/${filename.replace(/\..+/, "")}";`,
);
}
fs.writeFile(path.resolve("./src/index.ts"), exports.join("\n"), {
flag: "w",
});
}
putTypesInIndex();