Skip to content

Commit

Permalink
Emit declaration file
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Jan 11, 2024
1 parent 20cbdd0 commit 9ce1925
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/nextlove/src/generators/extract-route-spec-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 9ce1925

Please sign in to comment.