summary refs log tree commit diff
path: root/scripts/gen_index.js
blob: 8a3c7eb8297bde04161089c38b148da90fc8b345 (plain) (blame)
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
const path = require("path");
const fs = require("fs");
const { execIn, getLines } = require("./utils");

if (!process.argv[2] || !fs.existsSync(process.argv[2])) {
	console.log("Please pass a directory that exists!");
	process.exit(1);
}
console.log(`// ${process.argv[2]}/index.ts`);
const recurse = process.argv.includes("--recursive");

const files = fs.readdirSync(process.argv[2]).filter((x) => x.endsWith(".ts") && x != "index.ts");

let output = "";

files.forEach((x) => (output += `export * from "./${x.replaceAll(".ts", "")}";\n`));

const dirs = fs.readdirSync(process.argv[2]).filter((x) => {
	try {
		fs.readdirSync(path.join(process.argv[2], x));
		return true;
	} catch (e) {
		return false;
	}
});
dirs.forEach((x) => {
	output += `export * from "./${x}/index";\n`;
});
console.log(output);
fs.writeFileSync(path.join(process.argv[2], "index.ts"), output);

dirs.forEach((x) => {
	if (recurse)
		console.log(
			execIn([process.argv[0], process.argv[1], `"${path.join(process.argv[2], x)}"`, "--recursive"].join(" "), process.cwd())
		);
});