@ttoss/cloudformation 0.10.9 → 0.10.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,151 +0,0 @@
1
- import { CloudFormationTemplate } from './CloudFormationTemplate';
2
- import yaml from 'js-yaml';
3
-
4
- export interface TagAndType {
5
- tag: string;
6
- options: yaml.TypeConstructorOptions;
7
- }
8
-
9
- const cloudFormationTypes: TagAndType[] = [
10
- {
11
- tag: '!Equals',
12
- options: {
13
- kind: 'sequence',
14
- construct: (data: any) => {
15
- return { 'Fn::Equals': data };
16
- },
17
- },
18
- },
19
- {
20
- tag: '!FindInMap',
21
- options: {
22
- kind: 'sequence',
23
- construct: (data: any) => {
24
- return { 'Fn::FindInMap': data };
25
- },
26
- },
27
- },
28
- {
29
- tag: '!GetAtt',
30
- options: {
31
- kind: 'scalar',
32
- construct: (data: any) => {
33
- return { 'Fn::GetAtt': data.split('.') };
34
- },
35
- },
36
- },
37
- {
38
- tag: '!GetAtt',
39
- options: {
40
- kind: 'sequence',
41
- construct: (data: any) => {
42
- return { 'Fn::GetAtt': data };
43
- },
44
- },
45
- },
46
- {
47
- tag: '!If',
48
- options: {
49
- kind: 'sequence',
50
- construct: (data: any) => {
51
- return { 'Fn::If': data };
52
- },
53
- },
54
- },
55
- {
56
- tag: '!ImportValue',
57
- options: {
58
- kind: 'scalar',
59
- construct: (data: any) => {
60
- return { 'Fn::ImportValue': data };
61
- },
62
- },
63
- },
64
- {
65
- tag: '!Join',
66
- options: {
67
- kind: 'sequence',
68
- construct: (data: any) => {
69
- return { 'Fn::Join': data };
70
- },
71
- },
72
- },
73
- {
74
- tag: '!Not',
75
- options: {
76
- kind: 'sequence',
77
- construct: (data: any) => {
78
- return { 'Fn::Not': data };
79
- },
80
- },
81
- },
82
- {
83
- tag: '!Ref',
84
- options: {
85
- kind: 'scalar',
86
- construct: (data: any) => {
87
- return { Ref: data };
88
- },
89
- },
90
- },
91
- {
92
- tag: '!Sub',
93
- options: {
94
- kind: 'scalar',
95
- construct: (data: any) => {
96
- return { 'Fn::Sub': data };
97
- },
98
- },
99
- },
100
- {
101
- tag: '!Sub',
102
- options: {
103
- kind: 'sequence',
104
- construct: (data: any) => {
105
- return { 'Fn::Sub': data };
106
- },
107
- },
108
- },
109
- ];
110
-
111
- const getYamlTypes = (tagAndTypeArr: TagAndType[]) => {
112
- return tagAndTypeArr.map(({ tag, options }) => {
113
- return new yaml.Type(tag, options);
114
- });
115
- };
116
-
117
- /**
118
- * Transform CloudFormation directives in objects. For example, transform
119
- * !Ref Something in { Ref: Something }.
120
- */
121
- export const getSchema = (tagAndTypeArr: TagAndType[] = []) => {
122
- return yaml.DEFAULT_SCHEMA.extend(
123
- getYamlTypes([...tagAndTypeArr, ...cloudFormationTypes])
124
- );
125
- };
126
-
127
- /**
128
- * Transform a JSON in a YAML string.
129
- *
130
- * @param cloudFormationTemplate JSON CloudFormation template
131
- * @returns YAML as string
132
- */
133
- export const dumpToYamlCloudFormationTemplate = (
134
- cloudFormationTemplate: CloudFormationTemplate
135
- ) => {
136
- return yaml.dump(cloudFormationTemplate, { schema: getSchema() });
137
- };
138
-
139
- /**
140
- * Transform YAML string in JSON object.
141
- *
142
- * @param template template in String format.
143
- * @param tagAndTypeArr YAML types.
144
- * @returns JSON template.
145
- */
146
- export const loadCloudFormationTemplate = (
147
- template: string,
148
- tagAndTypeArr: TagAndType[] = []
149
- ) => {
150
- return yaml.load(template, { schema: getSchema(tagAndTypeArr) });
151
- };
@@ -1,54 +0,0 @@
1
- import * as fs from 'node:fs';
2
- import * as path from 'node:path';
3
- import { type CloudFormationTemplate } from './CloudFormationTemplate';
4
- import { readCloudFormationYamlTemplate } from './readCloudFormationYamlTemplate';
5
- import { readConfigFile } from '@ttoss/read-config-file';
6
-
7
- export const defaultTemplatePaths = ['ts', 'js', 'yaml', 'yml', 'json'].map(
8
- (extension) => {
9
- return `./src/cloudformation.${extension}`;
10
- }
11
- );
12
-
13
- export const findAndReadCloudFormationTemplate = async ({
14
- templatePath: defaultTemplatePath,
15
- options = {},
16
- }: {
17
- templatePath?: string;
18
- options?: unknown;
19
- }): Promise<CloudFormationTemplate> => {
20
- const templatePath =
21
- defaultTemplatePath ||
22
- defaultTemplatePaths
23
- /**
24
- * Iterate over extensions. If the template of the current extension is
25
- * found, we save it on the accumulator and return it every time until
26
- * the loop ends.
27
- */
28
- .reduce((acc, cur) => {
29
- if (acc) {
30
- return acc;
31
- }
32
-
33
- return fs.existsSync(path.resolve(process.cwd(), cur)) ? cur : acc;
34
- }, '');
35
-
36
- if (!templatePath) {
37
- throw new Error('Cannot find a CloudFormation template.');
38
- }
39
-
40
- const extension = templatePath?.split('.').pop() as string;
41
-
42
- /**
43
- * We need to read Yaml first because CloudFormation specific tags aren't
44
- * recognized when parsing a simple Yaml file. I.e., a possible error:
45
- * "Error message: "unknown tag !<!Ref> at line 21, column 34:\n"
46
- */
47
- if (['yaml', 'yml'].includes(extension)) {
48
- return readCloudFormationYamlTemplate({ templatePath });
49
- }
50
-
51
- const configFilePath = path.resolve(process.cwd(), templatePath);
52
-
53
- return readConfigFile({ configFilePath, options });
54
- };
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export { findAndReadCloudFormationTemplate } from './findAndReadCloudFormationTemplate';
2
- export * from './CloudFormationTemplate';
@@ -1,43 +0,0 @@
1
- import * as fs from 'fs';
2
- import * as path from 'path';
3
- import { CloudFormationTemplate } from './CloudFormationTemplate';
4
- import {
5
- TagAndType,
6
- loadCloudFormationTemplate,
7
- } from './cloudFormationYamlTemplate';
8
-
9
- const getTypes = (): TagAndType[] => {
10
- return [
11
- {
12
- tag: `!SubString`,
13
- options: {
14
- kind: 'scalar',
15
- construct: (filePath: string) => {
16
- return fs
17
- .readFileSync(path.resolve(process.cwd(), filePath))
18
- .toString();
19
- },
20
- },
21
- },
22
- ];
23
- };
24
-
25
- /**
26
- * CloudFormation
27
- * @param param0
28
- */
29
- export const readCloudFormationYamlTemplate = ({
30
- templatePath,
31
- }: {
32
- templatePath: string;
33
- }): CloudFormationTemplate => {
34
- const template = fs.readFileSync(templatePath).toString();
35
-
36
- const parsed = loadCloudFormationTemplate(template, getTypes());
37
-
38
- if (!parsed || typeof parsed === 'string') {
39
- throw new Error('Cannot parse CloudFormation template.');
40
- }
41
-
42
- return parsed as CloudFormationTemplate;
43
- };