grunt-yaml
Advanced tools
Comparing version 0.2.2 to 0.3.0
@@ -11,2 +11,4 @@ /* | ||
var _ = require('lodash'); | ||
module.exports = function(grunt) { | ||
@@ -35,7 +37,2 @@ | ||
default_options: { | ||
options: { | ||
constructors: { | ||
'!include': function () {} // For avoid tag missing error. | ||
} | ||
}, | ||
files: [ | ||
@@ -49,6 +46,13 @@ {expand: true, cwd: 'test/fixtures/', src: ['**/*.yml'], dest: 'tmp/default_options/'} | ||
space: 2, | ||
constructors: { | ||
'!include': function (node, yaml) { | ||
var data = grunt.file.read(node.value, 'utf-8'); | ||
return yaml.load(data); | ||
customTypes: { | ||
'!include scalar': function(value, yamlLoader) { | ||
var data = grunt.file.read(value, 'utf-8'); | ||
return yamlLoader(data); | ||
}, | ||
'!max sequence': function(values) { | ||
return Math.max.apply(null, values); | ||
}, | ||
'!extend mapping': function(value, yamlLoader) { | ||
var baseData = grunt.file.read(value.basePath, 'utf-8'); | ||
return _.extend(yamlLoader(baseData), value.partial); | ||
} | ||
@@ -55,0 +59,0 @@ } |
{ | ||
"name": "grunt-yaml", | ||
"description": "Compiles YAML to JSON.", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"homepage": "https://github.com/shiwano/grunt-yaml", | ||
@@ -32,9 +32,9 @@ "author": { | ||
"devDependencies": { | ||
"grunt-contrib-jshint": "~0.1.1", | ||
"grunt-contrib-clean": "~0.4.0", | ||
"grunt-contrib-nodeunit": "~0.1.2", | ||
"grunt": "~0.4.0" | ||
"grunt-contrib-jshint": "~0.8.0", | ||
"grunt-contrib-clean": "~0.5.0", | ||
"grunt-contrib-nodeunit": "~0.2.2", | ||
"grunt": "~0.4.2" | ||
}, | ||
"peerDependencies": { | ||
"grunt": "~0.4.0" | ||
"grunt": "~0.4.2" | ||
}, | ||
@@ -45,5 +45,6 @@ "keywords": [ | ||
"dependencies": { | ||
"js-yaml": "~1.0.3", | ||
"async": "~0.1.22" | ||
"js-yaml": "~3.0.1", | ||
"async": "~0.2.9", | ||
"lodash": "~2.4.1" | ||
} | ||
} |
@@ -38,6 +38,13 @@ # grunt-yaml [](http://travis-ci.org/shiwano/grunt-yaml) | ||
space: 4, | ||
constructors: { | ||
'!include': function (node, yaml) { | ||
var data = grunt.file.read(node.value, 'utf-8'); | ||
return yaml.load(data); | ||
customTypes: { | ||
'!include scalar': function(value, yamlLoader) { | ||
var data = grunt.file.read(value, 'utf-8'); | ||
return yamlLoader(data); | ||
}, | ||
'!max sequence': function(values) { | ||
return Math.max.apply(null, values); | ||
}, | ||
'!extend mapping': function(value, yamlLoader) { | ||
var baseData = grunt.file.read(value.basePath, 'utf-8'); | ||
return _.extend(yamlLoader(baseData), value.partial); | ||
} | ||
@@ -89,11 +96,13 @@ } | ||
#### options.constructors | ||
#### options.customTypes | ||
Type: `Object` | ||
Default value: `{}` | ||
A Object that defines custom constructors to [js-yaml](https://github.com/nodeca/js-yaml). | ||
A Object that defines custom types to [js-yaml](https://github.com/nodeca/js-yaml). A Object key is a `tag` and `loadKind` pair which is separated with space (e.g. '!include scalar' or '!max sequence', '!extend mapping'). A Object value is a wrapper of loadResolver function which take `value` and `yamlLoader` arguments. | ||
See also js-yaml [document](https://github.com/nodeca/js-yaml/wiki/Custom-types). | ||
#### options.middleware | ||
Type: `function` | ||
Default value: `function(response, json){}` | ||
Type: `Function` | ||
Default value: `function(response, json, src, dest) {}` | ||
@@ -104,3 +113,3 @@ A function which provides you an interface to manipulate the YAML before it becomes JSON, or manipulate the JSON after being stringified. | ||
Type: `Boolean` | ||
Default: `false` | ||
Default value: `false` | ||
@@ -117,3 +126,4 @@ A boolean flag which will prevent grunt-yaml from creating an output file if you would like to just work with the middleware function. | ||
## Release History | ||
* 2014-01-17 v0.2.2 Add src and dest path to middleware | ||
* 2014-01-17 v0.3.0 Introduce `customTypes` option for js-yaml v0.3.X. (Remove `constructors` option) | ||
* 2014-01-17 v0.2.2 Add src and dest path to middleware. | ||
* 2013-06-26 v0.2.1 Add `middleware` and `disableDest` options. | ||
@@ -120,0 +130,0 @@ * 2013-05-10 v0.2.0 Use `files` format. |
@@ -11,30 +11,54 @@ /* | ||
var path = require('path'); | ||
var yaml = require('js-yaml'); | ||
var async = require('async'); | ||
var path = require('path'), | ||
yaml = require('js-yaml'), | ||
async = require('async'), | ||
_ = require('lodash'); | ||
module.exports = function(grunt) { | ||
var yamlSchema = null; | ||
function loadYaml(data) { | ||
return yaml.safeLoad(data, { schema: yamlSchema }); | ||
} | ||
function createYamlSchema(customTypes) { | ||
var yamlTypes = []; | ||
_.each(customTypes, function(resolver, tagAndKindString) { | ||
var tagAndKind = tagAndKindString.split(/\s+/); | ||
var yamlType = new yaml.Type(tagAndKind[0], { | ||
loadKind: tagAndKind[1], | ||
loadResolver: function(state) { | ||
var result = resolver.call(this, state.result, loadYaml); | ||
if (_.isUndefined(result) || _.isFunction(result)) { | ||
return false; | ||
} else { | ||
state.result = result; | ||
return true; | ||
} | ||
} | ||
}); | ||
yamlTypes.push(yamlType); | ||
}); | ||
return yaml.Schema.create(yamlTypes); | ||
} | ||
grunt.registerMultiTask('yaml', 'Compile YAML to JSON', function() { | ||
var options = this.options({ | ||
constructors: {}, | ||
customTypes: {}, | ||
ignored: null, | ||
space: 2, | ||
middleware: function(){}, | ||
middleware: function() {}, | ||
disableDest: false | ||
}); | ||
var taskDone = this.async(); | ||
Object.keys(options.constructors).forEach(function(tag) { | ||
var constructor = options.constructors[tag]; | ||
yamlSchema = createYamlSchema(options.customTypes); | ||
yaml.addConstructor(tag, function(node) { | ||
return constructor.call(this, node, yaml); | ||
}); | ||
}); | ||
async.forEach(this.files, function(filePair, done) { | ||
_.each(this.files, function(filePair) { | ||
filePair.src.forEach(function(src) { | ||
if (grunt.file.isDir(src) || (options.ignored && path.basename(src).match(options.ignored))) { | ||
return done(); | ||
} | ||
if (grunt.file.isDir(src) || (options.ignored && path.basename(src).match(options.ignored))) | ||
return; | ||
@@ -44,23 +68,22 @@ var dest = filePair.dest.replace(/\.ya?ml$/, '.json'); | ||
yaml.loadAll(data, function(result) { | ||
var json = JSON.stringify(result, null, options.space); | ||
if(typeof options.middleware === 'function'){ | ||
options.middleware(result, json, src, dest); | ||
} | ||
if(!options.disableDest){ | ||
grunt.file.write(dest, json); | ||
grunt.log.writeln('Compiled ' + src.cyan + ' -> ' + dest.cyan); | ||
} | ||
done(); | ||
}); | ||
try { | ||
var result = loadYaml(data); | ||
} catch (e) { | ||
grunt.log.error(e); | ||
return false; | ||
} | ||
var json = JSON.stringify(result, null, options.space); | ||
if (_.isFunction(options.middleware)) { | ||
options.middleware(result, json, src, dest); | ||
} | ||
if (!options.disableDest) { | ||
grunt.file.write(dest, json); | ||
grunt.log.writeln('Compiled ' + src.cyan + ' -> ' + dest.cyan); | ||
} | ||
}); | ||
}, function(err) { | ||
if (err) { | ||
grunt.log.error(err); | ||
return taskDone(false); | ||
} | ||
taskDone(); | ||
}); | ||
}); | ||
}; |
@@ -43,7 +43,8 @@ 'use strict'; | ||
custom_options: function(test) { | ||
test.expect(2); | ||
test.ok(fs.existsSync('tmp/custom_options/_partial.json') === false, 'Enabled ignored option.'); | ||
var actual = grunt.file.read('tmp/custom_options/constructor_example.json'); | ||
var expected = grunt.file.read('test/expected/custom_options/constructor_example.json'); | ||
test.equal(actual, expected, 'Enabled constructors option.'); | ||
var actual = grunt.file.read('tmp/custom_options/custom_types_example.json'); | ||
var expected = grunt.file.read('test/expected/custom_options/custom_types_example.json'); | ||
test.equal(actual, expected, 'Enabled customTypes option.'); | ||
@@ -54,2 +55,3 @@ test.done(); | ||
middleware_options: function(test) { | ||
test.expect(3); | ||
test.ok(fs.existsSync('tmp/middleware_options/middleware.json') === false, 'Enabled disableDest option.'); | ||
@@ -56,0 +58,0 @@ |
16808
300
134
4
+ Addedlodash@~2.4.1
+ Addedasync@0.2.10(transitive)
+ Addedjs-yaml@3.0.2(transitive)
- Removedjs-yaml@1.0.3(transitive)
Updatedasync@~0.2.9
Updatedjs-yaml@~3.0.1