Skip to content

Commit

Permalink
added: include parameter is now used to ensure files are merged in th…
Browse files Browse the repository at this point in the history
…e specified order for predictable overwriting of values
  • Loading branch information
MartijnR committed Sep 13, 2017
1 parent 99bfd60 commit e65d504
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 40 deletions.
12 changes: 11 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ module.exports = function(grunt) {
return dest + src + 'translation-combined.json';
},
dest: Path.BUILD_PATH + '/'
},
together_overwrite: {
cwd: 'test/sample/loc',
expand: true,
src: ['*/'],
include: ['**/translation.json', '**/translation-additions.json'],
rename: function(dest, src) {
return dest + src + 'translation-combined-with-overwrite.json';
},
dest: Path.BUILD_PATH + '/'
}
},

Expand All @@ -72,4 +82,4 @@ module.exports = function(grunt) {

// By default, run all tests.
grunt.registerTask('default', ['test']);
};
};
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This multi task supports all the file mapping format Grunt supports. Please read
This required property specifies the *folders* (not files!) the plugin should look for translation json files.

##### include
This optional custom property specifies which files to include. If it is omitted, all json files will be included in the specified `src` folders. Any grunt globbing pattern can be used (array or string).
This optional custom property specifies which files to include and in which order. If it is omitted, all json files will be included in the specified `src` folders. Any grunt globbing pattern can be used (array or string).

##### dest
The destination folder.
Expand Down Expand Up @@ -55,7 +55,7 @@ i18next: {

### Complex Usage Example

This task finds .json files in folders that are direct descendents of _src/languages_, excluding files called "ignore-this.json". The task merges all the files in each src folder separately. The resulting files have file names "translation-combined.json", and are placed under _application/languages_ in their own subfolders that match the src subfolders.
This task finds .json files in folders that are direct descendants of _src/languages_, excluding files called "ignore-this.json". The task merges all the files in each src folder separately. The resulting files have file names "translation-combined.json", and are placed under _application/languages_ in their own subfolders that match the src subfolders.

```js
i18next: {
Expand Down
51 changes: 29 additions & 22 deletions tasks/i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
* Licensed under the MIT license.
* https://github.com/i18next/grunt-i18next/blob/master/LICENSE-MIT
*/
'use strict';
var path = require('path');

module.exports = function(grunt) {
'use strict';

grunt.registerMultiTask('i18next', 'Build locale files.', function() {
var that = this;
var x;
var y;
var z;
var filenames;

// default to all json files
this.data.include = this.data.include || '**/*.json';
Expand All @@ -36,43 +39,47 @@ module.exports = function(grunt) {
return obj1;
};

var iterateTroughFiles = function(abspath, rootdir, subdir, filename) {
var iterateThroughFiles = function(abspath, filename) {
var outputDir;
var outputFile;
var originalFile;
var destFile;
var merged;

if (grunt.file.isMatch(that.data.include, abspath)) {
// if data.rename is not defined the dest has to be a single file
outputFile = (that.data.rename) ? that.files[x].dest : that.data.dest + '/' + filename;
outputDir = outputFile.substring(0, outputFile.lastIndexOf('/'));

// if data.rename is not defined the dest has to be a single file
outputFile = (that.data.rename) ? that.files[x].dest : that.data.dest + '/' + filename;
outputDir = outputFile.substring(0, outputFile.lastIndexOf('/'));

// If output dir doesnt exists, then create it
if (!grunt.file.exists(outputDir)) {
grunt.file.mkdir(outputDir);
}
// If output dir doesnt exists, then create it
if (!grunt.file.exists(outputDir)) {
grunt.file.mkdir(outputDir);
}

originalFile = grunt.file.readJSON(abspath);
originalFile = grunt.file.readJSON(abspath);

// if dest file doenst exist, then just copy it.
if (!grunt.file.exists(outputFile)) {
grunt.file.write(outputFile, JSON.stringify(originalFile));
} else {
// read source file, read dest file. merge them. write it in dest file
destFile = grunt.file.readJSON(outputFile);
// if dest file doesn't exist, then just copy it.
if (!grunt.file.exists(outputFile)) {
grunt.file.write(outputFile, JSON.stringify(originalFile));
} else {
// read source file, read dest file. merge them. write it in dest file
destFile = grunt.file.readJSON(outputFile);

merged = mergeRecursive(destFile, originalFile);
merged = mergeRecursive(destFile, originalFile);

grunt.file.write(outputFile, JSON.stringify(merged));
}
grunt.file.write(outputFile, JSON.stringify(merged));
}
};

for (x = 0; x < this.files.length; x++) {
for (y = 0; y < this.files[x].src.length; y++) {
grunt.file.recurse(this.files[x].src[y], iterateTroughFiles);
// create array of filenames in the order specified by the pattern in the "include" parameter
filenames = grunt.file.expand({
cwd: this.files[x].src[y]
}, this.data.include);

for (z = 0; z < filenames.length; z++) {
iterateThroughFiles(path.join(this.files[x].src[y], filenames[z]), filenames[z]);
}
}
}
});
Expand Down
31 changes: 25 additions & 6 deletions test/i18next_test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var grunt = require('grunt');
'use strict';
//var grunt = require('grunt');
var fs = require('fs');

var path = 'test/sample/languages/';

exports.spreadOut = {
main: function(test) {
'use strict';

test.expect(4);

Expand All @@ -18,11 +18,10 @@ exports.spreadOut = {
},

checkBundleContents: function(test) {
'use strict';

test.expect(4);

var
var
enBundle = JSON.parse(fs.readFileSync(path + 'translation-en.json', 'utf8')),
nestedBundle = JSON.parse(fs.readFileSync(path + 'nested-en.json', 'utf8'));

Expand All @@ -38,7 +37,6 @@ exports.spreadOut = {

exports.together = {
main: function(test) {
'use strict';

test.expect(1);

Expand All @@ -48,7 +46,6 @@ exports.together = {
},

checkBundleContents: function(test) {
'use strict';

test.expect(3);

Expand All @@ -61,3 +58,25 @@ exports.together = {
test.done();
}
};

exports.together_overwrite = {
main: function(test) {

test.expect(1);

test.ok(fs.existsSync(path + 'en/translation-combined-with-overwrite.json'), 'The -combined bundle should exist.');

test.done();
},

checkBundleContents: function(test) {

test.expect(1);

var enBundle = JSON.parse(fs.readFileSync(path + 'en/translation-combined-with-overwrite.json', 'utf8'));

test.equal(enBundle['widget-a'].title, 'Overwritten', 'The title of widget-a should be Overwritten.');

test.done();
}
};
21 changes: 12 additions & 9 deletions test/sample/loc/en/translation-additions.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"widget-b": {
"title": "Activities",
"has_been": "has been ",
"completed": "completed",
"added": "added",
"deleted": "deleted",
"reopened": "reopened"
}
}
"widget-a": {
"title": "Overwritten"
},
"widget-b": {
"title": "Activities",
"has_been": "has been ",
"completed": "completed",
"added": "added",
"deleted": "deleted",
"reopened": "reopened"
}
}

0 comments on commit e65d504

Please sign in to comment.