Fix node filter get methods
[sdc.git] / openecomp-ui / tools / gulp / tasks / prod.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 'use strict';
17
18 let gulp, replace, Promise, webpack, webpackProductionConfig,cloneDeep, tap;
19 let langs = [];
20
21 /*
22 Runs the webpack build.
23 Will first seach for the resource bundles to see how many languages are supported and then run a build per langauage
24  */
25 function buildWebPackForLanguage(prodConfig, lang) {
26         return new Promise(function (resolve, reject) {
27                 webpack(prodConfig, function (err, stats) {
28                         console.log('[webpack:build ' + prodConfig.output.filename + ']', stats.toString());
29                         if (err || stats.hasErrors()) {
30                                 console.log('webpack:build : Failure!! ' + prodConfig.output.filename + ']');
31                                 reject(err || stats.toJson().errors);
32                         }
33                         else {
34                                 console.log('webpack:build : Done ' + prodConfig.output.filename + ']');
35                                 resolve();
36                         }
37                 });
38         });
39 }
40 /*
41  // this will check in the src directory which language bundles we have and will
42  // create the array to that we can run a webpack build per language afterwards
43  */
44 function getSupportedLanguages(options) {
45         return new Promise((resolve, reject) => {
46                 gulp.src(options.i18nBundles)
47                         .pipe(tap(function(file) {
48                                 let languageStartIndex = file.path.lastIndexOf('i18n') + 5;
49                                 let languageStr =  file.path.indexOf('.json') - languageStartIndex;
50                                 let currentLang = file.path.substr(languageStartIndex, languageStr);
51                                 console.log('Found bundle ' +  file.path + ' for [' + currentLang  + ']');
52                                 langs[currentLang] = file.path;
53                         }))
54                         .pipe(gulp.dest(options.outDir))
55                         .on('end', function () {
56                                 resolve();
57                         })
58                         .on('error', function (e) {
59                                 console.log('getLanguages : Failure!!');
60                                 reject(e);
61                         });
62         });
63 }
64
65 /**
66  * @param options
67  * @param options.outFileName optional <default build>
68  */
69 function prodTask(options) {
70         gulp = require('gulp');
71         replace = require('gulp-replace');
72         Promise = require('bluebird');
73         webpack = require('webpack');
74         cloneDeep = require('lodash/cloneDeep');
75         tap = require('gulp-tap');
76
77
78         // updating webpack for the production build. no need for sourcemaps in this case.
79         webpackProductionConfig = require('../../../webpack.production');
80
81         // get the languages so that we can bulid per language with the correct bundle
82         let getLanguages =getSupportedLanguages(options);
83         // this will run a webpack build per language
84         return getLanguages.then(() => {
85                 let promises = [];
86                 for (var lang in langs) {
87                         let prodConfig = cloneDeep(webpackProductionConfig);
88                         prodConfig.resolve.alias.i18nJson = langs[lang];
89                         prodConfig.output.filename = (options.outFileName || '[name].js').replace(/.js$/, '_' + lang + '.js');
90                         promises.push(buildWebPackForLanguage(prodConfig, lang));
91                 }
92                 return Promise.all(promises);
93         });
94
95 }
96
97 module.exports = prodTask;