Skip to content

Commit

Permalink
feat: v1 anv v2 directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ohoareau committed Sep 2, 2024
1 parent b1d50fc commit d94973c
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 44 deletions.
2 changes: 2 additions & 0 deletions src/generators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {default as v1} from './v1';
export {default as v2} from './v2';
30 changes: 30 additions & 0 deletions src/generators/v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {enriched_layer_config, layer_region_config} from '../types';
import replaceVars from '../utils/replaceVars';

export default async (
source: string,
{
regions,
defaultRegion,
}: {regions: Record<string, layer_region_config>; defaultRegion: string},
vars: Record<string, unknown>,
_: enriched_layer_config,
) => {
return Object.entries(regions).map(
([rCode, r]: [string, layer_region_config]) => {
const isMain = (r?.id || rCode) === defaultRegion;
return [
`main${(r?.id || rCode) === defaultRegion ? '' : `_${rCode.replace(/-/g, '_')}`}.tf`,
replaceVars(source, {
...vars,
region: r?.id || rCode,
is_main: isMain,
is_default_region: isMain,
rsuffix: isMain ? '' : `-${rCode}`,
...r,
...(vars?.id ? {id: vars.id} : {}),
}) as unknown as string,
];
},
) as [string, string][];
};
66 changes: 66 additions & 0 deletions src/generators/v2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {enriched_layer_config, layer_region_config} from '../types';
import replaceVars from '../utils/replaceVars';

export default async (
source: string,
{
regions,
defaultRegion,
}: {regions: Record<string, layer_region_config>; defaultRegion: string},
vars: Record<string, unknown>,
_: enriched_layer_config,
) => {
return [
// terraform file
[
`terraform.tf`,
replaceVars(source, {
...vars,
is_terraform_file: true,
}) as unknown as string,
],
// providers file
[
`providers.tf`,
replaceVars(source, {
...vars,
is_providers_file: true,
}) as unknown as string,
],
// data file
[
`data.tf`,
replaceVars(source, {
...vars,
is_data_file: true,
}) as unknown as string,
],
// outputs file
[
`outputs.tf`,
replaceVars(source, {
...vars,
is_outputs_file: true,
}) as unknown as string,
],
// region files
...Object.entries(regions).map(
([rCode, r]: [string, layer_region_config]) => {
const isMain = (r?.id || rCode) === defaultRegion;
return [
`main${(r?.id || rCode) === defaultRegion ? '' : `_${rCode.replace(/-/g, '_')}`}.tf`,
replaceVars(source, {
...vars,
region: r?.id || rCode,
is_main: isMain,
is_main_file: true,
is_default_region: isMain,
rsuffix: isMain ? '' : `-${rCode}`,
...r,
...(vars?.id ? {id: vars.id} : {}),
}) as unknown as string,
];
},
),
] as [string, string][];
};
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ export type layer_config = {
envs?: Record<string, unknown>;
};

export type generated_file = [string, string];
export type generator = (
source: string,
ctx: {regions: Record<string, layer_region_config>; defaultRegion: string},
vars: Record<string, unknown>,
layerConfig: enriched_layer_config,
) => Promise<generated_file[]> | generated_file[];
export type layer_region_config = {
id?: string;
[key: string]: unknown;
};
export type enriched_layer_config = {
defaultRegion: string;
regions?: Record<string, layer_region_config>;
format?: string;
};

export type loggable = {
Expand Down
66 changes: 22 additions & 44 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, layer_region_config} from '../types';
import {enriched_layer_config, generator, layer_region_config} from '../types';
import * as generators from '../generators';

export const generateEnvLayerFromFile = async (
sourceFile: string,
Expand All @@ -13,52 +13,30 @@ export const generateEnvLayerFromFile = async (
const defaultRegion = layerConfig.defaultRegion;
const regions: Record<string, layer_region_config> =
layerConfig.regions || {};
const format = layerConfig.format || 'v1';
const generator = (generators as Record<string, generator>)[format];

const mappedRegions = Object.entries(
regions && Object.keys(regions).length
? regions
: ({[defaultRegion]: {}} as Record<string, layer_region_config>),
).map(
([rCode, r]: [string, layer_region_config]) =>
[
r,
`${targetDir}/main${(r?.id || rCode) === defaultRegion ? '' : `_${rCode.replace(/-/g, '_')}`}.tf`,
rCode,
] as [layer_region_config, string, string],
);
if (!generator) throw new Error(`Unsupported layer format '${format}'`);

const reports = await Promise.allSettled(
mappedRegions.map(
async ([r, targetFile, rCode]: [
layer_region_config,
string,
string,
]) => {
const isMain = (r?.id || rCode) === defaultRegion;
writeFileSync(
targetFile,
replaceVars(readFileSync(sourceFile, 'utf8') as string, {
...vars,
region: r?.id || rCode,
is_main: isMain,
is_default_region: isMain,
rsuffix: isMain ? '' : `-${rCode}`,
...r,
...(vars?.id ? {id: vars.id} : {}),
}) as unknown as string,
);
},
),
const files = await generator(
readFileSync(sourceFile, 'utf8') as string,
{
regions:
regions && Object.keys(regions).length
? regions
: ({[defaultRegion]: {}} as Record<
string,
layer_region_config
>),
defaultRegion,
},
vars,
layerConfig,
);

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')}`,
);
(files || []).map(([file, content]) =>
writeFileSync(`${targetDir}/${file}`, content),
);
};

export default generateEnvLayerFromFile;

0 comments on commit d94973c

Please sign in to comment.