Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ import { BaseOrganization, OrganizationBaseProps } from './organization';
import { BasePerson, PersonBaseProps } from './person';
import { ProviderParameters } from './provider';
import { BaseTeam, TeamBaseProps } from './team';
import { ExtractOrganizationType, ExtractPersonType, ExtractTeamType } from './type_extractors';

export type DependenciesInterface<
PersonKeyType extends string,
TeamTypeType extends string,
RoleType,
ProviderParametersType
> = { [key: string | number | symbol]: Factory<
PersonKeyType,
TeamTypeType,
RoleType,
{}, BaseOrganization<{}>,
{}, BasePerson<{}, RoleType>,
{}, BaseTeam<PersonKeyType, RoleType, {}, BasePerson<{}, RoleType>, TeamTypeType, {}>,
ProviderParametersType,
{}
>;}

export abstract class Factory<
PersonKeyType extends string,
Expand All @@ -16,7 +33,8 @@ export abstract class Factory<
PersonType extends BasePerson<PersonPropsType, RoleType>,
TeamPropsType,
TeamType extends BaseTeam<PersonKeyType, RoleType, PersonPropsType, PersonType, TeamTypeType, TeamPropsType>,
ProviderParametersType
ProviderParametersType,
DependenciesType extends DependenciesInterface<PersonKeyType, TeamTypeType, RoleType, ProviderParametersType>
> {
scope: Construct;
namespace: string;
Expand All @@ -30,22 +48,25 @@ export abstract class Factory<
abstract organizationConstructor: new (
scope: Construct,
id: string,
config: OrganizationPropsType & OrganizationBaseProps
config: OrganizationPropsType & OrganizationBaseProps,
dependencies: { [key in keyof DependenciesType]: ExtractOrganizationType<DependenciesType[key]> }
) => OrganizationType

abstract personConstructor: new (
scope: Construct,
id: string,
organization: OrganizationType,
config: PersonPropsType & PersonBaseProps<RoleType>
config: PersonPropsType & PersonBaseProps<RoleType>,
dependencies: { [key in keyof DependenciesType]: ExtractPersonType<DependenciesType[key]> }
) => PersonType

abstract teamConstructor: new (
scope: Construct,
id: string,
organization: OrganizationType,
people: Record<PersonKeyType, PersonType>,
config: TeamPropsType & TeamBaseProps<PersonKeyType, TeamTypeType>
config: TeamPropsType & TeamBaseProps<PersonKeyType, TeamTypeType>,
dependencies: { [key in keyof DependenciesType]: ExtractTeamType<DependenciesType[key]> }
) => TeamType

abstract providerParameters: ProviderParameters<keyof ProviderParametersType>
Expand All @@ -70,25 +91,39 @@ export abstract class Factory<
this.teamsConfig = teamsConfig;
}

load() {
load(dependencies: DependenciesType | undefined = undefined) {
const providerConfig = getArrayOfKeys(this.providerParameters).reduce((previous, key) => {
// type X = ProviderParametersType[typeof key];
previous[key] = new TerraformVariable(this.scope, `${this.namespace.toUpperCase()}_${key.toString().toUpperCase()}`, this.providerParameters[key]).value;
return previous;
}, Object.assign({}));
new this.providerConstructor(this.scope, `${this.namespace}-provider`, providerConfig);
this.organization = new this.organizationConstructor(this.scope, this.namespace, this.organizationConfig);
const organizationDependencies = Object.entries(dependencies ?? {}).reduce((current, [name, factory]) => {
current[name] = factory.organization;
return current;
}, Object.assign({}));
this.organization = new this.organizationConstructor(this.scope, this.namespace, this.organizationConfig, organizationDependencies);
const peopleDependencies = Object.entries(dependencies ?? {}).reduce((current, [name, factory]) => {
current[name] = factory.people;
return current;
}, Object.assign({}));
this.people = mapRecord(this.peopleConfig, personConfig => new this.personConstructor(
this.scope,
this.namespace,
this.organization!, personConfig,
peopleDependencies,
));
const teamDependencies = Object.entries(dependencies ?? {}).reduce((current, [name, factory]) => {
current[name] = factory.teams;
return current;
}, Object.assign({}));
this.teams = mapRecord(this.teamsConfig, teamConfig => new this.teamConstructor(
this.scope,
this.namespace,
this.organization!,
this.people!,
teamConfig,
teamDependencies,
));
}

Expand Down
14 changes: 7 additions & 7 deletions src/type_extractors.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import { Factory } from './factory';

export type ExtractOrganizationType<ClassType> =
ClassType extends Factory<any, any, any, any, infer ExtractedType, any, any, any, any, any>
ClassType extends Factory<any, any, any, any, infer ExtractedType, any, any, any, any, any, any>
? ExtractedType
: never
;

export type ExtractOrganizationPropsType<ClassType> =
ClassType extends Factory<any, any, any, infer ExtractedType, any, any, any, any, any, any>
ClassType extends Factory<any, any, any, infer ExtractedType, any, any, any, any, any, any, any>
? ExtractedType
: never
;

export type ExtractPersonType<ClassType> =
ClassType extends Factory<any, any, any, any, any, any, infer ExtractedType, any, any, any>
ClassType extends Factory<any, any, any, any, any, any, infer ExtractedType, any, any, any, any>
? ExtractedType
: never
;

export type ExtractPersonPropsType<ClassType> =
ClassType extends Factory<any, any, any, any, any, infer ExtractedType, any, any, any, any>
ClassType extends Factory<any, any, any, any, any, infer ExtractedType, any, any, any, any, any>
? ExtractedType
: never
;

export type ExtractPersonKeyType<ClassType> =
ClassType extends Factory<infer ExtractedType, any, any, any, any, any, any, any, any, any>
ClassType extends Factory<infer ExtractedType, any, any, any, any, any, any, any, any, any, any>
? ExtractedType
: never
;

export type ExtractTeamType<ClassType> =
ClassType extends Factory<any, any, any, any, any, any, any, any, infer ExtractedType, any>
ClassType extends Factory<any, any, any, any, any, any, any, any, infer ExtractedType, any, any>
? ExtractedType
: never
;

export type ExtractTeamPropsType<ClassType> =
ClassType extends Factory<any, any, any, any, any, any, any, infer ExtractedType, any, any>
ClassType extends Factory<any, any, any, any, any, any, any, infer ExtractedType, any, any, any>
? ExtractedType
: never
;