@resistdesign/voltra 3.0.0-alpha.28 → 3.0.0-alpha.29

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,258 @@
1
+ import YAML from 'yaml';
2
+
3
+ // src/iac/utils/patch-utils.ts
4
+ var DEFAULT_MERGE_STRATEGY = "transpose";
5
+ var getValuePathString = (valuePathArray = []) => valuePathArray.map((p) => encodeURIComponent(p)).join("/");
6
+ var getValuePathArray = (valuePathString = "") => valuePathString.split("/").map((p) => decodeURIComponent(p));
7
+ var isConstructedFrom = (value, constructorReference) => value !== null && typeof value === "object" && "constructor" in value && value.constructor === constructorReference;
8
+ var mergeValues = (valuePathArray = [], existingValue, newValue, mergeStrategyMap = {}) => {
9
+ const valuePathString = getValuePathString(valuePathArray);
10
+ const arrayIndexWildcardValuePathString = getValuePathString(
11
+ valuePathArray.map((p) => typeof p === "number" ? "#" : p)
12
+ );
13
+ const {
14
+ [valuePathString]: {
15
+ strategy: specificKeyMergeStrategy = DEFAULT_MERGE_STRATEGY,
16
+ data: specificKeyMergeStrategyData = void 0
17
+ } = {},
18
+ [arrayIndexWildcardValuePathString]: {
19
+ strategy: arrayIndexWildcardMergeStrategy = DEFAULT_MERGE_STRATEGY,
20
+ data: arrayIndexWildcardMergeStrategyData = void 0
21
+ } = {}
22
+ } = mergeStrategyMap;
23
+ const mergeStrategy = valuePathString in mergeStrategyMap ? specificKeyMergeStrategy : arrayIndexWildcardMergeStrategy;
24
+ const mergeStrategyData = valuePathString in mergeStrategyMap ? specificKeyMergeStrategyData : arrayIndexWildcardMergeStrategyData;
25
+ let mergedValue = typeof newValue !== "undefined" ? newValue : existingValue;
26
+ if (mergeStrategy !== "replace") {
27
+ if (isConstructedFrom(existingValue, Array) && isConstructedFrom(newValue, Array)) {
28
+ if (mergeStrategy === "accumulate") {
29
+ mergedValue = [...existingValue, ...newValue];
30
+ } else if (mergeStrategy === "accumulate-unique") {
31
+ mergedValue = [
32
+ ...existingValue,
33
+ ...newValue.filter((item) => existingValue.indexOf(item) === -1)
34
+ ];
35
+ } else if (mergeStrategy === "accumulate-unique-by") {
36
+ const existingItemMap = {};
37
+ const newItemMap = {};
38
+ for (let i = 0; i < existingValue.length; i++) {
39
+ const existingItem = existingValue[i];
40
+ if (existingItem && typeof existingItem === "object") {
41
+ const identifier = mergeStrategyData instanceof Function ? mergeStrategyData(existingItem) : existingItem[mergeStrategyData];
42
+ existingItemMap[identifier] = existingItem;
43
+ }
44
+ }
45
+ for (let j = 0; j < newValue.length; j++) {
46
+ const newItem = newValue[j];
47
+ if (newItem && typeof newItem === "object") {
48
+ const identifier = mergeStrategyData instanceof Function ? mergeStrategyData(newItem) : newItem[mergeStrategyData];
49
+ newItemMap[identifier] = newItem;
50
+ }
51
+ }
52
+ mergedValue = Object.keys({
53
+ ...existingItemMap,
54
+ ...newItemMap
55
+ }).map(
56
+ (id, index) => mergeValues(
57
+ [...valuePathArray, index],
58
+ existingItemMap[id],
59
+ newItemMap[id],
60
+ mergeStrategyMap
61
+ )
62
+ );
63
+ } else if (mergeStrategy === "transpose") {
64
+ const fullLength = Math.max(existingValue.length, newValue.length);
65
+ mergedValue = [...new Array(fullLength)].map(
66
+ (_empty, index) => mergeValues(
67
+ [...valuePathArray, index],
68
+ existingValue[index],
69
+ newValue[index],
70
+ mergeStrategyMap
71
+ )
72
+ );
73
+ }
74
+ } else if (isConstructedFrom(existingValue, Object) && isConstructedFrom(newValue, Object)) {
75
+ mergedValue = Object.keys({ ...existingValue, ...newValue }).reduce(
76
+ (acc, k) => ({
77
+ ...acc,
78
+ [k]: mergeValues(
79
+ [...valuePathArray, k],
80
+ existingValue[k],
81
+ newValue[k],
82
+ mergeStrategyMap
83
+ )
84
+ }),
85
+ {}
86
+ );
87
+ }
88
+ }
89
+ return mergedValue;
90
+ };
91
+
92
+ // src/iac/utils/index.ts
93
+ var addParameter = (parameterInfo, template) => {
94
+ const { ParameterId, Parameter, Label, Group } = parameterInfo;
95
+ const {
96
+ Parameters,
97
+ Metadata: {
98
+ "AWS::CloudFormation::Interface": {
99
+ ParameterGroups = [],
100
+ ParameterLabels = {}
101
+ } = {}
102
+ } = {}
103
+ } = template;
104
+ let NewParameterGroups = ParameterGroups;
105
+ if (Group) {
106
+ const GroupObject = ParameterGroups.filter(
107
+ (g) => g.Label?.default === Group
108
+ )[0];
109
+ NewParameterGroups = GroupObject ? ParameterGroups.map(
110
+ (g) => g.Label?.default === Group ? {
111
+ ...g,
112
+ Parameters: [...g.Parameters || [], ParameterId]
113
+ } : g
114
+ ) : [
115
+ ...ParameterGroups,
116
+ {
117
+ Label: {
118
+ default: Group
119
+ },
120
+ Parameters: [ParameterId]
121
+ }
122
+ ];
123
+ }
124
+ return {
125
+ ...template,
126
+ Parameters: {
127
+ ...Parameters,
128
+ [ParameterId]: Parameter
129
+ },
130
+ Metadata: {
131
+ ...template.Metadata,
132
+ "AWS::CloudFormation::Interface": {
133
+ ...template?.Metadata?.["AWS::CloudFormation::Interface"],
134
+ ParameterGroups: NewParameterGroups,
135
+ ParameterLabels: {
136
+ ...ParameterLabels,
137
+ [ParameterId]: {
138
+ default: Label
139
+ }
140
+ }
141
+ }
142
+ }
143
+ };
144
+ };
145
+ var addParameters = (parameters, template) => parameters.reduce((acc, p) => addParameter(p, acc), template);
146
+ var patchTemplate = (patch, template) => mergeValues([], template, patch, {
147
+ [getValuePathString([
148
+ // Parameter Groups
149
+ "Metadata",
150
+ "AWS::CloudFormation::Interface",
151
+ "ParameterGroups"
152
+ ])]: {
153
+ strategy: "accumulate-unique-by",
154
+ data: (pG) => pG?.Label?.default
155
+ },
156
+ [getValuePathString([
157
+ // Parameter Group Parameter Ids
158
+ "Metadata",
159
+ "AWS::CloudFormation::Interface",
160
+ "ParameterGroups",
161
+ "#",
162
+ "Parameters"
163
+ ])]: {
164
+ strategy: "accumulate-unique"
165
+ }
166
+ });
167
+ var createResourcePack = (creator) => (params, template) => {
168
+ const patch = creator(params);
169
+ return patchTemplate(patch, template);
170
+ };
171
+ var SimpleCFT = class {
172
+ /**
173
+ * Create a SimpleCFT template wrapper.
174
+ *
175
+ * @param template - Initial CloudFormation template.
176
+ */
177
+ constructor(template = {
178
+ AWSTemplateFormatVersion: "2010-09-09"
179
+ }) {
180
+ this.template = template;
181
+ }
182
+ /**
183
+ * Apply a pack with configuration to the stack template.
184
+ * @see `@resistdesign/voltra/iac` and `@resistdesign/voltra/iac/packs` for examples.
185
+ * */
186
+ applyPack = (pack, params) => {
187
+ this.template = pack(params, this.template);
188
+ return this;
189
+ };
190
+ /**
191
+ * Apply a patch to the stack template.
192
+ *
193
+ * @param patch - Template patch to merge.
194
+ * */
195
+ patch = (patch) => {
196
+ this.template = patchTemplate(patch, this.template);
197
+ return this;
198
+ };
199
+ /**
200
+ * Add a stack parameter including its descriptive info and an optional parameter group.
201
+ *
202
+ * @param parameter - Parameter definition and metadata.
203
+ * */
204
+ addParameter = (parameter) => {
205
+ this.template = addParameter(parameter, this.template);
206
+ return this;
207
+ };
208
+ /**
209
+ * Add a group of stack parameters including their descriptive info and an optional parameter group.
210
+ *
211
+ * @param group - Parameter group definition.
212
+ * */
213
+ addParameterGroup = ({ Label: Group, Parameters }) => {
214
+ const parameterIds = Object.keys(Parameters);
215
+ const parameterList = parameterIds.map((ParameterId) => {
216
+ const { Label, ...Parameter } = Parameters[ParameterId];
217
+ return {
218
+ Group,
219
+ ParameterId,
220
+ Label,
221
+ Parameter
222
+ };
223
+ });
224
+ this.template = addParameters(parameterList, this.template);
225
+ return this;
226
+ };
227
+ /**
228
+ * Use a modification to dynamically apply various changes at once.
229
+ *
230
+ * @param modification - Modification callback to apply.
231
+ * */
232
+ modify = (modification) => {
233
+ modification(this);
234
+ return this;
235
+ };
236
+ /**
237
+ * Convert the stack template to a string.
238
+ *
239
+ * @returns JSON string representation of the template.
240
+ * */
241
+ toString = () => JSON.stringify(this.template, null, 2);
242
+ /**
243
+ * Convert the stack template to a JSON object.
244
+ *
245
+ * @returns Template JSON object.
246
+ * */
247
+ toJSON = () => this.template;
248
+ /**
249
+ * Convert the stack template to a YAML string.
250
+ *
251
+ * @returns YAML string representation of the template.
252
+ * */
253
+ toYAML = () => YAML.stringify(this.template, {
254
+ aliasDuplicateObjects: false
255
+ });
256
+ };
257
+
258
+ export { DEFAULT_MERGE_STRATEGY, SimpleCFT, addParameter, addParameters, createResourcePack, getValuePathArray, getValuePathString, isConstructedFrom, mergeValues, patchTemplate };
@@ -0,0 +1,71 @@
1
+ import { parseTemplate, validateAreas } from './chunk-WELZGQDJ.js';
2
+
3
+ // src/app/utils/EasyLayout.tsx
4
+ var getPascalCaseAreaName = (area) => {
5
+ return area.split("-").map((a) => a[0].toUpperCase() + a.slice(1)).join("");
6
+ };
7
+ var convertLayoutToCSS = (layout = "", spacing = {}) => {
8
+ const parsed = parseTemplate(layout);
9
+ validateAreas(parsed);
10
+ const renderTrack = (track) => {
11
+ if (track.kind === "px") {
12
+ return `${track.value}px`;
13
+ }
14
+ if (track.kind === "pct") {
15
+ return `${track.value}%`;
16
+ }
17
+ return `${track.value}fr`;
18
+ };
19
+ const areaRows = parsed.areaGrid.map((row) => row.join(" "));
20
+ const rows = parsed.rowTracks.map(renderTrack);
21
+ let css = "";
22
+ if (parsed.colTracks.length) {
23
+ css += `
24
+ grid-template-columns: ${parsed.colTracks.map(renderTrack).join(" ")};`;
25
+ }
26
+ css += `
27
+ grid-template-areas:
28
+ ${areaRows.map((a) => ` "${a}"`).join("\n")};`;
29
+ if (rows.length) {
30
+ css += `
31
+ grid-template-rows: ${rows.join(" ")};`;
32
+ }
33
+ if (typeof spacing.gap !== "undefined") {
34
+ css += `
35
+ gap: ${typeof spacing.gap === "number" ? `${spacing.gap}px` : spacing.gap};`;
36
+ }
37
+ if (typeof spacing.padding !== "undefined") {
38
+ css += `
39
+ padding: ${typeof spacing.padding === "number" ? `${spacing.padding}px` : spacing.padding};`;
40
+ }
41
+ return {
42
+ areasList: parsed.areaNames,
43
+ css
44
+ };
45
+ };
46
+ var getEasyLayoutTemplateDetails = (layout = "", spacing = {}) => convertLayoutToCSS(layout, spacing);
47
+ var createEasyLayout = (config, extendFrom, areasExtendFrom, spacing = {}) => {
48
+ return (layoutTemplate, ...expressions) => {
49
+ const mergedTemplate = layoutTemplate.reduce((acc, l, ind) => {
50
+ const expr = expressions[ind - 1];
51
+ const exprStr = typeof expr === "undefined" ? "" : expr;
52
+ return `${acc}${l}${exprStr}`;
53
+ }, "");
54
+ const { areasList, css } = convertLayoutToCSS(mergedTemplate, spacing);
55
+ const layout = config.createLayout({ base: extendFrom, css });
56
+ const areas = areasList.reduce((acc, area) => {
57
+ const pascalCaseAreaName = getPascalCaseAreaName(area);
58
+ const component = config.createArea({ base: areasExtendFrom, area });
59
+ return {
60
+ ...acc,
61
+ [pascalCaseAreaName]: component
62
+ };
63
+ }, {});
64
+ return {
65
+ layout,
66
+ areas
67
+ };
68
+ };
69
+ };
70
+
71
+ export { createEasyLayout, getEasyLayoutTemplateDetails, getPascalCaseAreaName };