• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16const _path = require('path');
17const fs = require('fs');
18const { REGEXP_PNG } = require('./lite-enum');
19const iconPath = process.env.iconPath || '';
20const img2bin = require('./lite-image2bin');
21
22/**
23 * Convert picture to bin format.
24 */
25class ImageCoverterPlugin {
26  /**
27   * constructor of the ImageCoverterPlugin class.
28   * @param {String} options The object of build folder.
29   */
30  constructor(options) {
31    this.options = options;
32  }
33  /**
34   * Find all image paths in png、jpg、bmp、jpeg format in the directory.
35   * @param {String} buildPath The path of build folder.
36   * @return {Array} Image path array.
37   */
38  getDir(buildPath) {
39    const rootDirectory = _path.resolve('', buildPath);
40    const pngPathArray = [];
41
42    function traverseAll(rootPath) {
43      fs.readdirSync(rootPath).forEach((file) => {
44        const filePath = _path.join(rootPath, file);
45        const stats = fs.statSync(filePath);
46        if (stats.isFile()) {
47          if (REGEXP_PNG.test(filePath)) {
48              pngPathArray.push(filePath);
49          }
50        } else if (stats.isDirectory()) {
51          traverseAll(filePath);
52        }
53      });
54    }
55
56    traverseAll(rootDirectory);
57    return pngPathArray;
58  }
59  /**
60   * Convert image format asynchronously, return code 0 successfully, otherwise return 1.
61   * @param {Object} compiler API specification, all configuration information of Webpack environment.
62   */
63  apply(compiler) {
64    const buildPath = this.options.build;
65    const getDir = this.getDir;
66    compiler.hooks.done.tap('image coverter', function(compilation, callback) {
67      const pathArray = getDir(buildPath);
68      const writeResult = (content) => {
69        fs.writeFile(_path.resolve(buildPath, 'image_convert_result.txt'), content, (err) => {
70          if (err) {
71            return console.error(err);
72          }
73        });
74      };
75      const totalImageCount = pathArray.length;
76      if (totalImageCount > 0) {
77        const promiseArray = pathArray.map((path) => {
78          return img2bin(path);
79        });
80        Promise.all(promiseArray).then(() => {
81          writeResult('{exitCode:0}');
82        }).catch(() => {
83          writeResult('{exitCode:1}');
84        });
85      } else {
86        writeResult('{exitCode:0}');
87      }
88      if (iconPath !== '') {
89        const iconArray = getDir(iconPath);
90        iconArray.forEach((path) => {
91          img2bin(path);
92        });
93      }
94    });
95  }
96}
97module.exports = ImageCoverterPlugin;
98