@homedev/hcl 0.0.1 → 0.0.3

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.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,27 @@
1
+ export declare const addAttribute: (to: HclTarget, attribute: HclAttribute) => HclAttribute;
2
+
3
+ export declare const addBlankLine: (to: HclTarget) => void;
4
+
5
+ export declare const addBlock: (to: HclTarget, block: HclBlock) => HclBlock;
6
+
7
+ export declare const addComment: (comment: string, to: HclTarget) => void;
8
+
9
+ export declare const attribute: (from: HclNullableTarget, name: string) => HclAttribute | undefined;
10
+
11
+ export declare const attributes: (from: HclNullableTarget, name: string) => HclAttribute[];
12
+
13
+ export declare const block: (from: HclNullableTarget, type: string) => HclBlock | undefined;
14
+
15
+ export declare const blocks: (from: HclNullableTarget, type: string) => HclBlock[];
16
+
17
+ export declare const bodyItem: (from: HclNullableTarget, desc: HclEntityDesc) => HclBodyItem | undefined;
18
+
19
+ export declare const bodyItems: <T extends HclBodyItem>(from: HclNullableTarget, desc: HclEntityDesc) => T[];
20
+
21
+ export declare const cloudBlock: (document: HclDocument) => HclBlock | undefined;
22
+
23
+ export declare const create: (body?: HclDocument["body"]) => HclDocument;
24
+
1
25
  export declare function hasObjectBlankLine(object: HclObject, key: string): boolean;
2
26
 
