From ba041ade6bec201db267e47542beec76e9b934a6 Mon Sep 17 00:00:00 2001 From: Fuxing Loh <4266087+fuxingloh@users.noreply.github.com> Date: Fri, 21 Jul 2023 15:49:16 +0800 Subject: [PATCH] chore(contented-processor): generate basic `index.d.ts` for `${type}Index` (#524) #### What this PR does / why we need it: Generate basic types for ${type}Index for better DX when working with `contented build`. --- packages/contented-processor/src/ContentedCodegen.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/contented-processor/src/ContentedCodegen.ts b/packages/contented-processor/src/ContentedCodegen.ts index e0ad9430..c0febc76 100644 --- a/packages/contented-processor/src/ContentedCodegen.ts +++ b/packages/contented-processor/src/ContentedCodegen.ts @@ -12,9 +12,9 @@ export class ContentedCodegen { async generateIndex() { const types = this.config.pipelines.map((pipeline) => pipeline.type); const ast = generateIndexAST([...new Set(types)]); - const outPath = join(this.outPath, `index.js`); await fs.mkdir(this.outPath, { recursive: true }); - await fs.writeFile(outPath, generate(ast).code); + await fs.writeFile(join(this.outPath, `index.js`), generate(ast).code); + await fs.writeFile(join(this.outPath, `index.d.ts`), generateTypes(types)); } async generatePipeline(type: string, contents: FileIndex[]) { @@ -204,3 +204,7 @@ function generate(ast: any) { const gen = new CodeGenerator(ast); return gen.generate(); } + +function generateTypes(types: string[]): string { + return types.map((type) => `export declare const ${type}Index: FileIndex[];`).join('\n'); +}