@yaml-tools/read-file
v1.1.3
Published
A utility to read YAML files which can include other YAML files via a special `+include` operator.
Downloads
2
Readme
@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: 10
import 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.yaml
Options
opts.cwd
-- defaults toprocess.cwd()
.disableIncludes
-- whentrue
,readFile
won't process+include
operators, defaultfalse
.disableFilePathInjection
-- whentrue
,readFile
won't injectfilePath
into eachYAML.Node
, defaultfalse
.
Helpers
forEachIncludeOperator
-- this helper function is the core for the+include
operator processing.injectFilePath
-- a helper function to injectfilePath
property into every document node.hasFilePath
-- type predicate helper function to check if aYAML.Node
hasfilePath
.withFilePath
-- returns aYAML.Node & WithFilePath
variety if passed value is aYAML.Node
withfilePath
.WithFilePath
-- TypeScript interface.