| 48 | } |
| 49 | |
| 50 | private async getConfigSourceFile(userConfigPath: string) { |
| 51 | if (fsExtra.existsSync(userConfigPath)) { |
| 52 | const sourceText = await fsExtra.readFile(userConfigPath, "utf-8"); |
| 53 | const sourceFile = ts.createSourceFile( |
| 54 | "openapi-codegen.config.ts", |
| 55 | sourceText, |
| 56 | ts.ScriptTarget.Latest |
| 57 | ); |
| 58 | |
| 59 | // Check if the config have `export default defineConfig({})` |
| 60 | let isValidConfig = false; |
| 61 | const importModules = new Set<string>(); |
| 62 | const visitor: ts.Visitor = (node) => { |
| 63 | if ( |
| 64 | ts.isExportAssignment(node) && |
| 65 | ts.isCallExpression(node.expression) && |
| 66 | ts.isIdentifier(node.expression.expression) && |
| 67 | node.expression.expression.escapedText === "defineConfig" |
| 68 | ) { |
| 69 | isValidConfig = true; |
| 70 | } |
| 71 | if (ts.isImportDeclaration(node)) { |
| 72 | importModules.add(getText(node.moduleSpecifier)); |
| 73 | } |
| 74 | |
| 75 | return node.forEachChild(visitor); |
| 76 | }; |
| 77 | |
| 78 | ts.visitNode(sourceFile, visitor); |
| 79 | |
| 80 | if (isValidConfig) { |
| 81 | return { |
| 82 | isExistingConfig: true, |
| 83 | sourceFile, |
| 84 | importModules: Array.from(importModules.values()), |
| 85 | }; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Load and return emptyConfig.ts |
| 90 | const sourceFile = ts.createSourceFile( |
| 91 | "openapi-codegen.config.ts", |
| 92 | emptyConfig, |
| 93 | ts.ScriptTarget.Latest |
| 94 | ); |
| 95 | |
| 96 | return { |
| 97 | isExistingConfig: false, |
| 98 | sourceFile, |
| 99 | importModules: ["@openapi-codegen/cli"], |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | private async askForFile(): Promise<FileOptions> { |
| 104 | return { |