Skip to content

Commit

Permalink
Merge pull request #1 from alma-cdk/render-template-from-string
Browse files Browse the repository at this point in the history
fix: render template from string instead
  • Loading branch information
aripalo authored Aug 19, 2022
2 parents b457cf6 + 7012768 commit 3788541
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 67 deletions.
21 changes: 0 additions & 21 deletions src/smartstack/description/description.njk

This file was deleted.

26 changes: 23 additions & 3 deletions src/smartstack/description/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import * as path from 'path';
import { loadTemplate, renderTemplate, TemplateContext } from '../../template';
const template = loadTemplate(path.join(__dirname, 'description.njk'));
import { renderTemplate, TemplateContext } from '../../template';

const template = `{#
Template for Stack Description prop
--------------------------------------------------------------------------------
AWS CloudFormation stack descriptions have a maximum length of 1024 bytes.
But since we're opinionated, a stack summary should fit into a tweet.
#}{#
#}{% if environment | notEmpty %}{#
"Staging Environment: "
#}{{ environment | pascal | stripUnderscore | truncate(32, true, "") | append(" Environment: ") }}{#
#}{% elif account | notEmpty %}{#
"Dev Account: "
#}{{ account | pascal | truncate(32, true, "") | append(" Account: ") }}{#
#}{% endif %}{#
"My description goes here"
If truncated, an ellipsis is appended:
https://mozilla.github.io/nunjucks/templating.html#truncate
#}{{ body | truncate(280, true)}}`;

interface TemplateProps extends TemplateContext {
body: string;
Expand Down
39 changes: 36 additions & 3 deletions src/smartstack/name/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
import * as path from 'path';
import { loadTemplate, renderTemplate, TemplateContext } from '../../template';
import { renderTemplate, TemplateContext } from '../../template';
import { isSet } from '../../utils/isSet';
const template = loadTemplate(path.join(__dirname, 'name.njk'));

const template = `{#
Template for Stack Name prop
--------------------------------------------------------------------------------
AWS CloudFormation stack names have a maximum length of 128 characters.
This template truncates various values if they exceed their limit:
Max Lenghts:
- No Environment or Account present:
32+1+48 = 81
- Environment present:
32+1+32+13+48 = 126
- Account present:
32+1+32+9+48 = 122
#}{#
"MyProject-"
#}{{ projectName | pascal | truncate(32, true, "") | append("-") }}{#
#}{% if environment | notEmpty %}{#
"Staging-Environment-"
#}{{ environment | pascal | stripUnderscore | truncate(32, true, "") | append("-Environment-") }}{#
#}{% elif account | notEmpty %}{#
"Dev-Account-"
#}{{ account | pascal| truncate(32, true, "") | append("-Account-") }}{#
#}{% endif %}{#
"MyStack"
#}{{ stackId | pascal | truncate(48, true, "")}}`;

interface TemplateProps extends TemplateContext {
stackId: string;
Expand Down
34 changes: 0 additions & 34 deletions src/smartstack/name/name.njk

This file was deleted.

6 changes: 0 additions & 6 deletions src/template/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { readFileSync } from 'fs';
import { pascalCase } from 'change-case';
import { Environment } from 'nunjucks';

Expand All @@ -20,11 +19,6 @@ env.addFilter('append', function(str: string, trailer: string) {
return `${str}${trailer}`;
});


export function loadTemplate(path: string): string {
return readFileSync(path, 'utf-8').trim();
}

type TemplateContextValue = string | undefined;
export type TemplateContext = Record<string, TemplateContextValue>;

Expand Down

0 comments on commit 3788541

Please sign in to comment.