Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinegoutagny committed Mar 8, 2016
1 parent 4c30f2f commit c854caf
Show file tree
Hide file tree
Showing 13 changed files with 326 additions and 54 deletions.
34 changes: 2 additions & 32 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history
npm-debug.log
tmp
13 changes: 13 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"boss": true,
"eqnull": true,
"node": true
}
73 changes: 73 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* grunt-merge-yaml
* https://github.com/Swaven/grunt-merge-yaml
*
* Copyright (c) 2016 Swaven
* Licensed under the MIT license.
*/

'use strict';

module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
jshint: {
all: [
'Gruntfile.js',
'tasks/*.js',
'<%= nodeunit.tests %>'
],
options: {
jshintrc: '.jshintrc'
}
},

// Before generating any new files, remove any previously-created files.
clean: {
tests: ['tmp']
},

// Configuration to be run (and then tested).
merge_yaml: {
default_options: {
options: {
},
files: {
'tmp/default_options': ['test/fixtures/testing', 'test/fixtures/123']
}
},
custom_options: {
options: {
separator: ': ',
punctuation: ' !!!'
},
files: {
'tmp/custom_options': ['test/fixtures/testing', 'test/fixtures/123']
}
}
},

// Unit tests.
nodeunit: {
tests: ['test/*_test.js']
}

});

// Actually load this plugin's task(s).
grunt.loadTasks('tasks');

// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-nodeunit');

// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
grunt.registerTask('test', ['clean', 'merge_yaml', 'nodeunit']);

// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test']);

};
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

22 changes: 22 additions & 0 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2016 Swaven

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,63 @@
# grunt-merge-yaml
Grunt plugin to merge 2 yaml files

> Merge together 2 yaml files
## Getting Started
This plugin requires Grunt `>=0.4.5`

If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

```shell
npm install grunt-merge-yaml --save-dev
```

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

```js
grunt.loadNpmTasks('grunt-merge-yaml');
```

## The "merge_yaml" task

### Overview
In your project's Gruntfile, add a section named `merge_yaml` to the data object passed into `grunt.initConfig()`.

```js
grunt.initConfig({
merge_yaml: {
options: {
// Task-specific options go here.
},
your_target: {
// Target-specific file lists and/or options go here.
},
},
});
```

### Options

No option available.

### Usage Examples

```js
grunt.initConfig({
merge_yaml: {
options: {},
target: {
base: 'base.yml',
target: 'otherFile.yml',
dest: 'combined.yml'
}
},
});
```


## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).

## Release History

* 2016-03-08: [v0.1.0](https://github.com/Swaven/grunt-merge-yaml/releases/tag/v0.1.0): first release
43 changes: 43 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "grunt-merge-yaml",
"description": "Merge together 2 yaml files",
"version": "0.1.0",
"homepage": "https://github.com/Swaven/grunt-merge-yaml",
"author": {
"name": "Swaven"
},
"repository": {
"type": "git",
"url": "https://github.com/Swaven/grunt-merge-yaml.git"
},
"bugs": {
"url": "https://github.com/Swaven/grunt-merge-yaml/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/Swaven/grunt-merge-yaml/blob/master/LICENSE-MIT"
}
],
"engines": {
"node": ">=4.3.0"
},
"scripts": {
"test": "grunt test"
},
"devDependencies": {
"grunt-contrib-jshint": "^0.9.2",
"grunt-contrib-clean": "^0.5.0",
"grunt-contrib-nodeunit": "^0.3.3",
"grunt": ">=0.4.5"
},
"peerDependencies": {
"grunt": ">=0.4.5"
},
"keywords": [
"gruntplugin"
],
"dependencies": {
"js-yaml": "^3.5.3"
}
}
59 changes: 59 additions & 0 deletions tasks/merge_yaml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* grunt-merge-yaml
* https://github.com/Swaven/grunt-merge-yaml
*
* Copyright (c) 2016 Swaven
* Licensed under the MIT license.
*/

'use strict';

module.exports = function(grunt) {

grunt.registerMultiTask('merge_yaml', 'Merge together 2 yaml files', function() {

/* Creates and return an object as a combination of given objects
* @o1: object from base config
* @o2: object from target config
*/
function explore(o1, o2){
if (typeof o2 === 'undefined') { // ignore undefined values
return o1;
}
else if (o2 === null) { // null values overwrite the base value
return null;
}
else if (Array.isArray(o2)){ // Array overwrite base value
return o2;
}
else if (typeof o2 === 'object'){ // Object is completed
var res = {}, keys = Object.keys(o1);
for (var i = 0; i< keys.length; i++){
var key = keys[i];
res[key] = explore(o1[key], o2[key]);
}
return res;
}
else { // primitive values overwrite base value
return o2;
}
}

// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
});

var fs = require('fs'),
yaml = require('js-yaml'),
baseFile = this.data.base,
target = this.data.target,
dest = this.data.dest,
// file = 'config/config.' + target + '.yml',
baseConfig = grunt.file.readYAML(baseFile),
config = grunt.file.readYAML(target),
mergedConfig = explore(baseConfig, config),
mergedContent = yaml.safeDump(mergedConfig); // Serialize object as YAML

grunt.file.write('config/config.compiled.yml', mergedContent, {encoding: 'utf-8'});
});
};
1 change: 1 addition & 0 deletions test/expected/custom_options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Testing: 1 2 3 !!!
1 change: 1 addition & 0 deletions test/expected/default_options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Testing, 1 2 3.
1 change: 1 addition & 0 deletions test/fixtures/123
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 2 3
1 change: 1 addition & 0 deletions test/fixtures/testing
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Testing
48 changes: 48 additions & 0 deletions test/merge_yaml_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

var grunt = require('grunt');

/*
======== A Handy Little Nodeunit Reference ========
https://github.com/caolan/nodeunit
Test methods:
test.expect(numAssertions)
test.done()
Test assertions:
test.ok(value, [message])
test.equal(actual, expected, [message])
test.notEqual(actual, expected, [message])
test.deepEqual(actual, expected, [message])
test.notDeepEqual(actual, expected, [message])
test.strictEqual(actual, expected, [message])
test.notStrictEqual(actual, expected, [message])
test.throws(block, [error], [message])
test.doesNotThrow(block, [error], [message])
test.ifError(value)
*/

exports.merge_yaml = {
setUp: function(done) {
// setup here if necessary
done();
},
default_options: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/default_options');
var expected = grunt.file.read('test/expected/default_options');
test.equal(actual, expected, 'should describe what the default behavior is.');

test.done();
},
custom_options: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/custom_options');
var expected = grunt.file.read('test/expected/custom_options');
test.equal(actual, expected, 'should describe what the custom option(s) behavior is.');

test.done();
},
};

0 comments on commit c854caf

Please sign in to comment.