Skip to content

Commit

Permalink
fix: render template from string
Browse files Browse the repository at this point in the history
  • Loading branch information
joni3k committed Aug 18, 2022
1 parent 77c4df7 commit 7012768
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 43 deletions.
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 7012768

Please sign in to comment.