UNPKG

@hyperlane-xyz/utils

Version:

General utilities and types for the Hyperlane network

55 lines 1.63 kB
import path from 'path'; import { parse, } from 'yaml'; import { objMerge, stringifyObject } from '../objects.js'; import { isFile, readFileAtPath, writeToFile } from './utils.js'; /** * Parses YAML content with sensible defaults. * @see stackoverflow.com/questions/63075256/why-does-the-npm-yaml-library-have-a-max-alias-number */ export function yamlParse(content, options) { return parse(content, { maxAliasCount: -1, ...options }); } /** * Reads and parses a YAML file. */ export function readYaml(filepath) { return yamlParse(readFileAtPath(filepath)); } /** * Attempts to read and parse a YAML file, returning null if it fails. */ export function tryReadYaml(filepath) { try { return readYaml(filepath); } catch { return null; } } /** * Writes an object as YAML to a file with a trailing newline. * Uses stringifyObject to properly handle ethers BigNumber serialization. */ export function writeYaml(filepath, obj) { writeToFile(filepath, stringifyObject(obj, 'yaml', 2).trimEnd()); } /** * Merges an object with existing YAML file content and writes the result. * If the file doesn't exist, writes the object directly. */ export function mergeYaml(filepath, obj) { if (isFile(filepath)) { const previous = readYaml(filepath); writeYaml(filepath, objMerge(previous, obj)); } else { writeYaml(filepath, obj); } } /** * Reads YAML from a directory with the specified filename. */ export function readYamlFromDir(directory, filename) { return readYaml(path.join(directory, filename)); } //# sourceMappingURL=yaml.js.map