hcl-fmt
v0.0.4
Published
`terraform fmt` / `tofu fmt` compiled to WebAssembly, exposed as a JavaScript library.
Readme
hcl-fmt
terraform fmt / tofu fmt compiled to WebAssembly, exposed as a JavaScript library.
Formats HCL/Terraform/OpenTofu source code using the exact same rules as tofu fmt: indentation, = alignment, "${expr}" → expr unwrapping, and legacy type normalization.
Note: This project was built with the assistance of Claude Code (Anthropic AI).
Requirements
- Go ≥ 1.23
- Node.js ≥ 18
Setup
The Go formatting logic is extracted from the OpenTofu source. Clone it once before building:
npm run clone-opentofu
go mod downloadBuild
npm run buildUsage
import { format } from './index.js'
const source = `
resource "aws_s3_bucket" "example" {
bucket = "my-bucket"
val = "\${var.name}"
}
`
const formatted = await format(source)
console.log(formatted)format(source) returns a Promise<string> with the canonical form, or throws if the HCL is invalid.
