11module . 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