@@ -4,13 +4,14 @@ module.exports = function(grunt) {
44 // Project configuration.
55 grunt . initConfig ( {
66 pkg : grunt . file . readJSON ( 'package.json' ) ,
7- "merge-json" : {
8- en : {
9- src : [
10- 'routes/lang/en-application.json' ,
11- 'frontend/src/**/lang/en.json'
12- ] ,
13- dest : 'routes/lang/en.json'
7+ 'generate-lang-json' : {
8+ options : {
9+ langFileExt : '.json' ,
10+ src : {
11+ backend : 'routes/lang' ,
12+ frontend : 'frontend/src/**/lang'
13+ } ,
14+ dest : 'temp/lang'
1415 }
1516 } ,
1617 copy : {
@@ -87,13 +88,17 @@ module.exports = function(grunt) {
8788 partialRegex : / ^ p a r t _ / ,
8889 partialsPathRegex : / \/ p a r t i a l s \/ /
8990 } ,
90- files : {
91- "frontend/src/templates/templates.js" : [
92- "frontend/src/core/**/*.hbs" ,
93- "frontend/src/modules/**/*.hbs" ,
94- "frontend/src/plugins/**/*.hbs"
95- ]
96- }
91+ files : [
92+ {
93+ follow : true ,
94+ src : [
95+ 'frontend/src/core/**/*.hbs' ,
96+ 'frontend/src/modules/**/*.hbs' ,
97+ 'frontend/src/plugins/**/*.hbs'
98+ ] ,
99+ dest : 'frontend/src/templates/templates.js'
100+ }
101+ ]
97102 }
98103 } ,
99104 requirejs : {
@@ -217,7 +222,10 @@ module.exports = function(grunt) {
217222 var ret = '' ;
218223
219224 for ( var i = 0 , l = src . length ; i < l ; i ++ ) {
220- grunt . file . expand ( { filter : options . filter } , src [ i ] ) . forEach ( function ( lessPath ) {
225+ grunt . file . expand ( {
226+ filter : options . filter ,
227+ follow : true
228+ } , src [ i ] ) . forEach ( function ( lessPath ) {
221229 ret += '@import \'' + path . normalize ( lessPath ) + '\';\n' ;
222230 } ) ;
223231 }
@@ -262,6 +270,30 @@ module.exports = function(grunt) {
262270 done ( ) ;
263271 }
264272 } ) ;
273+ grunt . registerTask ( 'generate-lang-json' , function ( ) {
274+ const fs = require ( 'fs-extra' ) ;
275+ const path = require ( 'path' ) ;
276+
277+ const options = this . options ( ) ;
278+ const backendGlob = path . join ( options . src . backend , `*${ options . langFileExt } ` ) ;
279+ const dest = options . dest ;
280+ // load each route lang file
281+ /**
282+ * NOTE there must be a file in routes/lang for the language to be loaded,
283+ * won't work if you've only got lang files in frontend
284+ */
285+ grunt . file . expand ( { } , path . join ( backendGlob ) ) . forEach ( backendPath => {
286+ const basename = path . basename ( backendPath ) ;
287+ const frontendGlob = path . join ( options . src . frontend , basename ) ;
288+ let data = { ...fs . readJSONSync ( backendPath ) } ;
289+ // load all matching frontend lang files
290+ grunt . file . expand ( { } , frontendGlob ) . forEach ( frontendPath => {
291+ data = { ...data , ...fs . readJSONSync ( frontendPath ) } ;
292+ } ) ;
293+ fs . ensureDirSync ( dest ) ;
294+ fs . writeJSONSync ( path . join ( dest , basename ) , data , { spaces : 2 } ) ;
295+ } ) ;
296+ } ) ;
265297
266298 grunt . registerTask ( 'default' , [ 'build:dev' ] ) ;
267299 grunt . registerTask ( 'test' , [ 'mochaTest' ] ) ;
@@ -283,7 +315,7 @@ module.exports = function(grunt) {
283315 config . isProduction = isProduction ;
284316 grunt . file . write ( configFile , JSON . stringify ( config , null , 2 ) ) ;
285317 // run the task
286- grunt . task . run ( [ 'migration-conf' , 'requireBundle' , 'merge -json' , 'copy' , 'less:' + compilation , 'handlebars' , 'requirejs:' + compilation ] ) ;
318+ grunt . task . run ( [ 'migration-conf' , 'requireBundle' , 'generate-lang -json' , 'copy' , 'less:' + compilation , 'handlebars' , 'requirejs:' + compilation ] ) ;
287319
288320 } catch ( e ) {
289321 grunt . task . run ( [ 'requireBundle' , 'copy' , 'less:' + compilation , 'handlebars' , 'requirejs:' + compilation ] ) ;
0 commit comments