Skip to content

Commit 6b1d7cf

Browse files
author
Matt Karl
committed
Simplified the JSON creation, added support for CWD
1 parent 2d46b62 commit 6b1d7cf

File tree

4 files changed

+75
-87
lines changed

4 files changed

+75
-87
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3-
- "0.10"
3+
- "0.12"
44
before_install:
55
- npm install -g grunt-cli

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,20 @@ grunt.loadNpmTasks('grunt-concat-json');
3030

3131
Type: `String`|`Array`
3232

33-
The path to the source JSON files or collection of individual files. For instance: `"src/**/*.json"`, which will include all JSON files in the `src` folder.
33+
The path to the source JSON files or collection of individual files. For instance: `"src/**/*.json"`, which will concatenate all JSON files in the `src` folder.
3434

3535
### dest
3636

3737
Type: `String`
3838

39-
The output, concatenated JSON file.
39+
The path to the output concatenated JSON file.
4040

4141
### cwd
4242

4343
Type: `String`
44+
Default: `null`
4445

45-
The base folder to source files from. This will exclude this folder and it's parents from nested layer in the output JSON file. For instance if the `src` property is `"src/**/*.json"`, then `cwd` might be set to `src` to not include `src` as the name of the root property.
46+
The root folder to source files from. This will exclude this folder and it's parents from nested layer representation in the output JSON file. If `cwd` is set, then the root folder does not need to be specified as part of the `src`.
4647

