项目作者: mikestead

项目描述 :
Generate a yaml document from smaller yaml documents
高级语言: JavaScript
项目地址: git://github.com/mikestead/yaml-fragment.git
创建时间: 2015-12-07T09:39:03Z
项目社区:https://github.com/mikestead/yaml-fragment

开源协议:MIT License

下载


Yaml Fragment 🗂️

Build Status npm version

A tool to construct a yaml document from smaller yaml documents.

Aims to generate a single yaml document formatted exactly as you wrote your referenced fragments,
making it easy to share with humans or machine.

Transforming to json and back to yaml can lose some original formatting, because of this no
transformation is done, instead each fragment is inserted where it’s referenced in a parent document.

OpenAPI spec
generation is a primary use case, but it can be used anywhere you have an unwieldy yaml document
which would be easier to maintain in fragments.

Installation

You can install globally

  1. npm install yaml-fragment -g

Or locally

  1. npm install yaml-fragment --save-dev

Usage

CLI

Here’s an example to generate api.yml from fragments found in a spec directory, with index.yml being the root fragment. Also enables openapi processing defaults.

  1. yaml-fragment -d ./spec -o api.yml --openapi

Options

  1. Usage: cli [options]
  2. Options:
  3. -V, --version output the version number
  4. -d, --rootDir <dir> root directory of your fragments
  5. -i, --indexFile [fragment] index document which references fragments (default: ./index.yml)
  6. -o, --outFile <file> output file of the grouped fragments
  7. --openapi enable openapi fragment processing
  8. -h, --help output usage information

Code

A similar example from the CLI one above.

  1. import { genDocument } from 'yaml-fragment'
  2. genDocument({
  3. rootDir: './spec', // base directory of fragments, with default of index.yml root document
  4. outFile: 'api.yml', // the file to generate
  5. openapi: true // enable openapi processing defaults
  6. }}

Options

  • rootDir: string root directory of your fragments
  • indexFile: string index document which references fragments (default: ./index.yml)
  • outFile: string output file of the grouped fragments
  • openapi: bool enable openapi fragment processing. See OpenAPI section below.
  • indent: string indentation to use in yaml. Defaults to two spaces.
  • formatMapKey function which returns a function, given a directory path, to format a parsed path object to a yaml map key. Defaults to return raw filename.
  • sortCollection function which returns a function, given a directory path, which sorts keys of a collection. Defaults to use localeCompare
  • quoteMapKeyRegex regex to test map keys. If it fails the key will be wrapped in quotes. Defaults to wrap anything starting with a number.
  • relativePaths true if using relative paths in generated collection files. Defaults to true.

Fragments

Start with a base document which includes $refs to local fragments.

Important: To be replaced, $ref paths must begin with./or../.

index.yml

  1. swagger: '2.0'
  2. info:
  3. $ref: ./info.yml
  4. host: petstore.swagger.io
  5. basePath: /v1
  6. schemes:
  7. - http
  8. consumes:
  9. - application/json
  10. produces:
  11. - application/json
  12. paths:
  13. $ref: ./paths/.map.yml
  14. definitions:
  15. $ref: ./definitions/.map.yml

Maps

To generate a yaml map based on each fragment file in a directory, simply target
that directory with a .map.yml file as seen above. This file shouldn’t exist on disk.

During document generation any .map.yml will be populated in memory with references
to each yaml fragment file in the same directory, e.g.

  1. Error:
  2. $ref: ./Error.yml
  3. Pet:
  4. $ref: ./Pet.yml
  5. Pets:
  6. $ref: ./Pets.yml

The key for each defaults to the filename it references, however you can process these names
via the option formatMapKey. For example if the key is an OpenAPI path we can use underscores
in the filename to represent forward slash and then replace these during generation.

  1. options.formatMapKey = (dirPath) => file => file.name.split('_').join('/')

This would convert the filename _pets_{petId} to the key /pets/{petId} and is
one of the defaults applied when the openapi options is present.

Lists

To generate a yaml list based on each fragment file in a directory, simply target
that directory with a .list.yml file (much the same a .map.yml). This file shouldn’t exist on disk.

During document generation any .list.yml will be populated in memory with references
to each yaml fragment file in the same directory, e.g.

  1. - $ref: ./Error.yml
  2. - $ref: ./Pet.yml
  3. - $ref: ./Pets.yml

OpenAPI

By enabling the openapi option you’ll turn on some automatic defaults to process an OpenAPI document.

First, the formatMapKey option is set with a function to format OpenAPI path fragments which sit under a paths directory. This will turn file names like

  1. _pets_{petId}.yml

to map keys like

  1. /pets/{petId}

Second, sortCollection is applied to path keys to place paths with parameters below paths with none. For example

  1. GET /pets/popular
  2. GET /pets/{petId}

Although it’s not at all great design to collide paths like this (really try and avoid it), it can
help on some platforms like V8 (Node) where object iteration is deterministic. For example when adding
Express routes which are priority ordered.

Examples

See here for a more complete
example of fragments.