@yaml-tools/read-file
TypeScript icon, indicating that this package has built-in type declarations

1.1.3 • Public • Published

@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 to process.cwd().
  • disableIncludes -- when true, readFile won't process +include operators, default false.
  • disableFilePathInjection -- when true, readFile won't inject filePath into each YAML.Node, default false.

Helpers

  • forEachIncludeOperator -- this helper function is the core for the +include operator processing.
  • injectFilePath -- a helper function to inject filePath property into every document node.
  • hasFilePath -- type predicate helper function to check if a YAML.Node has filePath.
  • withFilePath -- returns a YAML.Node & WithFilePath variety if passed value is a YAML.Node with filePath.
  • WithFilePath -- TypeScript interface.

Readme

Keywords

none

Package Sidebar

Install

npm i @yaml-tools/read-file

Weekly Downloads

10

Version

1.1.3

License

ISC

Unpacked Size

11.1 kB

Total Files

9

Last publish

Collaborators

  • alexgorbatchev