Skip to content

Instantly share code, notes, and snippets.

@rayfranco
Created April 7, 2014 23:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayfranco/10074082 to your computer and use it in GitHub Desktop.
Save rayfranco/10074082 to your computer and use it in GitHub Desktop.
Convert YML to JSON with gulp

Convert YML to JS with gulp

This was a very straightforward workaround I used in a project to get things done while the assemble team is working on some serious converter gulp-convert.

// Minimal requirements
var map = require('map-stream');
var yaml = require('js-yaml');
var gulp = require('gulp');
// Create JS files from YML files
gulp.task('data', function(){
gulp.src('src/data/*.yml')
.pipe(map(function(file,cb){
if (file.isNull()) return cb(null, file); // pass along
if (file.isStream()) return cb(new Error("Streaming not supported"));
var json;
try {
json = yaml.load(String(file.contents.toString('utf8')));
} catch(e) {
console.log(e);
console.log(json);
}
file.path = gutil.replaceExtension(file.path, '.json');
file.contents = new Buffer(JSON.stringify(json));
cb(null,file);
}))
.pipe(gulp.dest('build/data'));
});
@rchrd2
Copy link

rchrd2 commented Apr 8, 2014

This worked! I just had to add var gutil = require("gulp-util");.

Copy link

ghost commented May 14, 2014

awesome, it saved my day.... thanks

@rayfranco
Copy link
Author

Looks like a plugin just got released by @crissdev
https://github.com/CrissDev/gulp-yaml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment