From 9ce192566d7e5b0658017e60f7058b1ba898d4b3 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Thu, 11 Jan 2024 14:58:59 -0800 Subject: [PATCH] Emit declaration file --- .../generators/extract-route-spec-schemas.ts | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/nextlove/src/generators/extract-route-spec-schemas.ts b/packages/nextlove/src/generators/extract-route-spec-schemas.ts index f905ca5f7..104d67ac6 100644 --- a/packages/nextlove/src/generators/extract-route-spec-schemas.ts +++ b/packages/nextlove/src/generators/extract-route-spec-schemas.ts @@ -28,7 +28,7 @@ export const extractRouteSpecs = async (opts: GenerateRouteTypesOpts) => { const { packageDir, pathGlob = "/pages/api/**/*.ts" } = opts const fullPathGlob = path.posix.join(packageDir, pathGlob) - const project = new Project() + const project = new Project({ compilerOptions: { declaration: true } }) const paths: string[] = [] for (const sourceFile of project.addSourceFilesAtPaths(fullPathGlob)) { @@ -106,6 +106,25 @@ export const extractRouteSpecs = async (opts: GenerateRouteTypesOpts) => { } ` + // Generate types (.d.ts) + const entryPoint = project.createSourceFile( + "extracted-route-specs.ts", + entryPointContent + ) + const emitResult = entryPoint.getEmitOutput({ emitOnlyDtsFiles: true }) + if (opts.outputFile) { + const declarationFilePath = path.join( + path.dirname(opts.outputFile), + path.basename(opts.outputFile).replace(".mjs", "").replace(".js", "") + + ".d.ts" + ) + await fs.writeFile( + declarationFilePath, + emitResult.getOutputFiles()[0].getText() + ) + } + + // Generate values (.js) const pkgRaw = await fs.readFile( path.join(packageDir, "package.json"), "utf-8"