@@ -39,10 +39,7 @@ export const generateCommandDecorator = (command: commander.Command): void => {
3939 await validateDefinition ( opts . project ) ;
4040 const jsonSource = await validateTemplateSource ( opts . project ) ;
4141 await validateRemoteSource ( jsonSource ) ;
42- const generatedDir = await createGenerated ( opts . project ) ;
43- const templatePath = await parseDefinitionJson ( opts . project ) ;
44- await copyTfTemplate ( templatePath , generatedDir ) ;
45- await generateSpkTfvars ( opts . project , generatedDir ) ;
42+ await generateConfig ( opts . project ) ;
4643 } catch ( err ) {
4744 logger . error (
4845 "Error occurred while generating project deployment files"
@@ -175,14 +172,82 @@ export const validateRemoteSource = async (
175172 return true ;
176173} ;
177174
175+ /**
176+ * Creates "generated" directory if it does not already exists
177+ *
178+ * @param projectPath Path to the definition.json file
179+ */
180+ export const generateConfig = async ( projectPath : string ) : Promise < void > => {
181+ try {
182+ // First, search for definition.json in current working directory
183+ const templatePath = await parseDefinitionJson ( projectPath ) ;
184+ const cwdPath = process . cwd ( ) ;
185+ if ( fs . existsSync ( path . join ( cwdPath , "definition.json" ) ) ) {
186+ // If there exists a definition.json, then read file
187+ logger . info ( `A definition.json was found in the parent directory.` ) ;
188+ const parentDefinitionJSON = await readDefinitionJson ( cwdPath ) ;
189+ const leafDefinitionJSON = await readDefinitionJson ( projectPath ) ;
190+ /* Iterate through parent and leaf JSON objects to find matches
191+ If there is a match, then replace parent key-value
192+ If there is no match between the parent and leaf,
193+ then append leaf key-value parent key-value JSON */
194+ for ( const parentKey in parentDefinitionJSON . variables ) {
195+ if ( parentKey ) {
196+ for ( const leafKey in leafDefinitionJSON . variables ) {
197+ if ( parentKey === leafKey ) {
198+ let parentVal = parentDefinitionJSON . variables [ parentKey ] ;
199+ parentVal = leafDefinitionJSON . variables [ leafKey ] ;
200+ } else {
201+ // Append to parent variables block
202+ const leafVal = leafDefinitionJSON . variables [ leafKey ] ;
203+ parentDefinitionJSON . variables [ leafKey ] = leafVal ;
204+ }
205+ }
206+ }
207+ }
208+ // Create a generated parent directory
209+ const parentDirectory = await createGenerated ( cwdPath + "-generated" ) ;
210+ // Then, create generated child directory
211+ const childDirectory = await createGenerated (
212+ path . join ( parentDirectory , projectPath )
213+ ) ;
214+ // Generate Terraform files in generated directory
215+ const spkTfvarsObject = await generateSpkTfvars (
216+ parentDefinitionJSON . variables
217+ ) ;
218+ await checkSpkTfvars ( childDirectory ) ;
219+ await writeToSpkTfvarsFile ( spkTfvarsObject , childDirectory ) ;
220+ // const templatePath = await parseDefinitionJson(projectPath);
221+ await copyTfTemplate ( templatePath , childDirectory ) ;
222+ } else {
223+ // If there is not a definition.json in current working directory,
224+ // then proceed with reading definition.json in project path
225+ // await createGenerated(projectPath)
226+ // logger.info(`A definition.json was not found in the parent directory.`)
227+ const definitionJSON = await readDefinitionJson ( projectPath ) ;
228+ // Create a generated directory
229+ const generatedDirectory = await createGenerated (
230+ projectPath + "-generated"
231+ ) ;
232+ // Generate Terraform files in generated directory
233+ const spkTfvarsObject = await generateSpkTfvars ( definitionJSON . variables ) ;
234+ await checkSpkTfvars ( generatedDirectory ) ;
235+ await writeToSpkTfvarsFile ( spkTfvarsObject , generatedDirectory ) ;
236+ await copyTfTemplate ( templatePath , generatedDirectory ) ;
237+ }
238+ } catch ( err ) {
239+ return err ;
240+ }
241+ } ;
242+
178243/**
179244 * Creates "generated" directory if it does not already exists
180245 *
181246 * @param projectPath Path to the definition.json file
182247 */
183248export const createGenerated = async ( projectPath : string ) : Promise < string > => {
184249 try {
185- const newGeneratedPath = projectPath + "-generated" ;
250+ const newGeneratedPath = projectPath ;
186251 mkdirp . sync ( newGeneratedPath ) ;
187252 logger . info ( `Created generated directory: ${ newGeneratedPath } ` ) ;
188253 return newGeneratedPath ;
@@ -210,6 +275,22 @@ export const parseDefinitionJson = async (projectPath: string) => {
210275 return templatePath ;
211276} ;
212277
278+ /**
279+ * Checks if an spk.tfvars
280+ *
281+ * @param projectPath Path to the spk.tfvars file
282+ */
283+ export const checkSpkTfvars = async ( generatedPath : string ) : Promise < void > => {
284+ try {
285+ // Remove existing spk.tfvars if it already exists
286+ if ( fs . existsSync ( path . join ( generatedPath , "spk.tfvars" ) ) ) {
287+ fs . unlinkSync ( path . join ( generatedPath , "spk.tfvars" ) ) ;
288+ }
289+ } catch ( err ) {
290+ return err ;
291+ }
292+ } ;
293+
213294/**
214295 *
215296 * Takes in the "variables" block from definition.json file and returns
@@ -224,41 +305,49 @@ export const parseDefinitionJson = async (projectPath: string) => {
224305 * key = "value"
225306 *
226307 */
227- export const generateSpkTfvars = async (
228- projectPath : string ,
229- generatedPath : string
230- ) => {
308+ export const generateSpkTfvars = async ( definitionJSON : string [ ] ) => {
231309 try {
232- // Remove existing spk.tfvars if it already exists
233- if ( fs . existsSync ( path . join ( generatedPath , "spk.tfvars" ) ) ) {
234- fs . unlinkSync ( path . join ( generatedPath , "spk.tfvars" ) ) ;
235- }
236- // Parse definition.json and extract "variables"
237- const definitionJSON = await readDefinitionJson ( projectPath ) ;
238- const variables = definitionJSON . variables ;
310+ const tfVars : string [ ] = [ ] ;
311+ // Parse definition.json "variables" block
312+ const variables = definitionJSON ;
239313 // Restructure the format of variables text
240314 const tfVariables = JSON . stringify ( variables )
241- . replace ( / \: / g, "=" )
242315 . replace ( / \{ | \} / g, "" )
243316 . replace ( / \, / g, "\n" )
244317 . split ( "\n" ) ;
245318 // (re)Create spk.tfvars
246319 tfVariables . forEach ( t => {
247- const tfVar = t . split ( "=" ) ;
248- if ( tfVar [ 0 ] . length > 0 ) {
249- tfVar [ 0 ] = tfVar [ 0 ] . replace ( / \" / g, "" ) ;
320+ const tfVar = t . split ( ":" ) ;
321+ const result = tfVar . slice ( 0 , 1 ) ;
322+ result . push ( tfVar . slice ( 1 ) . join ( ":" ) ) ;
323+ if ( result [ 0 ] . length > 0 ) {
324+ result [ 0 ] = result [ 0 ] . replace ( / \" / g, "" ) ;
250325 }
251- const newTfVar = tfVar . join ( " = " ) ;
252- fs . appendFileSync (
253- path . join ( generatedPath , "spk.tfvars" ) ,
254- newTfVar + "\n"
255- ) ;
326+ const newTfVar = result . join ( " = " ) ;
327+ tfVars . push ( newTfVar ) ;
256328 } ) ;
329+ return tfVars ;
257330 } catch ( err ) {
258- logger . error ( err ) ;
331+ logger . error ( `There was an error with generating the spk tfvars object.` ) ;
332+ return err ;
259333 }
260334} ;
261335
336+ /**
337+ * Reads in a tfVars object and returns a spk.tfvars file
338+ *
339+ * @param spkTfVars spk tfvars object in an array
340+ * @param generatedPath Path to write the spk.tfvars file to
341+ */
342+ export const writeToSpkTfvarsFile = async (
343+ spkTfVars : string [ ] ,
344+ generatedPath : string
345+ ) => {
346+ spkTfVars . forEach ( tfvar => {
347+ fs . appendFileSync ( path . join ( generatedPath , "spk.tfvars" ) , tfvar + "\n" ) ;
348+ } ) ;
349+ } ;
350+
262351/**
263352 * Reads a definition.json and returns a JSON object
264353 *
0 commit comments