Skip to content

Commit

Permalink
chore: strip away lodash dep
Browse files Browse the repository at this point in the history
  • Loading branch information
pomykaczi committed Mar 20, 2024
1 parent 853bd61 commit 3b42348
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 33 deletions.
9 changes: 2 additions & 7 deletions src/project/account-type.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Construct } from 'constructs';
import { findKey } from 'lodash';
import { EnvRegExp } from './envregexp';
import { Account } from './interfaces';
import { addError } from '../error';
import { findAccountTypeByEnvironment } from '../utils/find';

/**
* Internal class to handle set/get operations for Account Type
Expand Down Expand Up @@ -35,11 +34,7 @@ export class AccountType {
environmentType: string,
): string {

const accountType = findKey(accounts, (account) =>
account.environments?.filter((environment) =>
new EnvRegExp(environment).test(environmentType),
),
);
const accountType = findAccountTypeByEnvironment(accounts, environmentType);

if (typeof accountType !== 'string') {
addError(scope,
Expand Down
2 changes: 1 addition & 1 deletion src/project/project-context.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Construct } from 'constructs';
import { get } from 'lodash';
import { AccountType } from './account-type';
import { EnvironmentType } from './environment-type';
import { Account } from './interfaces';
import { Project } from './project';
import { addError } from '../error';
import { get } from '../utils/get';

export class ProjectContext {
/**
Expand Down
4 changes: 2 additions & 2 deletions src/smartstack/name/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { renderTemplate, TemplateContext } from '../../template';
import { isSet } from '../../utils/isSet';
import { isNonEmptyString } from '../../utils/isSet';

const template = `{#
Template for Stack Name prop
Expand Down Expand Up @@ -54,7 +54,7 @@ export interface NameProps {
export function formatName(props: NameProps): string {

// allow end-user override
if (isSet(props.override)) return props.override!;
if (isNonEmptyString(props.override)) return props.override!;

// otherwise user our recommended format
return renderTemplate(template, <TemplateProps>{
Expand Down
6 changes: 3 additions & 3 deletions src/smartstack/tags/checks.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Construct } from 'constructs';
import { Values } from './values';
import { isSet } from '../../utils/isSet';
import { isNonEmptyString } from '../../utils/isSet';

export function hasAccount(values: Values): boolean {
return isSet(values.accountType);
return isNonEmptyString(values.accountType);
}

export function hasEnvironment(values: Values): boolean {
return isSet(values.environmentType);
return isNonEmptyString(values.environmentType);
}

export function useLegacyTags(scope: Construct): boolean {
Expand Down
8 changes: 4 additions & 4 deletions src/smartstack/tags/taggers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Tags } from 'aws-cdk-lib';
import { capitalCase, pascalCase } from 'change-case';
import { Construct } from 'constructs';
import { isSet } from 'lodash';
import { hasAccount, hasEnvironment, useLegacyTags } from './checks';
import { tagKey, Values } from './values';
import { isNonEmptyString } from '../../utils/isSet';

interface Tagger {
(scope: Construct, tags: Tags, values: Values): void;
Expand Down Expand Up @@ -41,13 +41,13 @@ export const tagAuthorName: Tagger = (_: Construct, tags: Tags, values: Values)
};

export const tagAuthorOrganization: Tagger = (_: Construct, tags: Tags, values: Values) => {
if (isSet(values.authorOrganization)) {
if (isNonEmptyString(values.authorOrganization)) {
tags.add(tagKey.AUTHOR_ORGANIZATION, values.authorOrganization);
}
};

export const tagAuthorEmail: Tagger = (_: Construct, tags: Tags, values: Values) => {
if (isSet(values.authorEmail)) {
if (isNonEmptyString(values.authorEmail)) {
tags.add(tagKey.AUTHOR_EMAIL, values.authorEmail);
}
};
};
6 changes: 3 additions & 3 deletions src/smartstack/termination/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { isSet } from '../../utils/isSet';
import { isNonEmptyString } from '../../utils/isSet';

export interface TerminationProtectionProps {
override?: boolean;
environmentType?: string;
}

function isEnvironmental(environmentType?: string): boolean {
return isSet(environmentType);
return isNonEmptyString(environmentType);
}

function isStagingProduction(environmentType?: string): boolean {
Expand All @@ -23,4 +23,4 @@ export function decideTerminationProtection(props: TerminationProtectionProps):

// finally decide based on if staging/production or other
return isStagingProduction(props.environmentType);
}
}
13 changes: 13 additions & 0 deletions src/utils/find.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Account, EnvRegExp } from '../project';

export function findAccountTypeByEnvironment(obj: Record<string, Account>, predicate: string): string | undefined {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const list = obj[key].environments?.filter((environment) => new EnvRegExp(environment).test(predicate));
if (Array.isArray(list) && list.length > 0) {
return key;
}
}
}
return undefined;
}
14 changes: 14 additions & 0 deletions src/utils/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

export function get(obj: Record<string, any> | undefined, path: string, defValue?: any): any {
// If path is not defined or it has false value
if (!path) return undefined;
// Check if path is string or array. Regex : ensure that we do not have '.' and brackets.
// Regex explained: https://regexr.com/58j0k
const pathArray = path.match(/([^[.\]])+/g);
// Find value
const result = pathArray?.reduce((prevObj, key) => {
return prevObj && prevObj[key];
}, obj);
// If found value is undefined return default value; otherwise return the value
return result === undefined ? defValue : result;
}
8 changes: 3 additions & 5 deletions src/utils/isSet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { values } from 'lodash';


export function isSet(value?: string | undefined): boolean {
return (typeof value === 'string' && values.length > 0);
}
export function isNonEmptyString(value?: unknown): value is string {
return (typeof value === 'string' && value.length > 0);
}
74 changes: 66 additions & 8 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Match } from 'aws-cdk-lib/assertions';
import { generateTestApp } from './helpers/app';
import { sortTagsByKey, tagsAsDictionary, TagValue } from './helpers/tags';
import { AccountStrategy } from '../src';
import { AccountStrategy, PC } from '../src';


describe('Integration', () => {
Expand All @@ -20,12 +20,32 @@ describe('Integration', () => {
id: '111111111111',
config: {
baseDomain: 'example.net',
sizing: {
staging: {
cpu: 256,
memory: 512,
},
},
flags: [
'foo',
'bar',
],
},
},
prod: {
id: '222222222222',
config: {
baseDomain: 'example.com',
sizing: {
production: {
cpu: 1024,
memory: 4096,
},
},
flags: [
'foo',
'bar',
],
},
},
}),
Expand Down Expand Up @@ -65,7 +85,7 @@ describe('Integration', () => {
},
],
TableName: 'DevelopmentMyTable',
Tags: expectedTags,
Tags: Match.arrayWith(expectedTags),
}),
);

Expand Down Expand Up @@ -106,7 +126,7 @@ describe('Integration', () => {
'AWS::Route53::HostedZone',
Match.objectLike({
Name: 'example.net.',
HostedZoneTags: expectedTags,
HostedZoneTags: Match.arrayWith(expectedTags),
}),
);
});
Expand Down Expand Up @@ -145,7 +165,7 @@ describe('Integration', () => {
},
],
TableName: 'FeatureAbc123MyTable',
Tags: expectedTags,
Tags: Match.arrayWith(expectedTags),
}),
);

Expand Down Expand Up @@ -186,7 +206,7 @@ describe('Integration', () => {
'AWS::Route53::HostedZone',
Match.objectLike({
Name: 'example.net.',
HostedZoneTags: expectedTags,
HostedZoneTags: Match.arrayWith(expectedTags),
}),
);
});
Expand Down Expand Up @@ -225,7 +245,7 @@ describe('Integration', () => {
},
],
TableName: 'ProductionMyTable',
Tags: expectedTags,
Tags: Match.arrayWith(expectedTags),
}),
);

Expand All @@ -241,7 +261,7 @@ describe('Integration', () => {
'AWS::S3::Bucket',
Match.objectLike({
BucketName: 'acme-corp-my-cool-project-production-my-bucket',
Tags: expectedTags,
Tags: Match.arrayWith(expectedTags),
}),
);

Expand All @@ -260,11 +280,49 @@ describe('Integration', () => {
'AWS::Route53::HostedZone',
Match.objectLike({
Name: 'example.com.',
HostedZoneTags: expectedTags,
HostedZoneTags: Match.arrayWith(expectedTags),
}),
);
});

test('dev/config', () => {

const {
stack,
} = generateTestApp({
...props,
context: {
account: 'dev',
environment: 'development',
},
});

expect(PC.getAccountConfig(stack, 'baseCamp', 'no camping')).toBe('no camping');
expect(PC.getAccountConfig(stack, 'baseDomain')).toBe('example.net');
expect(PC.getAccountConfig(stack, 'sizing.staging.cpu')).toBe(256);
expect(PC.getAccountConfig(stack, 'sizing.test.cpu', 256)).toBe(256);
expect(PC.getAccountConfig(stack, 'flags[0]')).toBe('foo');
});

test('prod/config', () => {

const {
stack,
} = generateTestApp({
...props,
context: {
account: 'prod',
environment: 'production',
},
});

expect(PC.getAccountConfig(stack, 'baseCamp', 'no camping')).toBe('no camping');
expect(PC.getAccountConfig(stack, 'baseDomain')).toBe('example.com');
expect(PC.getAccountConfig(stack, 'sizing.production.cpu')).toBe(1024);
expect(PC.getAccountConfig(stack, 'sizing.preproduction.cpu', 512)).toBe(512);
expect(PC.getAccountConfig(stack, 'flags[0]')).toBe('foo');
});

});

});

0 comments on commit 3b42348

Please sign in to comment.