vinyl-yaml-data

2.0.1 • Public • Published

vinyl-yaml-data

NPM version Build Status Coverage Status dependencies Status devDependencies Status

Convert vinyl objects of YAML files into plain objects

const gulp = require('gulp');
const {Transform} = require('stream');
const vinylYamlData = require('vinyl-yaml-data');
 
// data/person.yaml: 'name: Bob'
 
gulp.task('default', () => {
  return gukl.src('data/*.yaml')
    .pipe(vinylYamlData());
    .pipe(new Transform({
      objectMode: true,
      transform(obj, enc, cb) {
        obj; //=> {person: {name: 'Bob'}}
        cb();
      })
    );
});

The latest version requires Node v4+. If you're using Node v0.x, use v1.1.0.

Installation

Use npm.

npm install vinyl-yaml-data

API

const vinylYamlData = require('vinyl-yaml-data');

vinylYamlData([options])

options: Object
Return: Object (stream.Transform)

It returns a transform stream. The stream parses file.contents as YAML, and read back an object which contains the parsed data.

The parsed object will be assigned to the specific object path based on the file.path. For example,

file.path object path
foo.yaml foo
foo/bar.yaml foo.bar
foo/bar/baz.qux.yaml foo.bar['baz.qux']
../foo/bar.txt ['..'].foo.bar
foo/../bar/baz.txt bar.baz

Path components included in file.base will be omitted from the object path.

const File = require('vinyl');
const vinylYamlData = require('vinyl-yaml-data');
 
const vinylYamlStream = vinylYamlData();
 
vinylYamlStream.on('data', data => {
  data; //=> {baz: ['Hello', 'world']}
});
 
vinylYamlStream.write(new File({
  cwd: 'foo',
  base: 'bar',
  path: 'bar/baz.yaml',
  contents: new Buffer('[Hello, world]')
}));
 
vinylYamlStream.end();

options

The argument will be directly passed to yaml.safeLoad's option and used on parsing.

Additionally, ext option is available.

options.ext

Type: Boolean
Default: false

By default object paths don't include file extension. true keeps it in the last property name.

const File = require('vinyl');
const vinylYamlData = require('vinyl-yaml-data');
 
vinylYamlData()
.on('data', data => {
  data; //=> {'foo.yml': {a: 'b'}}
})
.end(new File({
  path: 'foo.yml',
  contents: new Buffer('a: b')
}));

Example

Simulate _data directory of Jekyll

deep-extend-stream helps you to merge multiple YAML data into a single object in a stream.

const gulp = require('gulp');
const gulpJade = require('gulp-jade');
 
const vinylYamlData = require('vinyl-yaml-data');
const deepExtend = require('deep-extend-stream');
 
let locals;
 
gulp.task('data', () => {
  locals = {};
 
  return gulp.src('data/**/*.y{,a}ml')
    .pipe(vinylYamlData())
    .pipe(deepExtend(locals));
});
 
gulp.task('views', ['data'], () => {
  return gulp.src('views/**/*.jade')
    .pipe(gulpJade({locals}))
    .pipe(gulp.dest('build'));
});
 
gulp.task('default', ['views']);

License

Copyright (c) 2014 - 2016 Shinnosuke Watanabe

Licensed under the MIT License.

Package Sidebar

Install

npm i vinyl-yaml-data

Weekly Downloads

36

Version

2.0.1

License

MIT

Last publish

Collaborators

  • shinnn