claylo / yaml-include

Valid, modular YAML documents with js-yaml. Seriously.
ISC License
39 stars 17 forks source link
cloudformation-yaml js-yaml nodejs openapi yaml

yaml-include

Build Status Coverage Status Dependency Status devDependency Status Commitizen friendly Greenkeeper badge

This package provides support custom tags in a YAML document that facilitate inclusion of external .yaml files, or directories of .yaml files. This functionality is of course frowned upon by the YAML core team, but I find YAML too damn useful as a configuration format to not support inclusions. If you feel the same way, these tags will be helpful to you.

Installation

$ npm install yaml-include

Usage

A very small script can be created to leverage the yaml-include tags. The script would look something like this:

var yaml = require('js-yaml');
var yamlinc = require('yaml-include');
var fs = require('fs');
var p = require('path');

// set the base file for relative includes
yamlinc.setBaseFile(p.join(__dirname, 'yaml-dir', 'base.yaml'));

var src = fs.readFileSync(yamlinc.basefile, 'utf8');

var obj = yaml.load(src, { schema: yamlinc.YAML_INCLUDE_SCHEMA, filename: yamlinc.basefile });

// use your loaded object!

A YAML file using these tags might look like this (this file is in example/swagger/spec.yaml):

swagger: "2.0"
info:
  description: |
    This is a sample server Petstore server.

    [Learn about Swagger](http://swagger.wordnik.com) or join the IRC channel `#swagger` on irc.freenode.net.

    For this sample, you can use the api key `special-key` to test the authorization filters
  version: "1.0.0"
  title: Swagger Petstore
  termsOfService: http://helloreverb.com/terms/
  contact:
    name: apiteam@wordnik.com
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
host: petstore.swagger.wordnik.com
basePath: /v2
schemes:
  - http

paths: !!inc/dir [ 'paths' ]

securityDefinitions: !!inc/file security.yaml

definitions: !!inc/dir [ 'definitions', { recursive: false, allowEmpty: false }]

How It Works

Documents and files discovered during inclusion using the !!inc/dir tag are added to a generated YAML segment. Processing considers directory names and file names (before the .ext) to be keys, and the contents are mapped onto them. The easiest way to explain is with an example.

Given base.yaml looks like this...

name: Include Test
included: !!inc/dir [ 'inc' ]

with the default settings, you'd wind up with the following:

Directory Structure                     Resulting YAML Equivalent
-------------------                     -------------------------
somedir/                                name: Include Test
|-- base.yaml                           included:
`-- inc/                                  /:
    |-- -alt-ignore-prefix.yaml             foo:
    |-- ToLower.yaml                          ... YAML contents of foo.yaml
    |-- _ignored/                           ToLower:
    |   |-- batman.yaml                       ... YAML contents of ToLower.yaml
    |   `-- captain-america.yaml            '-alt-ignore-prefix':
    |-- empty.yaml                            ... YAML contents of -alt-ignore-prefix.yaml
    |-- foo.yaml                          /sub:
    |-- sub/                                StillUpper:
    |   |-- StillUpper.yaml                   ... contents of StillUpper.yaml
    |   |-- _SomeVar/                     '/{alter-ego}':
    |   |   `-- hello.yaml                  Superman:
    |   `-- skip.data                         ... contents of Superman.yaml
    `-- ~alter-ego/
        `-- Superman.yaml

For a bunch of different examples on each of the subdirectories in this example, look in the test/fixtures/options directory.

YAML API

Please note: There's not much an API at all within the JavaScript files. This package extends the js-yaml package, and the descriptions that follow are related to usage of the custom YAML tags this package exposes.

!!inc/dir [ path [, options] ]

Parses path as a single directory pathname. options is a mapping YAML type.

options:

NOTE: if you want to use an !!inc/dir tag within an included file, make sure the inclusion path you enter is relative to the top-level included file.

!!inc/file path

Parses path as a path to a single YAML document. The contents of that document will be a mapping under the key the tag is used on.

NOTE: Files are permitted to include other files or directories. Just make sure any paths within those files are relative to the top-level document.

License

View the LICENSE file (ISC).