HCL v2 parser for JS
This is a HCL version 2 parser for JavaScript, there are several other JS parsers for HCL but none that support the newer HCL v2 syntax
It wraps the very helpful tmccombs/hcl2json and calls the convert package in order to parse HCL input strings to JSON strings. The Go code in parser.go
is converted to JS using GopherJS
TypeScript definitions are included
Reference
The module exports the following functions:
function parseToString(input: string): string
function parseToObject(input: string): any
Usage
Install as normal with NPM
npm install hcl2-parser
Importing into your project
const hcl = require("hcl2-parser")
import * as hcl = from "hcl2-parser"
Simple example of usage
const hcl = require("hcl2-parser")
const hclString = `
# Create a resource group
variable "azureRegion" {
type = string
default = "uksouth"
}
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = var.azureRegion
}
`
stringResult = hcl.parseToString(hclString)
console.log(stringResult)
objectResult = hcl.parseToObject(hclString)
console.log(objectResult[0].resource.azurerm_resource_group)