-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
135 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {resolve} from 'path'; | ||
import {fetch_config} from '../types'; | ||
|
||
export const fetchConfig: fetch_config = async (configFile: string) => { | ||
return import(resolve(configFile)); | ||
}; | ||
|
||
export default fetchConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {readdirSync} from 'fs'; | ||
import {fetched_layer} from '../types'; | ||
|
||
export const fetchLayers = async ( | ||
sourceDir: string, | ||
): Promise<fetched_layer[]> => { | ||
return readdirSync(sourceDir, {withFileTypes: true}) | ||
.filter(e => !e.isDirectory() && /.tmpl.tf$/.test(e.name)) | ||
.map(e => ({ | ||
name: e.name.replace(/\.tmpl\.tf$/, ''), | ||
file: e.name, | ||
filePath: `${sourceDir}/${e.name}`, | ||
})); | ||
}; | ||
|
||
export default fetchLayers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,47 @@ | ||
import {dirname} from 'path'; | ||
import {existsSync, mkdirSync, readFileSync, writeFileSync} from 'fs'; | ||
import replaceVars from './replaceVars'; | ||
import {enriched_layer_config} from '../types'; | ||
|
||
export const generateEnvLayerFromFile = async ( | ||
sourceFile: string, | ||
targetFile: string, | ||
targetDir: string, | ||
vars: Record<string, unknown>, | ||
layerConfig: enriched_layer_config, | ||
) => { | ||
const parentDir = dirname(targetFile); | ||
existsSync(parentDir) || mkdirSync(parentDir, {recursive: true}); | ||
writeFileSync( | ||
targetFile, | ||
replaceVars( | ||
readFileSync(sourceFile, 'utf8') as string, | ||
vars, | ||
) as unknown as string, | ||
existsSync(targetDir) || mkdirSync(targetDir, {recursive: true}); | ||
|
||
const defaultRegion = layerConfig.defaultRegion; | ||
const regions: string[] = layerConfig.regions || []; | ||
|
||
const mappedRegions = (regions || [defaultRegion]).map( | ||
r => | ||
[ | ||
r, | ||
`${targetDir}/main${r === defaultRegion ? '' : `_${r.replace(/-/g, '_')}`}.tf`, | ||
] as [string, string], | ||
); | ||
|
||
const reports = await Promise.allSettled( | ||
mappedRegions.map(async ([r, targetFile]: [string, string]) => { | ||
writeFileSync( | ||
targetFile, | ||
replaceVars(readFileSync(sourceFile, 'utf8') as string, { | ||
...vars, | ||
region: r, | ||
rsuffix: r === defaultRegion ? '' : `-${r}`, | ||
}) as unknown as string, | ||
); | ||
}), | ||
); | ||
|
||
const errors: {reason: Error}[] = reports.filter( | ||
x => x.status !== 'fulfilled', | ||
) as unknown as {reason: Error}[]; | ||
|
||
if (errors.length) | ||
throw new Error( | ||
`Unable to generate all env layer files for ${sourceFile}: ${errors.map((x: {reason: Error}) => x.reason.message).join('\n')}`, | ||
); | ||
}; | ||
|
||
export default generateEnvLayerFromFile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import mergeEnvConfig from './mergeEnvConfig'; | ||
import generateEnvLayerFromFile from './generateEnvLayerFromFile'; | ||
import generateLayerVars from './generateLayerVars'; | ||
import {fetched_layer, config, enriched_layer_config} from '../types'; | ||
|
||
const defaultRegion = 'eu-west-3'; | ||
|
||
export const generateEnvLayers = async ( | ||
layers: fetched_layer[], | ||
config: config, | ||
targetDir: string, | ||
defaultLayerConfig: Record<string, unknown> = {}, | ||
) => { | ||
return Promise.all( | ||
Object.keys(config.environments || {}).map(async (name: string) => { | ||
const env = mergeEnvConfig(config, name); | ||
await Promise.all( | ||
layers.map(async ({name: layer, file, filePath}) => { | ||
const layerEnv = {...env, env: name, layer: layer}; | ||
const layerConfig = | ||
(config && config.layers && config.layers[layer]) || {}; | ||
if (layerConfig) { | ||
if ( | ||
layerConfig.only_on_envs && | ||
!layerConfig.only_on_envs.includes(name) | ||
) | ||
return; | ||
if ( | ||
layerConfig.not_on_envs && | ||
layerConfig.not_on_envs.includes(name) | ||
) | ||
return; | ||
} | ||
await generateEnvLayerFromFile( | ||
filePath, | ||
`${targetDir}/${name}/${file.replace(/\.tmpl\.tf$/, '')}`, | ||
generateLayerVars(layerEnv, layerEnv), | ||
{ | ||
defaultRegion, | ||
...defaultLayerConfig, | ||
...(layerConfig?.['envs']?.['common'] || {}), | ||
...(layerConfig?.['envs']?.[name] || {}), | ||
} as enriched_layer_config, | ||
); | ||
}), | ||
); | ||
}), | ||
); | ||
}; | ||
|
||
export default generateEnvLayers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters