Fix node filter get methods
[sdc.git] / openecomp-ui / tools / gulp / tasks / i18n.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
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');
21 /**
22  *
23  * @param options
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
27  *
28  */
29 function i18nTask(options) {
30
31         var i18nJson = {};
32
33         function addWord(expr) {
34                 var word = expr.substring('i18n(\''.length, expr.length - 1);
35                 if (word !== '') {
36                         i18nJson[word] = word;
37                 }
38                 return expr;
39         }
40
41         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) {
52                                                 if (err) {
53                                                         reject(err);
54                                                 }
55                                                 else resolve();
56                                         });
57                                         console.log('Bundle with static keys was created under: ' + outfile);
58                                 }
59                                 resolve();
60                         }).on('error', function (err) {
61                         reject(err);
62                 });
63         });
64
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         }
95 }
96
97 module.exports = i18nTask;