@homedev/hcl 0.0.1 → 0.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 +42 -0
- package/dist/index.js +7 -7
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
export declare const addAttribute: (attribute: HclAttribute, to: HclTarget) => HclAttribute;
|
|
2
|
+
|
|
3
|
+
export declare const addBlankLine: (to: HclTarget) => void;
|
|
4
|
+
|
|
5
|
+
export declare const addBlock: (block: HclBlock, to: HclTarget) => HclBlock;
|
|
6
|
+
|
|
7
|
+
export declare const addComment: (comment: string, to: HclTarget) => void;
|
|
8
|
+
|
|
9
|
+
export declare const attribute: (name: string, from?: HclTarget) => HclAttribute | undefined;
|
|
10
|
+
|
|
11
|
+
export declare const attributes: (name: string, from?: HclTarget) => HclAttribute[];
|
|
12
|
+
|
|
13
|
+
export declare const block: (type: string, from?: HclTarget) => HclBlock | undefined;
|
|
14
|
+
|
|
15
|
+
export declare const blocks: (type: string, from?: HclTarget) => HclBlock[];
|
|
16
|
+
|
|
17
|
+
export declare const cloudBlock: (document: HclDocument) => HclBlock | undefined;
|
|
18
|
+
|
|
19
|
+
export declare const createDocument: (body?: HclDocument["body"]) => HclDocument;
|
|
20
|
+
|
|
21
|
+
export declare const findLocal: (name: string, document: HclDocument) => HclAttribute | undefined;
|
|
22
|
+
|
|
1
23
|
export declare function hasObjectBlankLine(object: HclObject, key: string): boolean;
|
|
2
24
|
|
|
3
25
|
export declare interface HclAttribute {
|
|
@@ -41,6 +63,8 @@ export declare interface HclObject {
|
|
|
41
63
|
|
|
42
64
|
export declare type HclScalar = string | number | boolean | null;
|
|
43
65
|
|
|
66
|
+
export declare type HclTarget = HclDocument | HclBlock;
|
|
67
|
+
|
|
44
68
|
export declare interface HclTraversal {
|
|
45
69
|
kind: 'traversal';
|
|
46
70
|
parts: string[];
|
|
@@ -48,10 +72,18 @@ export declare interface HclTraversal {
|
|
|
48
72
|
|
|
49
73
|
export declare type HclValue = HclScalar | HclFunction | HclTraversal | HclValue[] | HclObject;
|
|
50
74
|
|
|
75
|
+
export declare const localBlocks: (document: HclDocument) => HclBlock[];
|
|
76
|
+
|
|
51
77
|
export declare function markObjectBlankLine(object: HclObject, key: string): void;
|
|
52
78
|
|
|
53
79
|
export declare function parse(source: string): HclDocument;
|
|
54
80
|
|
|
81
|
+
export declare const providerBlocks: (name: string, document: HclDocument) => HclBlock[];
|
|
82
|
+
|
|
83
|
+
export declare const requiredProvider: (name: string, document: HclDocument) => HclAttribute | undefined;
|
|
84
|
+
|
|
85
|
+
export declare const requiredProvidersBlock: (document: HclDocument) => HclBlock | undefined;
|
|
86
|
+
|
|
55
87
|
export declare function stringify(document: HclDocument, options?: StringifyOptions): string;
|
|
56
88
|
|
|
57
89
|
export declare interface StringifyOptions {
|
|
@@ -59,4 +91,14 @@ export declare interface StringifyOptions {
|
|
|
59
91
|
align?: boolean;
|
|
60
92
|
}
|
|
61
93
|
|
|
94
|
+
export declare const terraformBlock: (document: HclDocument) => HclBlock | undefined;
|
|
95
|
+
|
|
96
|
+
export declare const toBlock: (type: string, value: Record<string, HclValue>, labels?: string[]) => HclBlock;
|
|
97
|
+
|
|
98
|
+
export declare const updateAttribute: (name: string, value: HclAttribute["value"], to: HclTarget) => HclAttribute;
|
|
99
|
+
|
|
100
|
+
export declare const updateRequiredProvider: (name: string, version: string | undefined, document: HclDocument) => HclAttribute;
|
|
101
|
+
|
|
102
|
+
export declare const workspacesBlock: (document: HclDocument) => HclBlock | undefined;
|
|
103
|
+
|
|
62
104
|
export { }
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
var
|
|
2
|
-
`)){a.trailingComment=
|
|
1
|
+
var b=new WeakMap;function x(e,t){let i=b.get(e);if(!i)i=new Set,b.set(e,i);i.add(t)}function B(e,t){return b.get(e)?.has(t)??!1}function S(e){return new j(e).parseDocument()}class j{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((c,u)=>{let a=t.at(-1);if(c.inline&&a&&a.kind!=="comment"&&!c.value.includes(`
|
|
2
|
+
`)){a.trailingComment=c.value;return}t.push({kind:"comment",value:c.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)x(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)||T(this.source[this.position+e.length]))return!1;return this.advance(e.length),!0}readIdentifier(){if(!C(this.peek()))this.fail("Expected an identifier.");let e=this.position;this.advance();while(T(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
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
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
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
|
|
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 C(e){return/[A-Za-z_]/.test(e)}function T(e){return Boolean(e&&/[A-Za-z0-9_-]/.test(e))}function pe(e,t={}){let{indentation:i=" ",align:n=!0}=t;if(!i)throw Error("The indentation string must not be empty.");let r=A(e.body,i,n,0);return r.length?`${r.join(`
|
|
8
8
|
`)}
|
|
9
|
-
`:""}function
|
|
9
|
+
`:""}function A(e,t,i,n){let r=t.repeat(n),s=[],c=[],u=()=>{q(c).forEach((o,p)=>{if(p>0)s.push("");let k=i?Math.max(...o.map((l)=>l.name.length)):0;o.forEach((l,h)=>{if(h>0&&w(o[h-1].value))s.push("");s.push(`${r}${l.name.padEnd(k)} = ${d(l.value,t,i,n)}${E(l.trailingComment)}`)})}),c=[]};return e.forEach((a,o)=>{if(a.kind==="attribute"){c.push(a);return}if(u(),a.kind==="comment"){if(a.leadingBlankLine)s.push("");s.push(...a.value.split(/\r?\n/).map((p)=>`${r}#${p?` ${p.trim()}`:""}`));return}if(o>0)s.push("");s.push(`${r}${[a.type,...a.labels.map(H)].join(" ")} {`),s.push(...A(a.body,t,i,n+1)),s.push(`${r}}${E(a.trailingComment)}`)}),u(),s}function d(e,t,i,n){if(typeof e==="string")return H(e);if(typeof e==="number"||typeof e==="boolean")return String(e);if(e===null)return"null";if(V(e))return`${e.name}(${e.arguments.map((o)=>d(o,t,i,n)).join(", ")})`;if(O(e))return e.parts.join(".");if(Array.isArray(e))return F(e,t,i,n);let r=Object.entries(e);if(!r.length)return"{}";let s=t.repeat(n+1),c=t.repeat(n),u=r.map(([o])=>z(o)),a=[];return _(e,r).forEach((o,p)=>{if(p>0)a.push("");let k=i?Math.max(...o.map(({index:l})=>u[l].length)):0;o.forEach(({entry:[l,h],index:I},y)=>{if(y>0&&w(o[y-1].entry[1]))a.push("");a.push(`${s}${u[I].padEnd(k)} = ${d(h,t,i,n+1)}`)})}),`{
|
|
10
10
|
${a.join(`
|
|
11
11
|
`)}
|
|
12
|
-
${
|
|
13
|
-
${e.map((
|
|
12
|
+
${c}}`}function F(e,t,i,n){if(!e.length)return"[]";let r=t.repeat(n+1),s=t.repeat(n);return`[
|
|
13
|
+
${e.map((c)=>`${r}${d(c,t,i,n+1)}`).join(`
|
|
14
14
|
`)}
|
|
15
|
-
${s}]`}function
|
|
15
|
+
${s}]`}function q(e){return e.reduce((t,i)=>{if(i.leadingBlankLine||!t.length)t.push([]);return t.at(-1)?.push(i),t},[])}function _(e,t){return t.reduce((i,n,r)=>{if(B(e,n[0])||!i.length)i.push([]);return i.at(-1)?.push({entry:n,index:r}),i},[])}function z(e){return P(e)?e:H(e)}function P(e){return/^[A-Za-z_][A-Za-z0-9_-]*$/.test(e)}function w(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)&&!V(e)&&!O(e)}function V(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)&&e.kind==="function"}function O(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)&&e.kind==="traversal"}function E(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 m=(e,t)=>t?.body.filter((i)=>i.kind==="block"&&i.type===e)??[],K=(e,t)=>t?.body.filter((i)=>i.kind==="attribute"&&i.name===e)??[],f=(e,t)=>m(e,t)?.[0],g=(e,t)=>K(e,t)?.[0],v=(e)=>f("terraform",e),D=(e)=>f("required_providers",v(e)),N=(e)=>f("cloud",v(e)),de=(e)=>f("workspaces",N(e)),fe=(e,t)=>m("provider",t).filter((i)=>i.labels[0]===e),W=(e)=>m("locals",e),U=(e,t)=>g(e,D(t)),ke=(e,t)=>g(e,W(t)?.[0]),be=(e=[])=>({body:e}),He=(e,t,i=[])=>({kind:"block",type:e,labels:i,body:Object.entries(t).map(([n,r])=>({kind:"attribute",name:n,value:r}))}),L=(e,t)=>(t.body.push(e),e),Z=(e,t)=>(t.body.push(e),e),M=(e,t,i)=>{let n=g(e,i);if(n)return n.value=t,n;return Z({kind:"attribute",name:e,value:t},i)},me=(e,t)=>{t.body.push({kind:"comment",value:e})},ge=(e)=>{e.body.push({kind:"comment",value:"",leadingBlankLine:!0})},ve=(e,t,i)=>{let n=v(i)??L({kind:"block",type:"terraform",labels:[],body:[]},i),r=D(i)??L({kind:"block",type:"required_providers",labels:[],body:[]},n),s=U(e,i)??M(e,{source:e},r),c=R(s.value)?s.value:{source:e};if(t)c.version=t;else delete c.version;return s.value=c,s};function R(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)&&!("kind"in e)}export{Z as addAttribute,ge as addBlankLine,L as addBlock,me as addComment,g as attribute,K as attributes,f as block,m as blocks,N as cloudBlock,be as createDocument,ke as findLocal,B as hasObjectBlankLine,W as localBlocks,x as markObjectBlankLine,S as parse,fe as providerBlocks,U as requiredProvider,D as requiredProvidersBlock,pe as stringify,v as terraformBlock,He as toBlock,M as updateAttribute,ve as updateRequiredProvider,de as workspacesBlock};
|