|
1 | 1 | // biome-ignore lint/style/useNodejsImportProtocol: <explanation>
|
2 |
| -import fs from "fs" |
3 |
| -// biome-ignore lint/style/useNodejsImportProtocol: <explanation> |
4 | 2 | import path from "path"
|
5 |
| -import {Project} from "ts-morph" |
| 3 | +import {Project, type SourceFile} from "ts-morph" |
| 4 | +import type {IFsAdaptor} from "../../../core/file-system/fs-adaptor" |
6 | 5 | import type {CompilerOptions} from "../../../core/loaders/tsconfig.loader"
|
7 | 6 | import {isTruthy} from "../../../core/utils"
|
8 | 7 | import type {OpenapiTypescriptGeneratorConfig} from "../../../templates.types"
|
@@ -41,6 +40,7 @@ export async function generateTypescriptNextJS(
|
41 | 40 | const appDirectory = [".", "app", subDirectory]
|
42 | 41 | .filter(isTruthy)
|
43 | 42 | .join(path.sep)
|
| 43 | + |
44 | 44 | const generatedDirectory = [".", "generated", subDirectory]
|
45 | 45 | .filter(isTruthy)
|
46 | 46 | .join(path.sep)
|
@@ -85,23 +85,12 @@ export async function generateTypescriptNextJS(
|
85 | 85 | routeToNextJSFilepath(group.name),
|
86 | 86 | )
|
87 | 87 |
|
88 |
| - const existing = fs.existsSync( |
89 |
| - path.join(emitter.config.destinationDirectory, nextJsAppRouterPath), |
90 |
| - ) |
91 |
| - ? fs |
92 |
| - .readFileSync( |
93 |
| - path.join( |
94 |
| - emitter.config.destinationDirectory, |
95 |
| - nextJsAppRouterPath, |
96 |
| - ), |
97 |
| - "utf-8", |
98 |
| - ) |
99 |
| - .toString() |
100 |
| - : "" |
101 |
| - const sourceFile = project.createSourceFile( |
| 88 | + const sourceFile = await loadExistingRouteImplementation({ |
| 89 | + fsAdaptor: config.fsAdaptor, |
| 90 | + project, |
| 91 | + destinationDirectory: emitter.config.destinationDirectory, |
102 | 92 | nextJsAppRouterPath,
|
103 |
| - existing, |
104 |
| - ) |
| 93 | + }) |
105 | 94 |
|
106 | 95 | const nextJSAppRouterBuilder = new TypescriptNextjsAppRouterBuilder(
|
107 | 96 | nextJsAppRouterPath,
|
@@ -158,6 +147,30 @@ export async function generateTypescriptNextJS(
|
158 | 147 | ])
|
159 | 148 | }
|
160 | 149 |
|
| 150 | +async function loadExistingRouteImplementation({ |
| 151 | + fsAdaptor, |
| 152 | + project, |
| 153 | + destinationDirectory, |
| 154 | + nextJsAppRouterPath, |
| 155 | +}: { |
| 156 | + fsAdaptor: IFsAdaptor |
| 157 | + project: Project |
| 158 | + destinationDirectory: string |
| 159 | + nextJsAppRouterPath: string |
| 160 | +}): Promise<SourceFile> { |
| 161 | + const exists = await fsAdaptor.exists( |
| 162 | + path.join(destinationDirectory, nextJsAppRouterPath), |
| 163 | + ) |
| 164 | + |
| 165 | + const source = exists |
| 166 | + ? await fsAdaptor.readFile( |
| 167 | + path.join(destinationDirectory, nextJsAppRouterPath), |
| 168 | + ) |
| 169 | + : "" |
| 170 | + |
| 171 | + return project.createSourceFile(nextJsAppRouterPath, source) |
| 172 | +} |
| 173 | + |
161 | 174 | function routeToNextJSFilepath(route: string): string {
|
162 | 175 | const parts = route
|
163 | 176 | .split("/")
|
|
0 commit comments