blob: 85d5c37734189fe817b02e126e1c4f40fe354bf5 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
Michael Landoefa037d2017-02-19 12:57:33 +02002 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
AviZi280f8012017-06-09 02:39:56 +030012 * 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.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016var gulp = require('gulp');
17var fs = require('fs');
18var replace = require('gulp-replace');
19var clean = require('gulp-clean');
Avi Zivb8e2faf2017-07-18 19:45:38 +030020var tap = require('gulp-tap');
Michael Landoefa037d2017-02-19 12:57:33 +020021/**
22 *
23 * @param options
Avi Zivb8e2faf2017-07-18 19:45:38 +030024 * @param options.outDir
25 * @param options.srcDir
26 * @param options.i18nBundles - optional. if given will check the that all keys from js are mapped
Michael Landoefa037d2017-02-19 12:57:33 +020027 *
28 */
29function i18nTask(options) {
30
31 var i18nJson = {};
32
33 function addWord(expr) {
34 var word = expr.substring('i18n(\''.length, expr.length - 1);
Avi Zivb8e2faf2017-07-18 19:45:38 +030035 if (word !== '') {
36 i18nJson[word] = word;
37 }
Michael Landoefa037d2017-02-19 12:57:33 +020038 return expr;
39 }
40
Avi Zivb8e2faf2017-07-18 19:45:38 +030041 let createBundle = new Promise(function(resolve, reject) {
42 gulp.src(options.srcDir + '**/*.{js,jsx}', {base: './'})
43 .pipe(replace(/i18n\('.*?'/g, addWord))
44 .pipe(clean())
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) {
Michael Landoefa037d2017-02-19 12:57:33 +020052 if (err) {
53 reject(err);
54 }
55 else resolve();
56 });
Avi Zivb8e2faf2017-07-18 19:45:38 +030057 console.log('Bundle with static keys was created under: ' + outfile);
58 }
59 resolve();
60 }).on('error', function (err) {
61 reject(err);
Michael Landoefa037d2017-02-19 12:57:33 +020062 });
63 });
Avi Zivb8e2faf2017-07-18 19:45:38 +030064
65
66 if (options.i18nBundles === undefined) {
67 return createBundle;
68 } else {
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) {
76 if (!bundle[entry]) {
77 console.log('Missing Key: ' + entry);
78 } else {
79 delete bundle[entry];
80 }
81 }
82 for (entry in bundle) {
83 console.log('Unused in static files: ' + entry);
84 }
85 }))
86 .pipe(gulp.dest('./'))
87 .on('end', function () {
88 console.log('done');
89 }).on('error', function (err) {
90 reject(err);
91 });
92 });
93 });
94 }
Michael Landoefa037d2017-02-19 12:57:33 +020095}
96
97module.exports = i18nTask;