If you need to change the content of a YAML file, without losing the key order or field comments, this is the right package for you.
While parsing, yaml-transmute
builds a context (key order + comments) that is returned to the caller and can be later used to stringify the original object that was slightly modified.
This package is based on Eemeli's YAML npm package.
import { parse, stringify, YAMLContext } from "yaml-transmute";
const [obj, ctx]: [unknown, YAMLContext] = parse(`# Comment
foo:
bar: a # Inline comment
baz: b
qux: 0
`);
console.log(obj); // { foo: {bar: "a", baz: "b" }, qux: 0 }
// Create a new object, similar to the original one
const newObj = { foo: { bar: 1 }, qux: true, quux: false };
// Stringify the new object, using the context gathered from `parse`
const stringifiedYaml = stringify(newObj, ctx);
console.log(stringifiedYaml);
// # Comment
// foo:
// bar: 1 # Inline comment
// qux: true
// quux: false