Skip to content

Commit e16b90d

Browse files
committed
Refactor
1 parent 44da700 commit e16b90d

1 file changed

Lines changed: 22 additions & 15 deletions

File tree

Gruntfile.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ module.exports = function(grunt) {
44
// Project configuration.
55
grunt.initConfig({
66
pkg: grunt.file.readJSON('package.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'
15+
}
16+
},
717
copy: {
818
main: {
919
files: [
@@ -253,31 +263,28 @@ module.exports = function(grunt) {
253263
done();
254264
}
255265
});
256-
// TODO should probably have config up there ^
257266
grunt.registerTask('generate-lang-json', function() {
258-
var _ = require('underscore');
259-
var fs = require('fs-extra');
260-
var path = require('path');
267+
const fs = require('fs-extra');
268+
const path = require('path');
261269

262-
var langFileExt = '.json';
263-
var backendSrc = path.join('routes', 'lang', '*' + langFileExt);
264-
var frontendSrc = path.join('frontend', 'src', '**', 'lang');
265-
var dest = path.join('temp', 'lang');
270+
const options = this.options();
271+
const backendGlob = path.join(options.src.backend, `*${options.langFileExt}`);
272+
const dest = options.dest;
266273
// load each route lang file
267274
/**
268275
* NOTE there must be a file in routes/lang for the language to be loaded,
269276
* won't work if you've only got lang files in frontend
270277
*/
271-
grunt.file.expand({}, backendSrc).forEach(function(filePath) {
272-
var lang = path.basename(filePath, langFileExt);
273-
var data = _.extend({}, fs.readJSONSync(filePath));
278+
grunt.file.expand({}, path.join(backendGlob)).forEach(backendPath => {
279+
const basename = path.basename(backendPath);
280+
const frontendGlob = path.join(options.src.frontend, basename);
281+
let data = { ...fs.readJSONSync(backendPath) };
274282
// load all matching frontend lang files
275-
grunt.file.expand({}, path.join(frontendSrc, lang + langFileExt)).forEach(function(filePath2) {
276-
// TODO check for duplicates
277-
_.extend(data, fs.readJSONSync(filePath2));
283+
grunt.file.expand({}, frontendGlob).forEach(frontendPath => {
284+
data = { ...data, ...fs.readJSONSync(frontendPath) };
278285
});
279286
fs.ensureDirSync(dest);
280-
fs.writeJSONSync(path.join(dest, lang + langFileExt), data, { spaces: 2 });
287+
fs.writeJSONSync(path.join(dest, basename), data, { spaces: 2 });
281288
});
282289
});
283290

0 commit comments

Comments
 (0)