-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYAML.js
More file actions
31 lines (26 loc) · 1.03 KB
/
YAML.js
File metadata and controls
31 lines (26 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { tryRequire } from '@dr-js/core/module/env/tryRequire.js'
import { readText, writeText, readTextSync, writeTextSync } from '@dr-js/core/module/node/fs/File.js'
const GET_YAML = (log = console.warn) => {
const YAML = tryRequire('yaml')
if (YAML) return YAML
const error = new Error('[YAML] failed to load package "yaml"')
log(error)
throw error
}
let CACHED_YAML
const USE_YAML = (YAML) => { CACHED_YAML = YAML }
const __YAML__ = () => {
if (CACHED_YAML === undefined) CACHED_YAML = GET_YAML()
return CACHED_YAML
}
const parseYAML = (string) => __YAML__().parse(string)
const stringifyYAML = (value) => __YAML__().stringify(value)
const readYAML = async (path) => parseYAML(await readText(path))
const readYAMLSync = (path) => parseYAML(readTextSync(path))
const writeYAML = async (path, value) => writeText(path, stringifyYAML(value))
const writeYAMLSync = (path, value) => writeTextSync(path, stringifyYAML(value))
export {
GET_YAML, USE_YAML,
parseYAML, stringifyYAML,
readYAML, readYAMLSync, writeYAML, writeYAMLSync
}