commit 5881150d1f2845827602c450d296edd819d22515
parent 932ac91366c0a391baefd420d63aa2d458514053
Author: Nullptrderef <nullptrderef@proton.me>
Date: Sun, 26 May 2024 01:49:09 +0200
feat: generate a main.ts
Diffstat:
1 file changed, 28 insertions(+), 11 deletions(-)
diff --git a/contrib/type-extractor/extract-types.mjs b/contrib/type-extractor/extract-types.mjs
@@ -13,8 +13,6 @@ const runFileJob = async (file) => {
const tsDefs = file.match(/[\t ]*\.\. ts\:def\:\: [a-zA-Z][a-zA-Z0-9_]*/g);
/** @type string[] */
const defines = [];
- /** @type string[] */
- const imports = [];
let dtsOutput = "";
if (tsDefs)
for (const def of tsDefs) {
@@ -113,17 +111,17 @@ export { ${defines.join(", ")} };
"net.taler.docs.ts-extracted"
);
const tsDocOutput = path.join(output, "dts");
- const zodOutput = path.join(output, "zod");
+ // const zodOutput = path.join(output, "zod"); // TODO: this would be cool to have in future
if (fsSync.existsSync(tsDocOutput))
await fs.rm(tsDocOutput, { recursive: true });
await fs.mkdir(tsDocOutput, {
recursive: true,
});
- if (fsSync.existsSync(zodOutput)) await fs.rm(zodOutput, { recursive: true });
- await fs.mkdir(zodOutput, {
- recursive: true,
- });
+ // if (fsSync.existsSync(zodOutput)) await fs.rm(zodOutput, { recursive: true });
+ // await fs.mkdir(zodOutput, {
+ // recursive: true,
+ // });
const jobResults = (
await Promise.all(
genDocsForFiles.map(async (filepath) => ({
@@ -198,11 +196,11 @@ export { ${defines.join(", ")} };
});
// Resolve Inputs
/** @type {Record<string,string>} */
- const exportsByFile = {};
+ const fileByExport = {};
jobResults.forEach((result) => {
// these are processed intentionally in-order; the last items in jobResults will take priority over the first; polyfill will always take peak priority
result.result.defines.forEach(
- (define) => (exportsByFile[define] = result.output)
+ (define) => (fileByExport[define] = result.output)
);
});
await Promise.all(
@@ -232,9 +230,9 @@ export { ${defines.join(", ")} };
result.result.dtsOutput = `${toBeImported
.filter((v, i, a) => a.indexOf(v) === i)
.map((v) => {
- if (exportsByFile[v])
+ if (fileByExport[v])
return `import { ${v} } from ${JSON.stringify(
- "./" + path.basename(exportsByFile[v])
+ "./" + path.basename(fileByExport[v])
)}`;
console.warn("Could not find reference to", v);
return "// WARN: UNKNOWN REF: " + JSON.stringify(v);
@@ -249,5 +247,24 @@ ${result.result.dtsOutput}`;
await fs.writeFile(output, result.dtsOutput);
})
);
+ // Write the index.ts file
+ /** @type {Record<string,string[]>} */
+ const exportsByFile = {};
+ for (const [exported, file] of Object.entries(fileByExport)) {
+ exportsByFile[file] = exportsByFile[file] ?? [];
+ exportsByFile[file].push(exported);
+ }
+ await fs.writeFile(
+ path.join(tsDocOutput, "main.ts"),
+ Object.entries(exportsByFile)
+ .map(
+ ([file, exports]) =>
+ `export { ${exports.join(", ")} } from ${JSON.stringify(
+ "./" + path.basename(file) // TODO: use path.relative
+ )};`
+ )
+ .join("")
+ );
+
// TODO: call tsc on all our stuff, ensure it validates
})();