[SDC] rebase 1710 code
[sdc.git] / openecomp-ui / tools / gulp / deployment / tools / gulp / tasks / i18nUpdate.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, replace, rename, fs, prompt, Promise;
17
18 function mergePromptOptions(options) {
19
20         return new Promise(function(resolve, reject) {
21                 var lang = options.lang;
22                 var warDir = options.warDir;
23                 prompt.start();
24                 prompt.get([
25                         {
26                                 description: 'Enter war directory',
27                                 default: warDir,
28                                 name: 'warDir'
29                         },
30                         {
31                                 description: 'Enter locale.json parent directory name',
32                                 default: lang,
33                                 name: 'lang'
34                         }
35                 ], function (err, result) {
36
37                         if(err) {
38                                 reject(new Error('mergePromptOptions::>\n    ' + err));
39                                 return;
40                         }
41
42                         var warDir = result.warDir;
43                         var lang = result.lang;
44
45                         console.log('\nlocale.json parent directory name> "' + lang + '"');
46                         console.log('war director>"' + warDir + '"');
47
48                         resolve({
49                                 warDir: warDir,
50                                 lang: lang
51                         });
52                 });
53         });
54 }
55
56 function isBundleExists(path) {
57         return new Promise(function(resolve) {
58                 fs.stat(path, function(err) {
59                         resolve(null == err);
60                         /*if null == err then file exists.*/
61                 });
62         });
63 }
64
65 function copyEnglishBundle(enBundlePath, lang) {
66         return new Promise(function(resolve, reject) {
67                 gulp.src(enBundlePath, {base: './'})
68                         .pipe(rename({basename: 'bundle_' + lang}))
69                         .pipe(gulp.dest('./'))
70                         .on('end', function() {
71                                 resolve();
72                         })
73                         .on('error', function(err) {
74                                 reject(new Error('copyEnglishBundle::>\n    ' + err));
75                         });
76         });
77 }
78
79 function getLocaleContent(localePath) {
80
81         return new Promise(function(resolve, reject) {
82                 fs.readFile(localePath, {encoding: 'utf-8'}, function(err,data){
83                         if(err) {
84                                 reject('getLocaleContent()::>\n    ' + err);
85                                 return;
86                         }
87                         resolve(data);
88                 });
89         });
90
91 }
92
93 function extractLocaleJsonContent(localeDataStr) {
94
95         var localeJsonStrI18nStartIdx = localeDataStr.indexOf('I18N_IDENTIFIER_START');
96         var localeJsonStrI18nEndIdx = localeDataStr.indexOf('I18N_IDENTIFIER_END');
97
98         if(-1 === localeJsonStrI18nStartIdx || -1 === localeJsonStrI18nEndIdx) {
99                 return Promise.reject(new Error('extractLocaleJsonContent::> localeDataStr must contain %I18N_IDENTIFIER_START% and %I18N_IDENTIFIER_END%'));
100         }
101
102         var localeJsonStr = localeDataStr.substring(
103                 localeDataStr.indexOf('{', localeJsonStrI18nStartIdx),
104                 localeDataStr.lastIndexOf('}', localeJsonStrI18nEndIdx) + 1
105         );
106
107         try {
108                 JSON.parse(localeJsonStr);
109         } catch(e) {
110                 return Promise.reject(new Error('extractLocaleJsonContent::> localeDataStr must contain a valid json between %I18N_IDENTIFIER_START% and %I18N_IDENTIFIER_END%=>' + e));
111         }
112
113         return Promise.resolve(localeJsonStr);
114 }
115
116 function setBundleLocaleContent(bundlePath, localeJsonStr) {
117         return new Promise(function(resolve, reject) {
118                 gulp.src(bundlePath, {base: './'})
119                         .pipe(replace(/I18N_IDENTIFIER_START(.|[\r\n])*?I18N_IDENTIFIER_END/i, function(expr) {
120                                 return expr.substring(0, expr.indexOf('{')) + localeJsonStr + expr.substring(expr.lastIndexOf('}') + 1);
121                         }))
122                         .pipe(gulp.dest('./'))
123                         .on('end', function() {
124                                 resolve();
125                         })
126                         .on('error', function(err) {
127                                 reject(new Error('setBundleLocaleContent::>\n    ' + err));
128                         });
129         });
130 }
131
132
133 function update(options) {
134
135         gulp = require('gulp');
136         replace = require('gulp-replace');
137         rename = require('gulp-rename');
138         fs = require('fs');
139         prompt = require('prompt');
140         Promise = require('bluebird');
141
142         return mergePromptOptions(options).then(function(mergedOptions) {
143                 var lang = mergedOptions.lang;
144                 var warDir = mergedOptions.warDir;
145
146                 var bundlePath = warDir + '/js/bundle_' + lang + '.js';
147                 var localePath = warDir + '/i18n/' + lang + '/locale.json';
148
149                 return isBundleExists(bundlePath)
150                         .then(function(isBundleExist) {
151                                 var englishBundlePath;
152                                 if(!isBundleExist) {
153                                         englishBundlePath = warDir + '/js/bundle_en.js';
154                                         return copyEnglishBundle(englishBundlePath, lang);
155                                 }
156                         })
157                         .then(getLocaleContent.bind(null, localePath))
158                         .then(extractLocaleJsonContent)
159                         .then(setBundleLocaleContent.bind(null, bundlePath));
160         });
161
162 }
163
164
165
166 module.exports = update;