lemmy-js-client/putTypesInIndex.js
SleeplessOne1917 40dfb4c3ac
Make script for adding types to index (#118)
* Make script for adding types to index

* Add semicolons to export lines
2023-05-11 13:10:28 -04:00

36 lines
997 B
JavaScript

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 * from "./websocket";'];
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();