|
| 1 | +import path from 'node:path'; |
| 2 | + |
1 | 3 | import { Project } from '../entities/project.js'; |
2 | 4 | import { ResourceConfig } from '../entities/resource-config.js'; |
3 | 5 | import { ResourceInfo } from '../entities/resource-info.js'; |
4 | 6 | import { ProcessName, SubProcessName, ctx } from '../events/context.js'; |
5 | 7 | import { CodifyParser } from '../parser/index.js'; |
6 | 8 | import { DependencyMap, PluginManager } from '../plugins/plugin-manager.js'; |
7 | 9 | import { PromptType, Reporter } from '../ui/reporters/reporter.js'; |
| 10 | +import { FileUtils } from '../utils/file.js'; |
8 | 11 | import { InitializeOrchestrator } from './initialize.js'; |
9 | 12 |
|
10 | 13 | export type RequiredParameters = Map<string, RequiredParameter[]>; |
@@ -70,6 +73,7 @@ export class ImportOrchestrator { |
70 | 73 | ctx.processFinished(ProcessName.IMPORT) |
71 | 74 | reporter.displayImportResult(importResult); |
72 | 75 |
|
| 76 | + ImportOrchestrator.attachResourceInfo(importResult, resourceInfoList); |
73 | 77 | await ImportOrchestrator.saveResults(reporter, importResult, project) |
74 | 78 | } |
75 | 79 |
|
@@ -131,16 +135,60 @@ ${JSON.stringify(unsupportedTypeIds)}`); |
131 | 135 |
|
132 | 136 | private static async saveResults(reporter: Reporter, importResult: ImportResult, project: Project): Promise<void> { |
133 | 137 | const projectExists = !project.isEmpty(); |
| 138 | + const multipleCodifyFiles = project.codifyFiles.length > 1; |
134 | 139 |
|
135 | | - const continueSaving = await reporter.promptOptions( |
| 140 | + const promptResult = await reporter.promptOptions( |
136 | 141 | '\nDo you want to save the results?', |
137 | | - [projectExists ? `Update ${project.path}` : undefined, 'Save in a new file', 'No'].filter(Boolean) as string[] |
| 142 | + [projectExists ? multipleCodifyFiles ? 'Update existing file (multiple found)' : `Update existing file (${project.codifyFiles})` : undefined, 'In a new file', 'No'].filter(Boolean) as string[] |
138 | 143 | ) |
139 | | - if (!continueSaving) { |
140 | | - process.exit(0) |
| 144 | + |
| 145 | + if (promptResult === 'Update existing file (multiple found)') { |
| 146 | + const file = await reporter.promptOptions( |
| 147 | + '\nWhich file would you like to update?', |
| 148 | + project.codifyFiles, |
| 149 | + ) |
| 150 | + await ImportOrchestrator.saveToFile(file, importResult); |
| 151 | + |
| 152 | + } else if (promptResult.startsWith('Update existing file')) { |
| 153 | + await ImportOrchestrator.saveToFile(project.codifyFiles[0], importResult); |
| 154 | + |
| 155 | + } else if (promptResult === 'In a new file') { |
| 156 | + const newFileName = await ImportOrchestrator.generateNewImportFileName(); |
| 157 | + await ImportOrchestrator.saveToFile(newFileName, importResult); |
141 | 158 | } |
| 159 | + } |
| 160 | + |
| 161 | + private static async saveToFile(filePath: string, importResult: ImportResult): Promise<void> { |
| 162 | + const existing = await CodifyParser.parse(filePath); |
142 | 163 |
|
| 164 | + for (const resource of importResult.result) { |
| 165 | + existing.addOrReplace(resource); |
| 166 | + } |
143 | 167 |
|
| 168 | + console.log(JSON.stringify(existing.resourceConfigs.map((r) => r.raw), null, 2)) |
| 169 | + } |
| 170 | + |
| 171 | + private static async generateNewImportFileName(): Promise<string> { |
| 172 | + const cwd = process.cwd(); |
| 173 | + |
| 174 | + let fileName = path.join(cwd, 'import.codify.json') |
| 175 | + let counter = 1; |
| 176 | + |
| 177 | + while(true) { |
| 178 | + if (!(await FileUtils.fileExists(fileName))) { |
| 179 | + return fileName; |
| 180 | + } |
| 181 | + |
| 182 | + fileName = path.join(cwd, `import-${counter}.codify.json`); |
| 183 | + counter++; |
| 184 | + } |
| 185 | + } |
144 | 186 |
|
| 187 | + // We have to attach additional info to the imported configs to make saving easier |
| 188 | + private static attachResourceInfo(importResult: ImportResult, resourceInfoList: ResourceInfo[]): void { |
| 189 | + importResult.result.forEach((resource) => { |
| 190 | + const matchedInfo = resourceInfoList.find((info) => info.type === resource.type)!; |
| 191 | + resource.attachResourceInfo(matchedInfo); |
| 192 | + }) |
145 | 193 | } |
146 | 194 | } |
0 commit comments