@homedev/hcl 1.0.0 → 1.0.2
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 +52 -13
- package/dist/index.js +11 -11
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,24 +1,26 @@
|
|
|
1
|
-
export declare const addAttribute: (
|
|
1
|
+
export declare const addAttribute: (to: HclTarget, attribute: HclAttribute) => HclAttribute;
|
|
2
2
|
|
|
3
3
|
export declare const addBlankLine: (to: HclTarget) => void;
|
|
4
4
|
|
|
5
|
-
export declare const addBlock: (
|
|
5
|
+
export declare const addBlock: (to: HclTarget, block: HclBlock) => HclBlock;
|
|
6
6
|
|
|
7
7
|
export declare const addComment: (comment: string, to: HclTarget) => void;
|
|
8
8
|
|
|
9
|
-
export declare const attribute: (
|
|
9
|
+
export declare const attribute: (from: HclNullableTarget, name: string) => HclAttribute | undefined;
|
|
10
10
|
|
|
11
|
-
export declare const attributes: (
|
|
11
|
+
export declare const attributes: (from: HclNullableTarget, name: string) => HclAttribute[];
|
|
12
12
|
|
|
13
|
-
export declare const block: (
|
|
13
|
+
export declare const block: (from: HclNullableTarget, type: string) => HclBlock | undefined;
|
|
14
14
|
|
|
15
|
-
export declare const blocks: (
|
|
15
|
+
export declare const blocks: (from: HclNullableTarget, type: string) => HclBlock[];
|
|
16
16
|
|
|
17
|
-
export declare const
|
|
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[];
|
|
18
20
|
|
|
19
|
-
export declare const
|
|
21
|
+
export declare const cloudBlock: (document: HclDocument) => HclBlock | undefined;
|
|
20
22
|
|
|
21
|
-
export declare const
|
|
23
|
+
export declare const create: (body?: HclDocument["body"]) => HclDocument;
|
|
22
24
|
|
|
23
25
|
export declare function hasObjectBlankLine(object: HclObject, key: string): boolean;
|
|
24
26
|
|
|
@@ -51,18 +53,39 @@ export declare interface HclDocument {
|
|
|
51
53
|
body: HclBodyItem[];
|
|
52
54
|
}
|
|
53
55
|
|
|
56
|
+
export declare type HclEntityDesc = {
|
|
57
|
+
kind: 'block';
|
|
58
|
+
type: string;
|
|
59
|
+
} | {
|
|
60
|
+
kind: 'attribute';
|
|
61
|
+
name: string;
|
|
62
|
+
};
|
|
63
|
+
|
|
54
64
|
export declare interface HclFunction {
|
|
55
65
|
kind: 'function';
|
|
56
66
|
name: string;
|
|
57
67
|
arguments: HclValue[];
|
|
58
68
|
}
|
|
59
69
|
|
|
70
|
+
export declare type HclNullableTarget = HclTarget | undefined;
|
|
71
|
+
|
|
60
72
|
export declare interface HclObject {
|
|
61
73
|
[key: string]: HclValue;
|
|
62
74
|
}
|
|
63
75
|
|
|
76
|
+
export declare interface HclRequiredProvider {
|
|
77
|
+
source: HclSource;
|
|
78
|
+
version?: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
64
81
|
export declare type HclScalar = string | number | boolean | null;
|
|
65
82
|
|
|
83
|
+
export declare interface HclSource {
|
|
84
|
+
prefix?: string;
|
|
85
|
+
domain: string;
|
|
86
|
+
name: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
66
89
|
export declare type HclTarget = HclDocument | HclBlock;
|
|
67
90
|
|
|
68
91
|
export declare interface HclTraversal {
|
|
@@ -72,18 +95,34 @@ export declare interface HclTraversal {
|
|
|
72
95
|
|
|
73
96
|
export declare type HclValue = HclScalar | HclFunction | HclTraversal | HclValue[] | HclObject;
|
|
74
97
|
|
|
98
|
+
export declare const load: (path: string) => Promise<HclDocument | null>;
|
|
99
|
+
|
|
100
|
+
export declare const local: (document: HclDocument, name: string) => HclAttribute | undefined;
|
|
101
|
+
|
|
75
102
|
export declare const localBlocks: (document: HclDocument) => HclBlock[];
|
|
76
103
|
|
|
77
104
|
export declare function markObjectBlankLine(object: HclObject, key: string): void;
|
|
78
105
|
|
|
79
106
|
export declare function parse(source: string): HclDocument;
|
|
80
107
|
|
|
81
|
-
export declare const
|
|
108
|
+
export declare const parseProviderSource: (source: string) => HclSource;
|
|
109
|
+
|
|
110
|
+
export declare const providerBlocks: (document: HclDocument, name?: string) => HclBlock[];
|
|
82
111
|
|
|
83
|
-
export declare const requiredProvider: (
|
|
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>;
|
|
84
119
|
|
|
85
120
|
export declare const requiredProvidersBlock: (document: HclDocument) => HclBlock | undefined;
|
|
86
121
|
|
|
122
|
+
export declare const save: (path: string, document: HclDocument) => Promise<void>;
|
|
123
|
+
|
|
124
|
+
export declare const serializeProviderSource: (source: HclSource | string) => string;
|
|
125
|
+
|
|
87
126
|
export declare function stringify(document: HclDocument, options?: StringifyOptions): string;
|
|
88
127
|
|
|
89
128
|
export declare interface StringifyOptions {
|
|
@@ -95,9 +134,9 @@ export declare const terraformBlock: (document: HclDocument) => HclBlock | undef
|
|
|
95
134
|
|
|
96
135
|
export declare const toBlock: (type: string, value: Record<string, HclValue>, labels?: string[]) => HclBlock;
|
|
97
136
|
|
|
98
|
-
export declare const updateAttribute: (name: string, value: HclAttribute["value"]
|
|
137
|
+
export declare const updateAttribute: (to: HclTarget, name: string, value: HclAttribute["value"]) => HclAttribute;
|
|
99
138
|
|
|
100
|
-
export declare const updateRequiredProvider: (
|
|
139
|
+
export declare const updateRequiredProvider: (document: HclDocument, source: string | HclSource, version?: string | null) => HclAttribute;
|
|
101
140
|
|
|
102
141
|
export declare const workspacesBlock: (document: HclDocument) => HclBlock | undefined;
|
|
103
142
|
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
var
|
|
2
|
-
`)){
|
|
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,
|
|
5
|
-
`)e++,t||=!i,i=!1;this.advance()}else if(this.source.startsWith("#",this.position)||this.source.startsWith("//",this.position)){let
|
|
6
|
-
`)this.advance();
|
|
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
|
|
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
|
|
10
|
-
${
|
|
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
|
-
${c}}`}function
|
|
13
|
-
${e.map((c)=>`${
|
|
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
|
|
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};
|