AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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. |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | var gulp = require('gulp'); |
| 17 | var fs = require('fs'); |
| 18 | var replace = require('gulp-replace'); |
| 19 | var clean = require('gulp-clean'); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 20 | var tap = require('gulp-tap'); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 21 | /** |
| 22 | * |
| 23 | * @param options |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 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 |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 35 | if (word !== '') { |
| 36 | i18nJson[word] = word; |
| 37 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 38 | return expr; |
| 39 | } |
| 40 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 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) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 52 | if (err) { |
| 53 | reject(err); |
| 54 | } |
| 55 | else resolve(); |
| 56 | }); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 57 | console.log('Bundle with static keys was created under: ' + outfile); |
| 58 | } |
| 59 | resolve(); |
| 60 | }).on('error', function (err) { |
| 61 | reject(err); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 62 | }); |
| 63 | }); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 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 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | module.exports = i18nTask; |