1- import { ProcessName , ctx } from '../events/context.js' ;
1+ import chalk from 'chalk' ;
2+ import path from 'node:path' ;
3+
4+ import { ResourceConfig } from '../entities/resource-config.js' ;
5+ import { ProcessName , SubProcessName , ctx } from '../events/context.js' ;
26import { Reporter } from '../ui/reporters/reporter.js' ;
7+ import { FileUtils } from '../utils/file.js' ;
8+ import { resolvePathWithVariables , untildify } from '../utils/index.js' ;
39import { PluginInitOrchestrator } from './initialize-plugins.js' ;
410
511export const InitializeOrchestrator = {
@@ -13,6 +19,7 @@ export const InitializeOrchestrator = {
1319
1420 const { pluginManager, typeIdsToDependenciesMap } = await PluginInitOrchestrator . run ( { } , reporter ) ;
1521
22+ ctx . subprocessStarted ( SubProcessName . IMPORT_RESOURCE )
1623 const importResults = await Promise . all ( [ ...typeIdsToDependenciesMap . keys ( ) ] . map ( async ( typeId ) => {
1724 try {
1825 return await pluginManager . importResource ( {
@@ -23,15 +30,62 @@ export const InitializeOrchestrator = {
2330 return null ;
2431 }
2532 } ) )
33+ ctx . subprocessFinished ( SubProcessName . IMPORT_RESOURCE )
2634
2735 const flattenedResults = importResults . filter ( Boolean ) . flatMap ( p => p ?. result ) . filter ( Boolean )
2836
2937 const userSelectedTypes = await reporter . promptInitResultSelection ( [ ...new Set ( flattenedResults . map ( ( r ) => r ! . core . type ) ) ] )
38+ ctx . log ( 'Resource types were chosen to be imported.' )
3039
31- ctx . processFinished ( ProcessName . INIT ) ;
40+ const locationToSave = await this . promptSaveLocation ( reporter ) ;
41+ ctx . log ( `Save results to ${ locationToSave } ` )
42+ await reporter . hide ( ) ;
43+
44+ const resourcesRaw = flattenedResults . filter ( ( r ) => userSelectedTypes . includes ( r . core . type ) )
45+ . map ( ( r ) => ResourceConfig . fromJson ( r ! ) )
46+ . map ( ( r ) => r . raw ) ;
47+
48+ await FileUtils . writeFile ( locationToSave , JSON . stringify ( resourcesRaw , null , 2 ) ) ;
49+ ctx . log ( 'File successfully saved' ) ;
3250
33- console . log ( JSON . stringify ( flattenedResults , null , 2 ) ) ;
51+ await reporter . displayMessage ( `
52+ 🎉🎉 Codify successfully initialized. 🎉🎉
53+ The imported configs were written to: ${ locationToSave }
54+
55+ Use ${ chalk . bgMagenta . bold ( ' codify plan ' ) } to compute changes and ${ chalk . bgMagenta . bold ( ' codify apply ' ) } to apply them.
56+ For more information visit: https://docs.codifycli.com.
57+
58+ Enjoy!
59+ ` )
60+
61+ ctx . processFinished ( ProcessName . INIT ) ;
3462 } ,
3563
64+ async promptSaveLocation ( reporter : Reporter ) : Promise < string > {
65+ let locationToSave = '' ;
66+ let input = '' ;
67+ let isValidSaveLocation = false ;
68+ let error = false ;
69+
70+ while ( ! isValidSaveLocation ) {
71+ input = ( await reporter . promptInput (
72+ `Where to save the new Codify configs? ${ chalk . grey . dim ( '(leave blank for ~/codify.json)' ) } ` ,
73+ error ? `Invalid location: ${ input } already exists` : undefined )
74+ )
75+ input = input ? input : '~/codify.json' ;
76+
77+ locationToSave = path . resolve ( untildify ( resolvePathWithVariables ( input ) ) ) ;
78+
79+ try {
80+ isValidSaveLocation = ! ( await FileUtils . fileExists ( locationToSave ) ) ;
81+ error = ! isValidSaveLocation ;
82+ } catch {
83+ isValidSaveLocation = false ;
84+ error = true ;
85+ }
86+ }
87+
88+ return locationToSave ;
89+ }
3690
3791} ;
0 commit comments