4748
## Task Options
4849

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"email": "matteszklarz@cloudkid.com",
1010
"url": "http://cloudkid.com"
1111
},
12+
"contributors": [{
13+
"name": "Matt Karl",
14+
"email": "matt@cloudkid.com",
15+
"url": "http://cloudkid.com"
16+
}],
1217
"keywords": [
1318
"gruntplugin",
1419
"merge",
@@ -30,9 +35,9 @@
3035
"grunt": "~0.4.5"
3136
},
3237
"dependencies": {
33-
"chalk": "~1.1.0",
34-
"strip-json-comments": "~1.0.2",
35-
"jsonlint": "~1.6.2"
38+
"colors": "^1.1.2",
39+
"jsonlint": "~1.6.2",
40+
"strip-json-comments": "~1.0.2"
3641
},
3742
"engines": {
3843
"node": ">=0.10.0"

tasks/concat-json.js

Lines changed: 62 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
module.exports = function (grunt)
22
{
3-
var chalk = require("chalk");
4-
var stripJsonComments = require("strip-json-comments");
5-
var jsonlint = require("jsonlint");
6-
73
grunt.registerMultiTask("concat-json", "Merge Multiple JSON Files", function ()
84
{
5+
var path = require('path');
6+
var colors = require("colors");
7+
var jsonlint = require("jsonlint");
8+
var stripJsonComments = require("strip-json-comments");
9+
910
// prepare options
1011
var options = this.options(
1112
{
@@ -22,24 +23,27 @@ module.exports = function (grunt)
2223
try
2324
{
2425
// Start with an empty object
25-
var json = {};
26+
var output = {};
2627

2728
// Save paths of unnamed arrays to prevent duplicates
2829
var arrDict = {};
2930

3031
// Add fragments
31-
f.src.forEach(function (src)
32+
f.src.forEach(function(src)
3233
{
34+
// Source file using cwd
35+
if (f.cwd)
36+
{
37+
src = path.join(f.cwd, src);
38+
}
3339

3440
// Merge JSON file into object
3541
if (!grunt.file.exists(src))
3642
{
37-
throw "JSON source file \"" + chalk.red(src) + "\" not found.";
43+
throw "JSON source file \"" + src.red + "\" not found.";
3844
}
3945
else
4046
{
41-
var fragment;
42-
4347
try
4448
{
4549
// Read the raw file
@@ -50,54 +54,32 @@ module.exports = function (grunt)
5054

5155
// Lint the comment-free file.
5256
// If linting errors, terminal will let you know!
53-
var linted = jsonlint.parse(without);
54-
55-
fragment = {
56-
dir: '',
57-
// Attach comment-less JSON
58-
json: linted
59-
};
60-
61-
// Start a top level
62-
var currentDir = json;
57+
var json = jsonlint.parse(without);
6358

6459
// Remove the path to the contianer,
6560
// and the .json extension
66-
var path = src.replace(f.base + '/', '').replace('.json', '');
61+
var cwd = f.base || f.cwd; // backward support
62+
var target = src.replace(cwd + '/', '')
63+
.replace('.json', '')
64+
.split('/');
6765

68-
var test = true;
69-
while (test)
66+
var key, child = output;
67+
while(target.length)
7068
{
71-
test = testDirectory(path, currentDir);
72-
}
69+
key = target.shift();
7370

74-
/**
75-
*
76-
*
77-
* @param {String}
78-
* @param {String}
79-
*/
80-
function testDirectory(_path, _currentDir)
81-
{
82-
var _currentDirIsArray = Array.isArray(_currentDir);
83-
84-
// If the is a slash, we have a parent folder
85-
var firstSlash = _path.indexOf('/');
86-
if (firstSlash > -1)
71+
if (target.length > 0)
8772
{
88-
var dir = _path.slice(0, firstSlash);
89-
if (grunt.util._.has(_currentDir, dir) === false)
73+
if (!child[key])
9074
{
91-
_currentDir[dir] = {};
75+
child[key] = {};
9276
}
93-
94-
currentDir = _currentDir[dir];
95-
path = _path.slice(firstSlash + 1);
96-
return true;
77+
child = child[key];
78+
}
79+
else
80+
{
81+
child[key] = json; // add linted JSON
9782
}
98-
99-
_currentDir[path] = fragment.json;
100-
return false;
10183
}
10284
}
10385
catch (e)
@@ -118,66 +100,66 @@ module.exports = function (grunt)
118100
* Reminder, if an folder-array is nested in a folder-array, only
119101
* the top level folder-array will get a name change, as the children
120102
* arrays will become nameless array index items.
121-
*
122-
* @param {Object} _obj
103+
*
104+
* @method convertCollections
105+
* @param {Object} json
123106
*/
124-
var convertArrayMarkedFolders = function (_obj)
107+
var convertCollections = function(json)
125108
{
126-
if (typeof _obj !== 'object')
109+
if (typeof json !== 'object')
127110
{
128111
return false;
129112
}
130-
131-
for (var key in _obj)
113+
for (var key in json)
132114
{
133-
convertArrayMarkedFolders(_obj[key]);
115+
var contents = json[key];
116+
117+
// Send contents through recursively before doing
118+
// the contents copy.
119+
// The process removes all but the top level key.
120+
convertCollections(contents);
121+
134122
// Check all keys for the folderArrayMarker
135123
var indexOfMarker = key.indexOf(options.folderArrayMarker);
136-
var folderIsArray = indexOfMarker > -1;
137-
if (folderIsArray)
124+
if (indexOfMarker > -1)
138125
{
139-
/* Send contents through recursively before doing
140-
* the contents copy.
141-
* The process removes all but the top level key. */
142-
convertArrayMarkedFolders(_obj[key]);
143-
144-
var contents = _obj[key];
145-
/* Push the values one by one of the original key
146-
* into a new fresh array who's key has the
147-
* folderArrayMarker removed from it */
126+
// Push the values one by one of the original key
127+
// into a new fresh array who's key has the
128+
// folderArrayMarker removed from it
148129
var keyWithoutMarker = key.slice(0, indexOfMarker);
149-
_obj[keyWithoutMarker] = [];
130+
json[keyWithoutMarker] = [];
131+
150132
for (var k in contents)
151133
{
152-
_obj[keyWithoutMarker].push(contents[k]);
134+
json[keyWithoutMarker].push(contents[k]);
153135
}
154-
155136
// Delete the original marker key.
156-
delete _obj[key];
137+
delete json[key];
157138
}
158139
}
159140
};
160141

161-
convertArrayMarkedFolders(json);
142+
// Fixed folders marked as arrays
143+
convertCollections(output);
162144

163145
// Write object as new JSON
164-
grunt.log.debug(
165-
"writing JSON destination file \"" +
166-
chalk.green(f.dest) +
167-
"\"");
146+
grunt.log.debug("Writing JSON destination file \"" + f.dest.green + "\"");
168147

169148
grunt.file.write(
170149
f.dest,
171-
JSON.stringify(json, options.replacer, options.space));
172-
173-
grunt.log.writeln("File \"" +
174-
chalk.green(f.dest) +
175-
"\" created.");
150+
JSON.stringify(
151+
output,
152+
options.replacer,
153+
options.space
154+
)
155+
);
156+
157+
grunt.log.writeln("File \"" + f.dest.green + "\" created.");
176158
}
177159
catch (e)
178160
{
179161
grunt.fail.warn(e);
180162
}
181163
});
182164
});
183-
};
165+
};

0 commit comments

Comments
 (0)