@ttoss/cloudformation 0.10.9 → 0.10.10

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.
@@ -0,0 +1,194 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+
3
+ // src/findAndReadCloudFormationTemplate.ts
4
+ import * as fs2 from "node:fs";
5
+ import * as path2 from "node:path";
6
+
7
+ // src/readCloudFormationYamlTemplate.ts
8
+ import * as fs from "fs";
9
+ import * as path from "path";
10
+
11
+ // src/cloudFormationYamlTemplate.ts
12
+ import yaml from "js-yaml";
13
+ var cloudFormationTypes = [{
14
+ tag: "!Equals",
15
+ options: {
16
+ kind: "sequence",
17
+ construct: data => {
18
+ return {
19
+ "Fn::Equals": data
20
+ };
21
+ }
22
+ }
23
+ }, {
24
+ tag: "!FindInMap",
25
+ options: {
26
+ kind: "sequence",
27
+ construct: data => {
28
+ return {
29
+ "Fn::FindInMap": data
30
+ };
31
+ }
32
+ }
33
+ }, {
34
+ tag: "!GetAtt",
35
+ options: {
36
+ kind: "scalar",
37
+ construct: data => {
38
+ return {
39
+ "Fn::GetAtt": data.split(".")
40
+ };
41
+ }
42
+ }
43
+ }, {
44
+ tag: "!GetAtt",
45
+ options: {
46
+ kind: "sequence",
47
+ construct: data => {
48
+ return {
49
+ "Fn::GetAtt": data
50
+ };
51
+ }
52
+ }
53
+ }, {
54
+ tag: "!If",
55
+ options: {
56
+ kind: "sequence",
57
+ construct: data => {
58
+ return {
59
+ "Fn::If": data
60
+ };
61
+ }
62
+ }
63
+ }, {
64
+ tag: "!ImportValue",
65
+ options: {
66
+ kind: "scalar",
67
+ construct: data => {
68
+ return {
69
+ "Fn::ImportValue": data
70
+ };
71
+ }
72
+ }
73
+ }, {
74
+ tag: "!Join",
75
+ options: {
76
+ kind: "sequence",
77
+ construct: data => {
78
+ return {
79
+ "Fn::Join": data
80
+ };
81
+ }
82
+ }
83
+ }, {
84
+ tag: "!Not",
85
+ options: {
86
+ kind: "sequence",
87
+ construct: data => {
88
+ return {
89
+ "Fn::Not": data
90
+ };
91
+ }
92
+ }
93
+ }, {
94
+ tag: "!Ref",
95
+ options: {
96
+ kind: "scalar",
97
+ construct: data => {
98
+ return {
99
+ Ref: data
100
+ };
101
+ }
102
+ }
103
+ }, {
104
+ tag: "!Sub",
105
+ options: {
106
+ kind: "scalar",
107
+ construct: data => {
108
+ return {
109
+ "Fn::Sub": data
110
+ };
111
+ }
112
+ }
113
+ }, {
114
+ tag: "!Sub",
115
+ options: {
116
+ kind: "sequence",
117
+ construct: data => {
118
+ return {
119
+ "Fn::Sub": data
120
+ };
121
+ }
122
+ }
123
+ }];
124
+ var getYamlTypes = tagAndTypeArr => {
125
+ return tagAndTypeArr.map(({
126
+ tag,
127
+ options
128
+ }) => {
129
+ return new yaml.Type(tag, options);
130
+ });
131
+ };
132
+ var getSchema = (tagAndTypeArr = []) => {
133
+ return yaml.DEFAULT_SCHEMA.extend(getYamlTypes([...tagAndTypeArr, ...cloudFormationTypes]));
134
+ };
135
+ var loadCloudFormationTemplate = (template, tagAndTypeArr = []) => {
136
+ return yaml.load(template, {
137
+ schema: getSchema(tagAndTypeArr)
138
+ });
139
+ };
140
+
141
+ // src/readCloudFormationYamlTemplate.ts
142
+ var getTypes = () => {
143
+ return [{
144
+ tag: `!SubString`,
145
+ options: {
146
+ kind: "scalar",
147
+ construct: filePath => {
148
+ return fs.readFileSync(path.resolve(process.cwd(), filePath)).toString();
149
+ }
150
+ }
151
+ }];
152
+ };
153
+ var readCloudFormationYamlTemplate = ({
154
+ templatePath
155
+ }) => {
156
+ const template = fs.readFileSync(templatePath).toString();
157
+ const parsed = loadCloudFormationTemplate(template, getTypes());
158
+ if (!parsed || typeof parsed === "string") {
159
+ throw new Error("Cannot parse CloudFormation template.");
160
+ }
161
+ return parsed;
162
+ };
163
+
164
+ // src/findAndReadCloudFormationTemplate.ts
165
+ import { readConfigFile } from "@ttoss/read-config-file";
166
+ var defaultTemplatePaths = ["ts", "js", "yaml", "yml", "json"].map(extension => {
167
+ return `./src/cloudformation.${extension}`;
168
+ });
169
+ var findAndReadCloudFormationTemplate = async ({
170
+ templatePath: defaultTemplatePath,
171
+ options = {}
172
+ }) => {
173
+ const templatePath = defaultTemplatePath || defaultTemplatePaths.reduce((acc, cur) => {
174
+ if (acc) {
175
+ return acc;
176
+ }
177
+ return fs2.existsSync(path2.resolve(process.cwd(), cur)) ? cur : acc;
178
+ }, "");
179
+ if (!templatePath) {
180
+ throw new Error("Cannot find a CloudFormation template.");
181
+ }
182
+ const extension = templatePath?.split(".").pop();
183
+ if (["yaml", "yml"].includes(extension)) {
184
+ return readCloudFormationYamlTemplate({
185
+ templatePath
186
+ });
187
+ }
188
+ const configFilePath = path2.resolve(process.cwd(), templatePath);
189
+ return readConfigFile({
190
+ configFilePath,
191
+ options
192
+ });
193
+ };
194
+ export { findAndReadCloudFormationTemplate };
@@ -0,0 +1,84 @@
1
+ type Parameter = {
2
+ AllowedValues?: string[];
3
+ Default?: string | number;
4
+ Description?: string;
5
+ Type: string;
6
+ NoEcho?: boolean;
7
+ };
8
+ type Parameters = {
9
+ [key: string]: Parameter;
10
+ };
11
+ type Resource = {
12
+ Type: string;
13
+ DeletionPolicy?: 'Delete' | 'Retain';
14
+ Description?: string;
15
+ DependsOn?: string[] | string;
16
+ Condition?: string;
17
+ Properties: any;
18
+ };
19
+ type Policy = {
20
+ PolicyName: string;
21
+ PolicyDocument: {
22
+ Version: '2012-10-17';
23
+ Statement: {
24
+ Sid?: string;
25
+ Effect: 'Allow' | 'Deny';
26
+ Action: string | string[];
27
+ Resource: string | string[] | {
28
+ [key: string]: any;
29
+ } | {
30
+ [key: string]: any;
31
+ }[];
32
+ }[];
33
+ };
34
+ };
35
+ type IAMRoleResource = Resource & {
36
+ Type: 'AWS::IAM::Role';
37
+ Properties: {
38
+ AssumeRolePolicyDocument: {
39
+ Version: '2012-10-17';
40
+ Statement: {
41
+ Effect: 'Allow' | 'Deny';
42
+ Action: string;
43
+ Principal: any;
44
+ Condition?: {
45
+ [key: string]: any;
46
+ };
47
+ }[];
48
+ };
49
+ ManagedPolicyArns?: string[];
50
+ Path?: string;
51
+ Policies?: Policy[];
52
+ };
53
+ };
54
+ type Resources = {
55
+ [key: string]: IAMRoleResource | Resource;
56
+ };
57
+ type Output = {
58
+ Description?: string;
59
+ Value: string | any;
60
+ Export?: {
61
+ Name: string | any;
62
+ };
63
+ };
64
+ type Outputs = {
65
+ [key: string]: Output;
66
+ };
67
+ type CloudFormationTemplate = {
68
+ AWSTemplateFormatVersion: '2010-09-09';
69
+ Metadata?: any;
70
+ Description?: string;
71
+ Transform?: 'AWS::Serverless-2016-10-31';
72
+ Mappings?: any;
73
+ Conditions?: any;
74
+ Parameters?: Parameters;
75
+ Resources: Resources;
76
+ Outputs?: Outputs;
77
+ };
78
+
79
+ declare const findAndReadCloudFormationTemplate: ({ templatePath: defaultTemplatePath, options, }: {
80
+ templatePath?: string;
81
+ options?: unknown;
82
+ }) => Promise<CloudFormationTemplate>;
83
+
84
+ export { type CloudFormationTemplate, type IAMRoleResource, type Output, type Outputs, type Parameter, type Parameters, type Policy, type Resource, type Resources, findAndReadCloudFormationTemplate };
@@ -0,0 +1,84 @@
1
+ type Parameter = {
2
+ AllowedValues?: string[];
3
+ Default?: string | number;
4
+ Description?: string;
5
+ Type: string;
6
+ NoEcho?: boolean;
7
+ };
8
+ type Parameters = {
9
+ [key: string]: Parameter;
10
+ };
11
+ type Resource = {
12
+ Type: string;
13
+ DeletionPolicy?: 'Delete' | 'Retain';
14
+ Description?: string;
15
+ DependsOn?: string[] | string;
16
+ Condition?: string;
17
+ Properties: any;
18
+ };
19
+ type Policy = {
20
+ PolicyName: string;
21
+ PolicyDocument: {
22
+ Version: '2012-10-17';
23
+ Statement: {
24
+ Sid?: string;
25
+ Effect: 'Allow' | 'Deny';
26
+ Action: string | string[];
27
+ Resource: string | string[] | {
28
+ [key: string]: any;
29
+ } | {
30
+ [key: string]: any;
31
+ }[];
32
+ }[];
33
+ };
34
+ };
35
+ type IAMRoleResource = Resource & {
36
+ Type: 'AWS::IAM::Role';
37
+ Properties: {
38
+ AssumeRolePolicyDocument: {
39
+ Version: '2012-10-17';
40
+ Statement: {
41
+ Effect: 'Allow' | 'Deny';
42
+ Action: string;
43
+ Principal: any;
44
+ Condition?: {
45
+ [key: string]: any;
46
+ };
47
+ }[];
48
+ };
49
+ ManagedPolicyArns?: string[];
50
+ Path?: string;
51
+ Policies?: Policy[];
52
+ };
53
+ };
54
+ type Resources = {
55
+ [key: string]: IAMRoleResource | Resource;
56
+ };
57
+ type Output = {
58
+ Description?: string;
59
+ Value: string | any;
60
+ Export?: {
61
+ Name: string | any;
62
+ };
63
+ };
64
+ type Outputs = {
65
+ [key: string]: Output;
66
+ };
67
+ type CloudFormationTemplate = {
68
+ AWSTemplateFormatVersion: '2010-09-09';
69
+ Metadata?: any;
70
+ Description?: string;
71
+ Transform?: 'AWS::Serverless-2016-10-31';
72
+ Mappings?: any;
73
+ Conditions?: any;
74
+ Parameters?: Parameters;
75
+ Resources: Resources;
76
+ Outputs?: Outputs;
77
+ };
78
+
79
+ declare const findAndReadCloudFormationTemplate: ({ templatePath: defaultTemplatePath, options, }: {
80
+ templatePath?: string;
81
+ options?: unknown;
82
+ }) => Promise<CloudFormationTemplate>;
83
+
84
+ export { type CloudFormationTemplate, type IAMRoleResource, type Output, type Outputs, type Parameter, type Parameters, type Policy, type Resource, type Resources, findAndReadCloudFormationTemplate };
package/dist/index.js ADDED
@@ -0,0 +1,239 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+ "use strict";
3
+
4
+ var __create = Object.create;
5
+ var __defProp = Object.defineProperty;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __export = (target, all) => {
11
+ for (var name in all) __defProp(target, name, {
12
+ get: all[name],
13
+ enumerable: true
14
+ });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
19
+ get: () => from[key],
20
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
21
+ });
22
+ }
23
+ return to;
24
+ };
25
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
26
+ // If the importer is in node compatibility mode or this is not an ESM
27
+ // file that has been converted to a CommonJS file using a Babel-
28
+ // compatible transform (i.e. "__esModule" has not been set), then set
29
+ // "default" to the CommonJS "module.exports" for node compatibility.
30
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
31
+ value: mod,
32
+ enumerable: true
33
+ }) : target, mod));
34
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
35
+ value: true
36
+ }), mod);
37
+
38
+ // src/index.ts
39
+ var src_exports = {};
40
+ __export(src_exports, {
41
+ findAndReadCloudFormationTemplate: () => findAndReadCloudFormationTemplate
42
+ });
43
+ module.exports = __toCommonJS(src_exports);
44
+
45
+ // src/findAndReadCloudFormationTemplate.ts
46
+ var fs2 = __toESM(require("fs"), 1);
47
+ var path2 = __toESM(require("path"), 1);
48
+
49
+ // src/readCloudFormationYamlTemplate.ts
50
+ var fs = __toESM(require("fs"), 1);
51
+ var path = __toESM(require("path"), 1);
52
+
53
+ // src/cloudFormationYamlTemplate.ts
54
+ var import_js_yaml = __toESM(require("js-yaml"), 1);
55
+ var cloudFormationTypes = [{
56
+ tag: "!Equals",
57
+ options: {
58
+ kind: "sequence",
59
+ construct: data => {
60
+ return {
61
+ "Fn::Equals": data
62
+ };
63
+ }
64
+ }
65
+ }, {
66
+ tag: "!FindInMap",
67
+ options: {
68
+ kind: "sequence",
69
+ construct: data => {
70
+ return {
71
+ "Fn::FindInMap": data
72
+ };
73
+ }
74
+ }
75
+ }, {
76
+ tag: "!GetAtt",
77
+ options: {
78
+ kind: "scalar",
79
+ construct: data => {
80
+ return {
81
+ "Fn::GetAtt": data.split(".")
82
+ };
83
+ }
84
+ }
85
+ }, {
86
+ tag: "!GetAtt",
87
+ options: {
88
+ kind: "sequence",
89
+ construct: data => {
90
+ return {
91
+ "Fn::GetAtt": data
92
+ };
93
+ }
94
+ }
95
+ }, {
96
+ tag: "!If",
97
+ options: {
98
+ kind: "sequence",
99
+ construct: data => {
100
+ return {
101
+ "Fn::If": data
102
+ };
103
+ }
104
+ }
105
+ }, {
106
+ tag: "!ImportValue",
107
+ options: {
108
+ kind: "scalar",
109
+ construct: data => {
110
+ return {
111
+ "Fn::ImportValue": data
112
+ };
113
+ }
114
+ }
115
+ }, {
116
+ tag: "!Join",
117
+ options: {
118
+ kind: "sequence",
119
+ construct: data => {
120
+ return {
121
+ "Fn::Join": data
122
+ };
123
+ }
124
+ }
125
+ }, {
126
+ tag: "!Not",
127
+ options: {
128
+ kind: "sequence",
129
+ construct: data => {
130
+ return {
131
+ "Fn::Not": data
132
+ };
133
+ }
134
+ }
135
+ }, {
136
+ tag: "!Ref",
137
+ options: {
138
+ kind: "scalar",
139
+ construct: data => {
140
+ return {
141
+ Ref: data
142
+ };
143
+ }
144
+ }
145
+ }, {
146
+ tag: "!Sub",
147
+ options: {
148
+ kind: "scalar",
149
+ construct: data => {
150
+ return {
151
+ "Fn::Sub": data
152
+ };
153
+ }
154
+ }
155
+ }, {
156
+ tag: "!Sub",
157
+ options: {
158
+ kind: "sequence",
159
+ construct: data => {
160
+ return {
161
+ "Fn::Sub": data
162
+ };
163
+ }
164
+ }
165
+ }];
166
+ var getYamlTypes = tagAndTypeArr => {
167
+ return tagAndTypeArr.map(({
168
+ tag,
169
+ options
170
+ }) => {
171
+ return new import_js_yaml.default.Type(tag, options);
172
+ });
173
+ };
174
+ var getSchema = (tagAndTypeArr = []) => {
175
+ return import_js_yaml.default.DEFAULT_SCHEMA.extend(getYamlTypes([...tagAndTypeArr, ...cloudFormationTypes]));
176
+ };
177
+ var loadCloudFormationTemplate = (template, tagAndTypeArr = []) => {
178
+ return import_js_yaml.default.load(template, {
179
+ schema: getSchema(tagAndTypeArr)
180
+ });
181
+ };
182
+
183
+ // src/readCloudFormationYamlTemplate.ts
184
+ var getTypes = () => {
185
+ return [{
186
+ tag: `!SubString`,
187
+ options: {
188
+ kind: "scalar",
189
+ construct: filePath => {
190
+ return fs.readFileSync(path.resolve(process.cwd(), filePath)).toString();
191
+ }
192
+ }
193
+ }];
194
+ };
195
+ var readCloudFormationYamlTemplate = ({
196
+ templatePath
197
+ }) => {
198
+ const template = fs.readFileSync(templatePath).toString();
199
+ const parsed = loadCloudFormationTemplate(template, getTypes());
200
+ if (!parsed || typeof parsed === "string") {
201
+ throw new Error("Cannot parse CloudFormation template.");
202
+ }
203
+ return parsed;
204
+ };
205
+
206
+ // src/findAndReadCloudFormationTemplate.ts
207
+ var import_read_config_file = require("@ttoss/read-config-file");
208
+ var defaultTemplatePaths = ["ts", "js", "yaml", "yml", "json"].map(extension => {
209
+ return `./src/cloudformation.${extension}`;
210
+ });
211
+ var findAndReadCloudFormationTemplate = async ({
212
+ templatePath: defaultTemplatePath,
213
+ options = {}
214
+ }) => {
215
+ const templatePath = defaultTemplatePath || defaultTemplatePaths.reduce((acc, cur) => {
216
+ if (acc) {
217
+ return acc;
218
+ }
219
+ return fs2.existsSync(path2.resolve(process.cwd(), cur)) ? cur : acc;
220
+ }, "");
221
+ if (!templatePath) {
222
+ throw new Error("Cannot find a CloudFormation template.");
223
+ }
224
+ const extension = templatePath?.split(".").pop();
225
+ if (["yaml", "yml"].includes(extension)) {
226
+ return readCloudFormationYamlTemplate({
227
+ templatePath
228
+ });
229
+ }
230
+ const configFilePath = path2.resolve(process.cwd(), templatePath);
231
+ return (0, import_read_config_file.readConfigFile)({
232
+ configFilePath,
233
+ options
234
+ });
235
+ };
236
+ // Annotate the CommonJS export names for ESM import in node:
237
+ 0 && (module.exports = {
238
+ findAndReadCloudFormationTemplate
239
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/cloudformation",
3
- "version": "0.10.9",
3
+ "version": "0.10.10",
4
4
  "description": "CloudFormation utils.",
5
5
  "author": "ttoss",
6
6
  "contributors": [
@@ -24,7 +24,7 @@
24
24
  ],
25
25
  "dependencies": {
26
26
  "js-yaml": "^4.1.0",
27
- "@ttoss/read-config-file": "^2.0.2"
27
+ "@ttoss/read-config-file": "^2.0.3"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/jest": "^29.5.13",
@@ -32,8 +32,8 @@
32
32
  "@types/node": "^20.16.1",
33
33
  "jest": "^29.7.0",
34
34
  "tsup": "^8.3.0",
35
- "@ttoss/config": "^1.33.0",
36
- "@ttoss/test-utils": "^2.1.15"
35
+ "@ttoss/test-utils": "^2.1.16",
36
+ "@ttoss/config": "^1.34.0"
37
37
  },
38
38
  "keywords": [],
39
39
  "publishConfig": {