@@ -8,6 +8,7 @@ import { CodifyParser } from '../parser/index.js';
88import { DependencyMap , PluginManager } from '../plugins/plugin-manager.js' ;
99import { PromptType , Reporter } from '../ui/reporters/reporter.js' ;
1010import { FileUtils } from '../utils/file.js' ;
11+ import { FileModificationCalculator , ModificationType } from '../utils/file-modification-calculator.js' ;
1112import { InitializeOrchestrator } from './initialize.js' ;
1213
1314export type RequiredParameters = Map < string , RequiredParameter [ ] > ;
@@ -73,8 +74,10 @@ export class ImportOrchestrator {
7374 ctx . processFinished ( ProcessName . IMPORT )
7475 reporter . displayImportResult ( importResult ) ;
7576
76- ImportOrchestrator . attachResourceInfo ( importResult , resourceInfoList ) ;
77- await ImportOrchestrator . saveResults ( reporter , importResult , project )
77+ const additionalResourceInfo = await pluginManager . getMultipleResourceInfo ( project . resourceConfigs . map ( ( r ) => r . type ) ) ;
78+ resourceInfoList . push ( ...additionalResourceInfo ) ;
79+
80+ await ImportOrchestrator . saveResults ( reporter , importResult , project , resourceInfoList )
7881 }
7982
8083 static async getImportedConfigs (
@@ -133,7 +136,7 @@ ${JSON.stringify(unsupportedTypeIds)}`);
133136 ctx . subprocessFinished ( SubProcessName . VALIDATE )
134137 }
135138
136- private static async saveResults ( reporter : Reporter , importResult : ImportResult , project : Project ) : Promise < void > {
139+ private static async saveResults ( reporter : Reporter , importResult : ImportResult , project : Project , resourceInfoList : ResourceInfo [ ] ) : Promise < void > {
137140 const projectExists = ! project . isEmpty ( ) ;
138141 const multipleCodifyFiles = project . codifyFiles . length > 1 ;
139142
@@ -147,25 +150,40 @@ ${JSON.stringify(unsupportedTypeIds)}`);
147150 '\nWhich file would you like to update?' ,
148151 project . codifyFiles ,
149152 )
150- await ImportOrchestrator . saveToFile ( file , importResult ) ;
153+ await ImportOrchestrator . updateExistingFile ( reporter , file , importResult , resourceInfoList ) ;
151154
152155 } else if ( promptResult . startsWith ( 'Update existing file' ) ) {
153- await ImportOrchestrator . saveToFile ( project . codifyFiles [ 0 ] , importResult ) ;
156+ await ImportOrchestrator . updateExistingFile ( reporter , project . codifyFiles [ 0 ] , importResult , resourceInfoList ) ;
154157
155158 } else if ( promptResult === 'In a new file' ) {
156159 const newFileName = await ImportOrchestrator . generateNewImportFileName ( ) ;
157- await ImportOrchestrator . saveToFile ( newFileName , importResult ) ;
160+ await ImportOrchestrator . saveNewFile ( newFileName , importResult ) ;
158161 }
159162 }
160163
161- private static async saveToFile ( filePath : string , importResult : ImportResult ) : Promise < void > {
164+ private static async updateExistingFile ( reporter : Reporter , filePath : string , importResult : ImportResult , resourceInfoList : ResourceInfo [ ] ) : Promise < void > {
162165 const existing = await CodifyParser . parse ( filePath ) ;
163-
164- for ( const resource of importResult . result ) {
165- existing . addOrReplace ( resource ) ;
166+ ImportOrchestrator . attachResourceInfo ( importResult . result , resourceInfoList ) ;
167+ ImportOrchestrator . attachResourceInfo ( existing . resourceConfigs , resourceInfoList ) ;
168+
169+ const modificationCalculator = new FileModificationCalculator ( existing ) ;
170+ const result = modificationCalculator . calculate ( importResult . result . map ( ( resource ) => ( {
171+ modification : ModificationType . INSERT_OR_UPDATE ,
172+ resource
173+ } ) ) ) ;
174+
175+ reporter . displayFileModification ( result . diff ) ;
176+ const shouldSave = await reporter . promptConfirmation ( `Save to file (${ filePath } )?` ) ;
177+ if ( ! shouldSave ) {
178+ process . exit ( 0 ) ;
166179 }
167180
168- console . log ( JSON . stringify ( existing . resourceConfigs . map ( ( r ) => r . raw ) , null , 2 ) )
181+ await FileUtils . writeFile ( filePath , result . newFile ) ;
182+ }
183+
184+ private static async saveNewFile ( filePath : string , importResult : ImportResult ) : Promise < void > {
185+ const newFile = JSON . stringify ( importResult , null , 2 ) ;
186+ await FileUtils . writeFile ( filePath , newFile ) ;
169187 }
170188
171189 private static async generateNewImportFileName ( ) : Promise < string > {
@@ -185,9 +203,13 @@ ${JSON.stringify(unsupportedTypeIds)}`);
185203 }
186204
187205 // 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 ) => {
206+ private static attachResourceInfo ( resources : ResourceConfig [ ] , resourceInfoList : ResourceInfo [ ] ) : void {
207+ resources . forEach ( ( resource ) => {
190208 const matchedInfo = resourceInfoList . find ( ( info ) => info . type === resource . type ) ! ;
209+ if ( ! matchedInfo ) {
210+ throw new Error ( `Could not find type ${ resource . type } in the resource info` ) ;
211+ }
212+
191213 resource . attachResourceInfo ( matchedInfo ) ;
192214 } )
193215 }
0 commit comments