-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathyaml.ts
More file actions
23 lines (18 loc) · 812 Bytes
/
yaml.ts
File metadata and controls
23 lines (18 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import fs from 'node:fs';
import { type Document, parseDocument, parse as parseYaml, Scalar } from 'yaml';
// eslint-disable-next-line typescript-eslint/no-unnecessary-type-parameters -- convenience generic for callers
export function readYamlFile<T = Record<string, unknown>>(file: string): T {
const content = fs.readFileSync(file, 'utf-8');
return parseYaml(content) as T;
}
export type YamlDocument = Document.Parsed;
export function editYamlFile(file: string, callback: (doc: YamlDocument) => void) {
const content = fs.readFileSync(file, 'utf-8');
const doc = parseDocument(content);
callback(doc);
// prefer single quotes
fs.writeFileSync(file, doc.toString({ singleQuote: true }), 'utf-8');
}
export function scalarString(value: string): Scalar<string> {
return new Scalar(value);
}