UNPKG

583 BPlain TextView Raw
1import fs from 'fs';
2import yaml from 'js-yaml';
3
4export function parseYaml(path: any) {
5 let config;
6
7 if (fs.existsSync(path)) {
8 try {
9 config = yaml.safeLoad(fs.readFileSync(path, 'utf8'));
10 } catch (e) {
11 throw new Error(e);
12 }
13 }
14
15 return config;
16}
17
18export function safeDump(obj: object, path: any) {
19 let doc;
20 try {
21 doc = yaml.safeDump(obj, {
22 styles: {
23 '!!null': 'canonical'
24 },
25 sortKeys: true,
26 skipInvalid: true
27 });
28 } catch (e) {
29 throw new Error(e);
30 }
31
32 return fs.writeFileSync(path, doc, 'utf-8');
33}