2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
16 var gulp = require('gulp');
17 var fs = require('fs');
18 var replace = require('gulp-replace');
19 var clean = require('gulp-clean');
20 var tap = require('gulp-tap');
24 * @param options.outDir
25 * @param options.srcDir
26 * @param options.i18nBundles - optional. if given will check the that all keys from js are mapped
29 function i18nTask(options) {
33 function addWord(expr) {
34 var word = expr.substring('i18n(\''.length, expr.length - 1);
36 i18nJson[word] = word;
41 let createBundle = new Promise(function(resolve, reject) {
42 gulp.src(options.srcDir + '**/*.{js,jsx}', {base: './'})
43 .pipe(replace(/i18n\('.*?'/g, addWord))
45 .pipe(gulp.dest('./'))
46 .on('end', function () {
47 console.log('Retrieved keys from static references.');
48 if (options.i18nBundles === undefined) {
49 // creating the file from the words saved during the replace
50 let outfile = options.outDir + '/bundleForStaticKeys.json';
51 fs.writeFile(outfile,JSON.stringify(i18nJson, null, '\t'), function (err) {
57 console.log('Bundle with static keys was created under: ' + outfile);
60 }).on('error', function (err) {
66 if (options.i18nBundles === undefined) {
69 return createBundle.then(() => {
70 new Promise(function (resolve, reject) {
71 gulp.src(options.i18nBundles)
72 .pipe(tap(function (file) {
73 console.log('Checking against bundle: ' + file.path);
74 let bundle = JSON.parse(file.contents.toString());
75 for (entry in i18nJson) {
77 console.log('Missing Key: ' + entry);
82 for (entry in bundle) {
83 console.log('Unused in static files: ' + entry);
86 .pipe(gulp.dest('./'))
87 .on('end', function () {
89 }).on('error', function (err) {
97 module.exports = i18nTask;