From 2e7ad8dc705f7da98ce9129010027c4d4b94b5ef Mon Sep 17 00:00:00 2001 From: Ryan Kelly Date: Wed, 22 Jun 2016 14:23:35 +1000 Subject: [PATCH] chore(build): Refactor gruntfile into individual task files. --- Gruntfile.js | 90 +-------------------------------- {tasks => grunttasks}/eslint.js | 2 +- grunttasks/l10n-extract.js | 74 +++++++++++++++++++++++++++ grunttasks/templates.js | 36 +++++++++++++ 4 files changed, 113 insertions(+), 89 deletions(-) rename {tasks => grunttasks}/eslint.js (78%) create mode 100644 grunttasks/l10n-extract.js create mode 100644 grunttasks/templates.js diff --git a/Gruntfile.js b/Gruntfile.js index 726245de..b46fa543 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -4,103 +4,17 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -const path = require('path') -const extract = require('jsxgettext-recursive') - -// # Globbing -// for performance reasons we're only matching one level down: -// 'test/spec/{,*/}*.js' -// use this if you want to recursively match all subfolders: -// 'test/spec/**/*.js' module.exports = function (grunt) { 'use strict' require('load-grunt-tasks')(grunt) grunt.initConfig({ - copy: { - strings: { - files: [{ - expand: true, - flatten: true, - cwd: path.join(__dirname, 'node_modules', 'fxa-content-server-l10n', 'locale', 'templates', 'LC_MESSAGES'), - dest: __dirname, - src: [ - 'server.pot' - ] - }] - } - }, - nunjucks: { - options: { - tags: { - blockStart: '<%', - blockEnd: '%>', - variableStart: '<$', - variableEnd: '$>', - commentStart: '<#', - commentEnd: '#>' - }, - data: {} - }, - render: { - files: [ - { - expand: true, - cwd: 'partials/', - src: '*.html', - dest: 'templates/', - ext: '.html' - } - ] - } - } + pkg: grunt.file.readJSON('package.json') }) - grunt.registerTask('l10n-extract', 'Extract strings from templates for localization.', function () { - var done = this.async() - - var walker = extract({ - 'input-dir': path.join(__dirname, 'templates'), - 'output-dir': __dirname, - 'output': 'server.pot', - 'join-existing': true, - 'keyword': ['t'], - parsers: { - '.txt': 'handlebars', - '.html': 'handlebars' - } - }) - - walker.on('end', function () { - var jsWalker = extract({ - 'input-dir': __dirname, - /* node_modules and test should not contain any strings - * Gruntfile causes an error and should contain no strings - * bin/server.js extracts "/", so it is excluded. - */ - exclude: /(node_modules|test|Gruntfile|bin|scripts)/, - 'output-dir': __dirname, - 'output': 'server.pot', - 'join-existing': true, - 'keyword': ['gettext'], - parsers: { - '.js': 'javascript' - } - }) - - jsWalker.on('end', function () { - done() - }) - }) - }) - - // load local Grunt tasks - grunt.loadTasks('tasks') + grunt.loadTasks('grunttasks') grunt.registerTask('lint', 'Alias for eslint tasks', ['eslint']) - grunt.registerTask('templates', 'Alias for the template task', ['nunjucks']) - grunt.registerTask('default', [ 'templates', 'copy:strings', 'l10n-extract' ]) - } diff --git a/tasks/eslint.js b/grunttasks/eslint.js similarity index 78% rename from tasks/eslint.js rename to grunttasks/eslint.js index 4fcf8352..ed009b63 100644 --- a/tasks/eslint.js +++ b/grunttasks/eslint.js @@ -10,7 +10,7 @@ module.exports = function (grunt) { eslintrc: '.eslintrc' }, app: [ - '*.js', 'bin/*.js', 'tasks/*.js', 'templates/*.js', 'test/**/*.js', 'scripts/**/*.js' + '*.js', 'bin/*.js', 'grunttasks/*.js', 'templates/*.js', 'test/**/*.js', 'scripts/**/*.js' ] }) } diff --git a/grunttasks/l10n-extract.js b/grunttasks/l10n-extract.js new file mode 100644 index 00000000..274521c2 --- /dev/null +++ b/grunttasks/l10n-extract.js @@ -0,0 +1,74 @@ +#!/usr/bin/env node + +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +const path = require('path') +const extract = require('jsxgettext-recursive') + +const pkgroot = path.dirname(__dirname) + +module.exports = function (grunt) { + 'use strict' + + grunt.config('copy', { + strings: { + files: [{ + expand: true, + flatten: true, + cwd: path.join(pkgroot, 'node_modules', 'fxa-content-server-l10n', 'locale', 'templates', 'LC_MESSAGES'), + dest: pkgroot, + src: [ + 'server.pot' + ] + }] + } + }) + + grunt.registerTask('l10n-extract', 'Extract strings from templates for localization.', function () { + var done = this.async() + + var walker = extract({ + 'input-dir': path.join(pkgroot, 'templates'), + 'output-dir': pkgroot, + 'output': 'server.pot', + 'join-existing': true, + 'keyword': ['t'], + parsers: { + '.txt': 'handlebars', + '.html': 'handlebars' + } + }) + + walker.on('end', function () { + var jsWalker = extract({ + 'input-dir': pkgroot, + /* node_modules and test should not contain any strings + * Gruntfile causes an error and should contain no strings + * bin/server.js extracts "/", so it is excluded. + */ + exclude: /(node_modules|test|Gruntfile|grunttasks|bin|scripts|\.git|config)/, + 'output-dir': pkgroot, + 'output': 'server.pot', + 'join-existing': true, + 'keyword': ['gettext'], + parsers: { + '.js': 'javascript' + } + }) + + jsWalker.on('end', function () { + done() + }) + }) + }) + + // load local Grunt tasks + + grunt.registerTask('lint', 'Alias for eslint tasks', ['eslint']) + grunt.registerTask('templates', 'Alias for the template task', ['nunjucks']) + + grunt.registerTask('default', [ 'templates', 'copy:strings', 'l10n-extract' ]) + +} diff --git a/grunttasks/templates.js b/grunttasks/templates.js new file mode 100644 index 00000000..0c90241e --- /dev/null +++ b/grunttasks/templates.js @@ -0,0 +1,36 @@ +#!/usr/bin/env node + +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +module.exports = function (grunt) { + 'use strict' + + grunt.config('nunjucks', { + options: { + tags: { + blockStart: '<%', + blockEnd: '%>', + variableStart: '<$', + variableEnd: '$>', + commentStart: '<#', + commentEnd: '#>' + }, + data: {} + }, + render: { + files: [ + { + expand: true, + cwd: 'partials/', + src: '*.html', + dest: 'templates/', + ext: '.html' + } + ] + } + }) + + grunt.registerTask('templates', 'Alias for the template task', ['nunjucks']) +}