Skip to main content

HclUtility

Reads, serializes and diffs the terraform octo renders.

The render format is octo-owned and regular: # <moduleId>/<file> section headers, and top-level blocks that start on a line with no indent. So everything here is plain string work, needing no external HCL parser or diff library. parse reads the blocks a module test asserts on, serialize flattens a render into one string, unifiedDiff is the generic line-diff engine, and diffBlocks layers octo's block structure on top of it.

@group

Utilities/Hcl

Index

Constructors

constructor

Methods

staticdiffBlocks

  • diffBlocks(previous: string, current: string): string
  • Diffs two serialize renders at the block level. A block is identified by its header line, and blocks sharing a header (one provider "aws" per alias) by their first body line too, so a body change reads as ~ (a line-level unifiedDiff of what changed inside the block), not as an unrelated add + remove. An added block shows its full new body, a removed block its full old body — symmetric, with nothing trimmed away. The +/-/~ vocabulary mirrors the digestDiffs digest, tagged with moduleId/file. Returns '<no changes>' when nothing moved.


    Parameters

    • previous: string
    • current: string

    Returns string

staticparse

  • parse(hcl: string): { data: Record<string, HclBlock>; resource: Record<string, HclBlock> }
  • Reads the resource and data blocks of rendered terraform, keyed by their terraform address: <type>.<name> for a resource, data.<type>.<name> for a data source. It takes one file's text or a whole serialize render, such as the hclRender a module test gets back, since resource and data addresses are unique across an app.

    It reads octo's own render format, not arbitrary HCL: one attribute or block per line, and a value spanning lines only inside brackets.

    @example
    const { resource } = HclUtility.parse(hclRender);
    expect(Object.keys(resource)).toContain('aws_vpc.vpc-region-475bfa');
    expect(resource['aws_vpc.vpc-region-475bfa'].cidr_block).toBe('10.0.0.0/16');

    Parameters

    • hcl: string

    Returns { data: Record<string, HclBlock>; resource: Record<string, HclBlock> }

staticserialize

  • serialize(moduleFiles: Map<string>): string
  • Flattens the rendered terragrunt folders into one snapshot-friendly string: folders sorted by module id, files in a stable order, each prefixed with a # <moduleId>/<file> header. An empty file shows a <empty> body, so the file is still visible in the snapshot rather than omitted.


    Parameters

    • moduleFiles: Map<string>

    Returns string

staticunifiedDiff

  • unifiedDiff(before: string, after: string, __namedParameters?: { contextLines?: number }): string
  • Produces a git-style unified line diff of two multi-line strings, collapsing unchanged regions to at most contextLines of surrounding context per change and emitting @@ -a,c +b,c @@ hunk headers. Changed lines are tagged - (only in before) and + (only in after); context lines are tagged with two spaces, so the markers line up. Pure plain text, no ANSI color, so it is safe to embed in a snapshot.

    Returns an empty string when the two inputs are identical.


    Parameters

    • before: string
    • after: string
    • __namedParameters: { contextLines?: number } = {}
      • optionalcontextLines: number = 3

    Returns string