Git Product home page Git Product logo

yaml-include's Introduction

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: [email protected]
  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:

  • allowEmpty (default: false) - allow an empty file to appear in the generated result. When set to true, an empty file named empty.yaml will be represented as empty: {}.
  • recursive (default: true) - Specifies whether or not to recurse into subdirectories.
  • excludeFileNameFromNode (default: false) - If true, it will not create an additional node in the object with the file name included.
  • extensions (default: ['.yaml', '.yml']) - Determines file extensions to consider for inclusion.
  • lowerKeys (default: false) - Force all keys gleaned from the directory hierarchy to lower case.
  • variableIndicator (default: '~') - When a file or directory name is prefixed with this character, the representation in the generated output will be wrapped in the variablePrefix and variableSuffix strings.
  • variablePrefix (default: '{') - When representing a variable, this string will precede the variable name.
  • variableSuffix (default: '}') - When representing a variable, this string will follow the variable name.
  • ignoreIndicator (default: '_') - When a file or directory name is prefixed with this character, it and whatever contents it may hold will be ignored. NOTE: A whitelisted file path overrides this setting.
  • ignoreTopLevelDir (default: true) - Specifies if the directory being included use its own name as the initial key.
  • whitelist (default: []) - An array of paths to include regardless of any other settings.
  • blacklist (default: []) - An array of paths to skip regardless of any other settings.
  • excludeTopLevelDirSeparator (default: false) - Specifies if documents in the top level of the include path should be put under a key with an empty dir separator, or be added to the top level of the returned result.
  • pathSeparator (default: '/') - Determines path separator to use when joining subdirectory include paths together.

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).

yaml-include's People

Contributors

claylo avatar greenkeeper[bot] avatar riccardougolini avatar yasuhiroki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

yaml-include's Issues

lists/arrays are converted to indexed object when included

// main.yaml
colors: !!inc/file colors.yaml

// colors.yaml
[red, yellow, green]

Currently this returns something that looks like this:

{colors: {0: "red", 1: "yellow", 2: "green"}}

but I would expect to get something like this:

{colors: ["red","yellow","green"]}

This seems like a bug, correct?
naltatis

Referencing included aliases result in unidentified alias

Consider this file structure:

# in include1.yml
props:
  - &key
    key: value
# in main.yml
name: main

include1: !!inc/file include1.yml

properties:
  - *key

It will throw a YAMLException with: unidentified alias "key", because the key isn't even included.

inc/file inconsistent about base path

I have a directory structure like the following (example demonstrating my actual setup):

config/
-- development.yml
-- production.yml
-- databases.yml
-- assetHosts.yml
tests/
-- test.js

and I want to assign the objects loaded from databases.yml and assetHosts.yml to top-level keys in each of development.yml and production.yml like so:

databases: !! inc/file development.yml
assetHosts: !! inc/file assetHosts.yml

Then I load these files in both webpack from the root of the project, and running unit tests from the tests directory (ie. cd tests && mocha **/*.test.js).

Expected

It would find the files located in its own directory and parse them.

Actual

Webpack reports a file not found error trying to load databases.yml. So I change the directory to be relative to where I'm running webpack from (ie. inc/file config/databases.yml). This works for webpack, but then mocha reports an error when running tests because it can't find config/databases.yml.

Explanation

Since lib/file.js uses process.cwd() as its base, the actual path to the file is dependent on how I run js-yaml. This results in the path behavior being very unpredictable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.