3
27
  export declare interface HclAttribute {
@@ -29,18 +53,41 @@ export declare interface HclDocument {
29
53
  body: HclBodyItem[];
30
54
  }
31
55
 
56
+ export declare type HclEntityDesc = {
57
+ kind: 'block';
58
+ type: string;
59
+ } | {
60
+ kind: 'attribute';
61
+ name: string;
62
+ };
63
+
32
64
  export declare interface HclFunction {
33
65
  kind: 'function';
34
66
  name: string;
35
67
  arguments: HclValue[];
36
68
  }
37
69
 
70
+ export declare type HclNullableTarget = HclTarget | undefined;
71
+
38
72
  export declare interface HclObject {
39
73
  [key: string]: HclValue;
40
74
  }
41
75
 
76
+ export declare interface HclRequiredProvider {
77
+ source: HclSource;
78
+ version?: string;
79
+ }
80
+
42
81
  export declare type HclScalar = string | number | boolean | null;
43
82
 
83
+ export declare interface HclSource {
84
+ prefix?: string;
85
+ domain: string;
86
+ name: string;
87
+ }
88
+
89
+ export declare type HclTarget = HclDocument | HclBlock;
90
+
44
91
  export declare interface HclTraversal {
45
92
  kind: 'traversal';
46
93
  parts: string[];
@@ -48,10 +95,34 @@ export declare interface HclTraversal {
48
95
 
49
96
  export declare type HclValue = HclScalar | HclFunction | HclTraversal | HclValue[] | HclObject;
50
97
 
98
+ export declare const load: (path: string) => Promise<HclDocument | null>;
99
+
100
+ export declare const local: (document: HclDocument, name: string) => HclAttribute | undefined;
101
+
102
+ export declare const localBlocks: (document: HclDocument) => HclBlock[];
103
+
51
104
  export declare function markObjectBlankLine(object: HclObject, key: string): void;
52
105
 
53
106
  export declare function parse(source: string): HclDocument;
54
107
 
108
+ export declare const parseProviderSource: (source: string) => HclSource;
109
+
110
+ export declare const providerBlocks: (document: HclDocument, name?: string) => HclBlock[];
111
+
112
+ export declare const requiredProvider: (document: HclDocument, nameOrSource: string | HclSource) => HclAttribute | undefined;
113
+
114
+ export declare const requiredProviderBySource: (document: HclDocument, source: string | HclSource) => HclAttribute | undefined;
115
+
116
+ export declare const requiredProviderList: (document: HclDocument) => HclRequiredProvider[];
117
+
118
+ export declare const requiredProviders: (document: HclDocument) => Record<string, HclObject>;
119
+
120
+ export declare const requiredProvidersBlock: (document: HclDocument) => HclBlock | undefined;
121
+
122
+ export declare const save: (path: string, document: HclDocument) => Promise<void>;
123
+
124
+ export declare const serializeProviderSource: (source: HclSource | string) => string;
125
+
55
126
  export declare function stringify(document: HclDocument, options?: StringifyOptions): string;
56
127
 
57
128
  export declare interface StringifyOptions {
@@ -59,4 +130,14 @@ export declare interface StringifyOptions {
59
130
  align?: boolean;
60
131
  }
61
132
 
133
+ export declare const terraformBlock: (document: HclDocument) => HclBlock | undefined;
134
+
135
+ export declare const toBlock: (type: string, value: Record<string, HclValue>, labels?: string[]) => HclBlock;
136
+
137
+ export declare const updateAttribute: (to: HclTarget, name: string, value: HclAttribute["value"]) => HclAttribute;
138
+
139
+ export declare const updateRequiredProvider: (document: HclDocument, source: string | HclSource, version?: string | null) => HclAttribute;
140
+
141
+ export declare const workspacesBlock: (document: HclDocument) => HclBlock | undefined;
142
+
62
143
  export { }
package/dist/index.js CHANGED
@@ -1,15 +1,15 @@
1
- var m=new WeakMap;function g(e,t){let i=m.get(e);if(!i)i=new Set,m.set(e,i);i.add(t)}function v(e,t){return m.get(e)?.has(t)??!1}function V(e){return new y(e).parseDocument()}class y{source;position=0;line=1;column=1;constructor(e){this.source=e}parseDocument(){let e=this.parseBody(!1);if(this.skipTrivia(),!this.atEnd())this.fail("Expected an attribute or block.");return{body:e}}parseBody(e){let t=[];while(!0){let i=this.skipTrivia(),n=i.blankLine;if(i.comments.forEach((o,u)=>{let a=t.at(-1);if(o.inline&&a&&a.kind!=="comment"&&!o.value.includes(`
2
- `)){a.trailingComment=o.value;return}t.push({kind:"comment",value:o.value,...n&&u===0?{leadingBlankLine:n}:{}})}),e&&this.peek()==="}")return this.advance(),t;if(this.atEnd()){if(e)this.fail("Expected closing `}`.");return t}let r=this.readIdentifier();if(this.skipTrivia(),this.peek()==="="){this.advance(),t.push({kind:"attribute",name:r,value:this.parseValue(),...n?{leadingBlankLine:n}:{}});continue}let s=[];while(this.peek()!=="{"){if(this.atEnd())this.fail("Expected `=` or `{` after item name.");s.push(this.parseLabel()),this.skipTrivia()}this.advance(),t.push({kind:"block",type:r,labels:s,body:this.parseBody(!0),...n?{leadingBlankLine:n}:{}})}}parseLabel(){return this.skipTrivia(),this.peek()==='"'?this.readString():this.readIdentifier()}parseValue(){if(this.skipTrivia(),this.peek()==='"')return this.readString();if(this.peek()==="[")return this.parseArray();if(this.peek()==="{")return this.parseObject();if(this.readKeyword("true"))return!0;if(this.readKeyword("false"))return!1;if(this.readKeyword("null"))return null;let e=this.readNumber();if(e!==void 0)return e;let t=this.readIdentifier();if(this.skipTrivia(),this.peek()==="(")return this.parseFunction(t);if(this.peek()===".")return this.parseTraversal(t);this.fail("Expected a string, number, boolean, null, array, or object value.")}parseFunction(e){this.advance();let t=[];while(!0){if(this.skipTrivia(),this.peek()===")")return this.advance(),{kind:"function",name:e,arguments:t};if(t.push(this.parseValue()),this.skipTrivia(),this.peek()===",")this.advance();else if(this.peek()!==")")this.fail("Expected `,` or `)` in function call.")}}parseTraversal(e){let t=[e];while(this.peek()===".")this.advance(),this.skipTrivia(),t.push(this.readIdentifier()),this.skipTrivia();return{kind:"traversal",parts:t}}parseArray(){this.advance();let e=[];while(!0){if(this.skipTrivia(),this.peek()==="]")return this.advance(),e;e.push(this.parseValue());let t=this.skipTrivia();if(this.peek()===",")this.advance();else if(this.peek()!=="]"&&t.newlines===0)this.fail("Expected `,` or a new line in array.")}}parseObject(){this.advance();let e={},t=!1;while(!0){let i=this.skipTrivia();if(t||=i.blankLine,this.peek()==="}")return this.advance(),e;let n=this.peek()==='"'?this.readString():this.readIdentifier();if(t)g(e,n);if(this.skipTrivia(),this.peek()!=="="&&this.peek()!==":")this.fail("Expected `=` or `:` in object.");this.advance(),e[n]=this.parseValue();let r=this.skipTrivia();if(t=r.blankLine,this.peek()===",")this.advance();else if(this.peek()!=="}"&&r.newlines===0)this.fail("Expected `,` or a new line in object.")}}readKeyword(e){if(!this.source.startsWith(e,this.position)||H(this.source[this.position+e.length]))return!1;return this.advance(e.length),!0}readIdentifier(){if(!O(this.peek()))this.fail("Expected an identifier.");let e=this.position;this.advance();while(H(this.peek()))this.advance();return this.source.slice(e,this.position)}readNumber(){let e=/^[+-]?(?:\d+\.\d*|\.\d+|\d+)(?:[eE][+-]?\d+)?/.exec(this.source.slice(this.position));if(!e)return;let t=Number(e[0]);if(!Number.isFinite(t))this.fail("Number must be finite.");return this.advance(e[0].length),t}readString(){this.advance();let e="",t={'"':'"',"\\":"\\",n:`
1
+ var k=new WeakMap;function E(e,t){let i=k.get(e);if(!i)i=new Set,k.set(e,i);i.add(t)}function w(e,t){return k.get(e)?.has(t)??!1}function m(e){return new D(e).parseDocument()}class D{source;position=0;line=1;column=1;constructor(e){this.source=e}parseDocument(){let e=this.parseBody(!1);if(this.skipTrivia(),!this.atEnd())this.fail("Expected an attribute or block.");return{body:e}}parseBody(e){let t=[];while(!0){let i=this.skipTrivia(),r=i.blankLine;if(i.comments.forEach((c,l)=>{let o=t.at(-1);if(c.inline&&o&&o.kind!=="comment"&&!c.value.includes(`
2
+ `)){o.trailingComment=c.value;return}t.push({kind:"comment",value:c.value,...r&&l===0?{leadingBlankLine:r}:{}})}),e&&this.peek()==="}")return this.advance(),t;if(this.atEnd()){if(e)this.fail("Expected closing `}`.");return t}let n=this.readIdentifier();if(this.skipTrivia(),this.peek()==="="){this.advance(),t.push({kind:"attribute",name:n,value:this.parseValue(),...r?{leadingBlankLine:r}:{}});continue}let s=[];while(this.peek()!=="{"){if(this.atEnd())this.fail("Expected `=` or `{` after item name.");s.push(this.parseLabel()),this.skipTrivia()}this.advance(),t.push({kind:"block",type:n,labels:s,body:this.parseBody(!0),...r?{leadingBlankLine:r}:{}})}}parseLabel(){return this.skipTrivia(),this.peek()==='"'?this.readString():this.readIdentifier()}parseValue(){if(this.skipTrivia(),this.peek()==='"')return this.readString();if(this.peek()==="[")return this.parseArray();if(this.peek()==="{")return this.parseObject();if(this.readKeyword("true"))return!0;if(this.readKeyword("false"))return!1;if(this.readKeyword("null"))return null;let e=this.readNumber();if(e!==void 0)return e;let t=this.readIdentifier();if(this.skipTrivia(),this.peek()==="(")return this.parseFunction(t);if(this.peek()===".")return this.parseTraversal(t);this.fail("Expected a string, number, boolean, null, array, or object value.")}parseFunction(e){this.advance();let t=[];while(!0){if(this.skipTrivia(),this.peek()===")")return this.advance(),{kind:"function",name:e,arguments:t};if(t.push(this.parseValue()),this.skipTrivia(),this.peek()===",")this.advance();else if(this.peek()!==")")this.fail("Expected `,` or `)` in function call.")}}parseTraversal(e){let t=[e];while(this.peek()===".")this.advance(),this.skipTrivia(),t.push(this.readIdentifier()),this.skipTrivia();return{kind:"traversal",parts:t}}parseArray(){this.advance();let e=[];while(!0){if(this.skipTrivia(),this.peek()==="]")return this.advance(),e;e.push(this.parseValue());let t=this.skipTrivia();if(this.peek()===",")this.advance();else if(this.peek()!=="]"&&t.newlines===0)this.fail("Expected `,` or a new line in array.")}}parseObject(){this.advance();let e={},t=!1;while(!0){let i=this.skipTrivia();if(t||=i.blankLine,this.peek()==="}")return this.advance(),e;let r=this.peek()==='"'?this.readString():this.readIdentifier();if(t)E(e,r);if(this.skipTrivia(),this.peek()!=="="&&this.peek()!==":")this.fail("Expected `=` or `:` in object.");this.advance(),e[r]=this.parseValue();let n=this.skipTrivia();if(t=n.blankLine,this.peek()===",")this.advance();else if(this.peek()!=="}"&&n.newlines===0)this.fail("Expected `,` or a new line in object.")}}readKeyword(e){if(!this.source.startsWith(e,this.position)||A(this.source[this.position+e.length]))return!1;return this.advance(e.length),!0}readIdentifier(){if(!K(this.peek()))this.fail("Expected an identifier.");let e=this.position;this.advance();while(A(this.peek()))this.advance();return this.source.slice(e,this.position)}readNumber(){let e=/^[+-]?(?:\d+\.\d*|\.\d+|\d+)(?:[eE][+-]?\d+)?/.exec(this.source.slice(this.position));if(!e)return;let t=Number(e[0]);if(!Number.isFinite(t))this.fail("Number must be finite.");return this.advance(e[0].length),t}readString(){this.advance();let e="",t={'"':'"',"\\":"\\",n:`
3
3
  `,r:"\r",t:"\t"};while(!this.atEnd()&&this.peek()!=='"'){if(this.peek()===`
4
- `||this.peek()==="\r")this.fail("Unterminated string.");if(this.peek()!=="\\"){e+=this.peek(),this.advance();continue}this.advance();let i=this.peek();if(!(i in t))this.fail(`Unsupported escape sequence \\${i}.`);e+=t[i],this.advance()}if(this.atEnd())this.fail("Unterminated string.");return this.advance(),e}skipTrivia(){let e=0,t=!1,i=!0,n=[];while(!this.atEnd())if(/\s/.test(this.peek())){if(this.peek()===`
5
- `)e++,t||=!i,i=!1;this.advance()}else if(this.source.startsWith("#",this.position)||this.source.startsWith("//",this.position)){let r=this.peek()==="#"?1:2,s=this.position+r;i=!0;while(!this.atEnd()&&this.peek()!==`
6
- `)this.advance();n.push({value:this.source.slice(s,this.position).trim(),inline:e===0})}else if(this.source.startsWith("/*",this.position)){let r=this.source.indexOf("*/",this.position+2);if(r<0)this.fail("Unterminated block comment.");i=!0,n.push({value:this.source.slice(this.position+2,r).trim(),inline:e===0}),this.advance(r+2-this.position)}else return{newlines:e,blankLine:t,comments:n};return{newlines:e,blankLine:t,comments:n}}peek(){return this.source[this.position]??""}atEnd(){return this.position>=this.source.length}advance(e=1){for(let t=0;t<e;t++){if(this.peek()===`
7
- `)this.line++,this.column=1;else this.column++;this.position++}}fail(e){throw SyntaxError(`${e} (line ${String(this.line)}, column ${String(this.column)})`)}}function O(e){return/[A-Za-z_]/.test(e)}function H(e){return Boolean(e&&/[A-Za-z0-9_-]/.test(e))}function X(e,t={}){let{indentation:i=" ",align:n=!0}=t;if(!i)throw Error("The indentation string must not be empty.");let r=E(e.body,i,n,0);return r.length?`${r.join(`
4
+ `||this.peek()==="\r")this.fail("Unterminated string.");if(this.peek()!=="\\"){e+=this.peek(),this.advance();continue}this.advance();let i=this.peek();if(!(i in t))this.fail(`Unsupported escape sequence \\${i}.`);e+=t[i],this.advance()}if(this.atEnd())this.fail("Unterminated string.");return this.advance(),e}skipTrivia(){let e=0,t=!1,i=!0,r=[];while(!this.atEnd())if(/\s/.test(this.peek())){if(this.peek()===`
5
+ `)e++,t||=!i,i=!1;this.advance()}else if(this.source.startsWith("#",this.position)||this.source.startsWith("//",this.position)){let n=this.peek()==="#"?1:2,s=this.position+n;i=!0;while(!this.atEnd()&&this.peek()!==`
6
+ `)this.advance();r.push({value:this.source.slice(s,this.position).trim(),inline:e===0})}else if(this.source.startsWith("/*",this.position)){let n=this.source.indexOf("*/",this.position+2);if(n<0)this.fail("Unterminated block comment.");i=!0,r.push({value:this.source.slice(this.position+2,n).trim(),inline:e===0}),this.advance(n+2-this.position)}else return{newlines:e,blankLine:t,comments:r};return{newlines:e,blankLine:t,comments:r}}peek(){return this.source[this.position]??""}atEnd(){return this.position>=this.source.length}advance(e=1){for(let t=0;t<e;t++){if(this.peek()===`
7
+ `)this.line++,this.column=1;else this.column++;this.position++}}fail(e){throw SyntaxError(`${e} (line ${String(this.line)}, column ${String(this.column)})`)}}function K(e){return/[A-Za-z_]/.test(e)}function A(e){return Boolean(e&&/[A-Za-z0-9_-]/.test(e))}function O(e,t={}){let{indentation:i=" ",align:r=!0}=t;if(!i)throw Error("The indentation string must not be empty.");let n=S(e.body,i,r,0);return n.length?`${n.join(`
8
8
  `)}
9
- `:""}function E(e,t,i,n){let r=t.repeat(n),s=[],o=[],u=()=>{I(o).forEach((l,h)=>{if(h>0)s.push("");let d=i?Math.max(...l.map((c)=>c.name.length)):0;l.forEach((c,p)=>{if(p>0&&B(l[p-1].value))s.push("");s.push(`${r}${c.name.padEnd(d)} = ${f(c.value,t,i,n)}${x(c.trailingComment)}`)})}),o=[]};return e.forEach((a,l)=>{if(a.kind==="attribute"){o.push(a);return}if(u(),a.kind==="comment"){if(a.leadingBlankLine)s.push("");s.push(...a.value.split(/\r?\n/).map((h)=>`${r}#${h?` ${h.trim()}`:""}`));return}if(l>0)s.push("");s.push(`${r}${[a.type,...a.labels.map(k)].join(" ")} {`),s.push(...E(a.body,t,i,n+1)),s.push(`${r}}${x(a.trailingComment)}`)}),u(),s}function f(e,t,i,n){if(typeof e==="string")return k(e);if(typeof e==="number"||typeof e==="boolean")return String(e);if(e===null)return"null";if(j(e))return`${e.name}(${e.arguments.map((l)=>f(l,t,i,n)).join(", ")})`;if(T(e))return e.parts.join(".");if(Array.isArray(e))return L(e,t,i,n);let r=Object.entries(e);if(!r.length)return"{}";let s=t.repeat(n+1),o=t.repeat(n),u=r.map(([l])=>S(l)),a=[];return A(e,r).forEach((l,h)=>{if(h>0)a.push("");let d=i?Math.max(...l.map(({index:c})=>u[c].length)):0;l.forEach(({entry:[c,p],index:w},b)=>{if(b>0&&B(l[b-1].entry[1]))a.push("");a.push(`${s}${u[w].padEnd(d)} = ${f(p,t,i,n+1)}`)})}),`{
10
- ${a.join(`
9
+ `:""}function S(e,t,i,r){let n=t.repeat(r),s=[],c=[],l=()=>{W(c).forEach((a,p)=>{if(p>0)s.push("");let b=i?Math.max(...a.map((u)=>u.name.length)):0;a.forEach((u,d)=>{if(d>0&&I(a[d-1].value))s.push("");s.push(`${n}${u.name.padEnd(b)} = ${f(u.value,t,i,r)}${V(u.trailingComment)}`)})}),c=[]};return e.forEach((o,a)=>{if(o.kind==="attribute"){c.push(o);return}if(l(),o.kind==="comment"){if(o.leadingBlankLine)s.push("");s.push(...o.value.split(/\r?\n/).map((p)=>`${n}#${p?` ${p.trim()}`:""}`));return}if(a>0)s.push("");s.push(`${n}${[o.type,...o.labels.map(H)].join(" ")} {`),s.push(...S(o.body,t,i,r+1)),s.push(`${n}}${V(o.trailingComment)}`)}),l(),s}function f(e,t,i,r){if(typeof e==="string")return H(e);if(typeof e==="number"||typeof e==="boolean")return String(e);if(e===null)return"null";if(L(e))return`${e.name}(${e.arguments.map((a)=>f(a,t,i,r)).join(", ")})`;if(P(e))return e.parts.join(".");if(Array.isArray(e))return R(e,t,i,r);let n=Object.entries(e);if(!n.length)return"{}";let s=t.repeat(r+1),c=t.repeat(r),l=n.map(([a])=>Z(a)),o=[];return U(e,n).forEach((a,p)=>{if(p>0)o.push("");let b=i?Math.max(...a.map(({index:u})=>l[u].length)):0;a.forEach(({entry:[u,d],index:_},j)=>{if(j>0&&I(a[j-1].entry[1]))o.push("");o.push(`${s}${l[_].padEnd(b)} = ${f(d,t,i,r+1)}`)})}),`{
10
+ ${o.join(`
11
11
  `)}
12
- ${o}}`}function L(e,t,i,n){if(!e.length)return"[]";let r=t.repeat(n+1),s=t.repeat(n);return`[
13
- ${e.map((o)=>`${r}${f(o,t,i,n+1)}`).join(`
12
+ ${c}}`}function R(e,t,i,r){if(!e.length)return"[]";let n=t.repeat(r+1),s=t.repeat(r);return`[
13
+ ${e.map((c)=>`${n}${f(c,t,i,r+1)}`).join(`
14
14
  `)}
15
- ${s}]`}function I(e){return e.reduce((t,i)=>{if(i.leadingBlankLine||!t.length)t.push([]);return t.at(-1)?.push(i),t},[])}function A(e,t){return t.reduce((i,n,r)=>{if(v(e,n[0])||!i.length)i.push([]);return i.at(-1)?.push({entry:n,index:r}),i},[])}function S(e){return F(e)?e:k(e)}function F(e){return/^[A-Za-z_][A-Za-z0-9_-]*$/.test(e)}function B(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)&&!j(e)&&!T(e)}function j(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)&&e.kind==="function"}function T(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)&&e.kind==="traversal"}function x(e){return e?` # ${e}`:""}function k(e){return`"${e.replace(/\\/g,"\\\\").replace(/"/g,"\\\"").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t")}"`}export{v as hasObjectBlankLine,g as markObjectBlankLine,V as parse,X as stringify};
15
+ ${s}]`}function W(e){return e.reduce((t,i)=>{if(i.leadingBlankLine||!t.length)t.push([]);return t.at(-1)?.push(i),t},[])}function U(e,t){return t.reduce((i,r,n)=>{if(w(e,r[0])||!i.length)i.push([]);return i.at(-1)?.push({entry:r,index:n}),i},[])}function Z(e){return M(e)?e:H(e)}function M(e){return/^[A-Za-z_][A-Za-z0-9_-]*$/.test(e)}function I(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)&&!L(e)&&!P(e)}function L(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)&&e.kind==="function"}function P(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)&&e.kind==="traversal"}function V(e){return e?` # ${e}`:""}function H(e){return`"${e.replace(/\\/g,"\\\\").replace(/"/g,"\\\"").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t")}"`}var ge=(e,t)=>e?.body?.find((i)=>{if(i.kind!==t.kind)return!1;if(t.kind==="block"&&i.kind==="block"&&i.type!==t.type)return!1;if(t.kind==="attribute"&&i.kind==="attribute"&&i.name!==t.name)return!1;return!0}),F=(e,t)=>e?.body?.filter((i)=>{if(i.kind!==t.kind)return!1;if(t.kind==="block"&&i.kind==="block"&&i.type!==t.type)return!1;if(t.kind==="attribute"&&i.kind==="attribute"&&i.name!==t.name)return!1;return!0})??[],v=(e,t)=>F(e,{kind:"block",type:t}),G=(e,t)=>F(e,{kind:"attribute",name:t}),h=(e,t)=>v(e,t)?.[0],y=(e,t)=>G(e,t)?.[0],q=(e,t)=>(e.body.push(t),t),J=(e,t)=>(e.body.push(t),t),ve=(e,t)=>{t.body.push({kind:"comment",value:e})},ye=(e)=>{e.body.push({kind:"comment",value:"",leadingBlankLine:!0})},x=(e)=>h(e,"terraform"),B=(e)=>h(x(e),"required_providers"),Q=(e)=>h(x(e),"cloud"),xe=(e)=>h(Q(e),"workspaces"),Be=(e,t)=>{let i=v(e,"provider");return t?i.filter((r)=>r.labels[0]===t):i},X=(e)=>v(e,"locals"),Te=(e,t)=>X(e).map((i)=>y(i,t)).find((i)=>i!==void 0),N=(e)=>{let t=B(e);if(!t)return{};return Object.fromEntries(t.body.filter((i)=>i.kind==="attribute").map((i)=>[i.name,i.value]))},Y=(e,t)=>{let i=g(t),r=N(e);for(let n of Object.entries(r))if(g(n[1].source)===i)return z(e,n[0]);return},z=(e,t)=>{if(typeof t==="string"){let i=y(B(e),t);if(i)return i}return Y(e,t)},je=(e=[])=>({body:e}),Ee=(e,t,i=[])=>({kind:"block",type:e,labels:i,body:Object.entries(t).map(([r,n])=>({kind:"attribute",name:r,value:n}))}),ee=(e,t,i)=>{let r=y(e,t);if(r)return r.value=i,r;return J(e,{kind:"attribute",name:t,value:i})},te=(e)=>typeof e==="string"?T(e):e,we=(e,t,i)=>{let r=x(e)??q(e,{kind:"block",type:"terraform",labels:[],body:[]}),n=B(e)??q(r,{kind:"block",type:"required_providers",labels:[],body:[]}),s=te(t),c=z(e,s.name)??ee(n,s.name,{source:s.name}),l=c.value??{};if(l.source=g(s),i)l.version=i;if(i===null)delete l.version;return c.value=l,c},T=(e)=>{let t=e.split("/");if(t.length<2)return{domain:"hashicorp",name:t[0]};let i=t.pop(),r=t.pop();return{prefix:t.length>0?t.join("/"):void 0,domain:r,name:i}},g=(e)=>{if(typeof e==="string")e=T(e);let t=[];if(e.prefix)t.push(e.prefix);return t.push(e.domain),t.push(e.name),t.join("/")},Ae=(e)=>{let t=N(e);return Object.entries(t).map(([i,r])=>{if(typeof r!=="object"||r===null)throw Error(`Invalid required provider value for ${i}`);let n=typeof r.source==="string"?r.source:i,s=typeof r.version==="string"?r.version:void 0;return{source:T(n),version:s}})};import C from"fs/promises";var Se=async(e)=>{try{let t=await C.readFile(e,"utf-8");return m(t)}catch{return null}},Ie=async(e,t)=>{let i=O(t);await C.writeFile(e,i,"utf-8")};export{J as addAttribute,ye as addBlankLine,q as addBlock,ve as addComment,y as attribute,G as attributes,h as block,v as blocks,ge as bodyItem,F as bodyItems,Q as cloudBlock,je as create,w as hasObjectBlankLine,Se as load,Te as local,X as localBlocks,E as markObjectBlankLine,m as parse,T as parseProviderSource,Be as providerBlocks,z as requiredProvider,Y as requiredProviderBySource,Ae as requiredProviderList,N as requiredProviders,B as requiredProvidersBlock,Ie as save,g as serializeProviderSource,O as stringify,x as terraformBlock,Ee as toBlock,ee as updateAttribute,we as updateRequiredProvider,xe as workspacesBlock};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homedev/hcl",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "hcl parser and serializer",
5
5
  "author": "julzor",
6
6
  "license": "ISC",