Skip to content

Commit

Permalink
feat: add support for region params and id
Browse files Browse the repository at this point in the history
  • Loading branch information
ohoareau committed Sep 2, 2024
1 parent de664a1 commit c286e8b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ export type layer_config = {
envs?: Record<string, unknown>;
};

export type layer_region_config = {
id?: string;
[key: string]: unknown;
};
export type enriched_layer_config = {
defaultRegion: string;
regions?: string[];
regions?: Record<string, layer_region_config>;
};

export type loggable = {
Expand Down
44 changes: 28 additions & 16 deletions src/utils/generateEnvLayerFromFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {existsSync, mkdirSync, readFileSync, writeFileSync} from 'fs';
import replaceVars from './replaceVars';
import {enriched_layer_config} from '../types';
import {enriched_layer_config, layer_region_config} from '../types';

export const generateEnvLayerFromFile = async (
sourceFile: string,
Expand All @@ -11,27 +11,39 @@ export const generateEnvLayerFromFile = async (
existsSync(targetDir) || mkdirSync(targetDir, {recursive: true});

const defaultRegion = layerConfig.defaultRegion;
const regions: string[] = layerConfig.regions || [];
const regions: layer_region_config = layerConfig.regions || {};

const mappedRegions = (regions || [defaultRegion]).map(
r =>
const mappedRegions = Object.entries(regions || {[defaultRegion]: {}}).map(
([rCode, r]) =>
[
r,
`${targetDir}/main${r === defaultRegion ? '' : `_${r.replace(/-/g, '_')}`}.tf`,
] as [string, string],
`${targetDir}/main${(r?.id || rCode) === defaultRegion ? '' : `_${rCode.replace(/-/g, '_')}`}.tf`,
rCode,
] as [layer_region_config, 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,
);
}),
mappedRegions.map(
async ([r, targetFile, rCode]: [
layer_region_config,
string,
string,
]) => {
writeFileSync(
targetFile,
replaceVars(readFileSync(sourceFile, 'utf8') as string, {
...vars,
region: r?.id || rCode,
rsuffix:
(r?.id || rCode) === defaultRegion
? ''
: `-${rCode}`,
...r,
...(vars?.id ? {id: vars.id} : {}),
}) as unknown as string,
);
},
),
);

const errors: {reason: Error}[] = reports.filter(
Expand Down

0 comments on commit c286e8b

Please sign in to comment.