1.1.3 • Published 3 years ago
@yaml-tools/read-file v1.1.3
@yaml-tools/read-file
A utility for the yaml package to read YAML
files which can include other YAML files via a special +include operator. Each
included files can also include other files. No circular inclusion check is
done, so don't do it or else... In addition, each YAML.Node gets a new property
called filePath which contains full path to the originating YAML file.
Include file path must be either relative to the YAML file and start with a
dot ./foo or relative to the opts.cwd and start with a tilde ~/foo.
anchors:
map:
+include: ~/getNodePath.yaml
sequence:
- 0
- +include: ../readYAMLFile-sequence.yaml
- 1
another: value
boo: 10import readFile, { hasFilePath, withFilePath } from `@yaml-tools/read-file`;
const doc = readFile('./file.yaml', { cwd: process.cwd() });
// anchors:
// map:
// this:
// is:
// deep:
// foo: value
// bar: next
// sequence:
// - 0
// - a
// - b
// - c
// - 1
// another: value
// boo: 10
hasFilePath(doc.contents!);
// true
withFilePath(doc.contents!).filePath;
// /home/alex/test/file.yamlOptions
opts.cwd-- defaults toprocess.cwd().disableIncludes-- whentrue,readFilewon't process+includeoperators, defaultfalse.disableFilePathInjection-- whentrue,readFilewon't injectfilePathinto eachYAML.Node, defaultfalse.
Helpers
forEachIncludeOperator-- this helper function is the core for the+includeoperator processing.injectFilePath-- a helper function to injectfilePathproperty into every document node.hasFilePath-- type predicate helper function to check if aYAML.NodehasfilePath.withFilePath-- returns aYAML.Node & WithFilePathvariety if passed value is aYAML.NodewithfilePath.WithFilePath-- TypeScript interface.