blob: 38b2a02dcc2ed4c907e7fe4f272b90fc364c7aac [file] [log] [blame]
Michael Landoefa037d2017-02-19 12:57:33 +02001/*-
2 * ============LICENSE_START=======================================================
3 * SDC
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21var gulp = require('gulp');
22var fs = require('fs');
23var replace = require('gulp-replace');
24var clean = require('gulp-clean');
25var mkdirp = require('mkdirp');
26
27/**
28 *
29 * @param options.outputPath
30 * @param options.localesPath
31 * @param options.lang = options.lang
32 *
33 * @returns {string}
34 */
35function composeLocalesDirPath(options) {
36 return options.outputPath + options.localesPath + options.lang;
37}
38
39/**
40 *
41 * @param options.outputPath
42 * @param options.localesPath
43 * @param options.lang
44 *
45 * @returns {string}
46 */
47function composeLocaleFilePath(options) {
48 return composeLocalesDirPath(options) + '/locale.json';
49}
50
51
52/**
53 * @param options
54 * @param options.outputPath
55 * @param options.localesPath
56 * @param options.lang = options.lang
57 *
58 */
59function ensureLocalesDir(options) {
60
61 return new Promise(function (resolve, reject) {
62 mkdirp(composeLocalesDirPath(options), function (err) {
63 if (err) {
64 reject(err);
65 }
66 else {
67 resolve();
68 }
69 });
70 });
71
72}
73
74/**
75 *
76 * @param options
77 * @param options.outputPath
78 * @param options.localesPath
79 * @param options.lang = options.lang
80 *
81 */
82function i18nTask(options) {
83
84 var i18nJson = {};
85
86 function addWord(expr) {
87 var word = expr.substring('i18n(\''.length, expr.length - 1);
88 i18nJson[word] = word;
89 return expr;
90 }
91
92 return ensureLocalesDir(options).then(function () {
93 return new Promise(function(resolve, reject) {
94 gulp.src(options.outputPath + '**/*.js', {base: './'})
95 .pipe(replace(/i18n\('.*?'/g, addWord))
96 .pipe(clean())
97 .pipe(gulp.dest('./'))
98 .on('end', function () {
99
100 var i18nJsonWrapper = { dataWrapperArr: ["I18N_IDENTIFIER_START", i18nJson, "I18N_IDENTIFIER_END"] , i18nDataIdx: 1};
101
102 fs.writeFile(composeLocaleFilePath(options), JSON.stringify(i18nJsonWrapper), function (err) {
103 if (err) {
104 reject(err);
105 }
106 else resolve();
107 });
108 }).on('error', function (err) {
109 reject(err);
110 });
111 });
112 });
113}
114
115module.exports = i18nTask;