1/*
2 * Copyright (C) 2024 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 childProcess = require('child_process');
18const fs = require('fs');
19
20const staticPath = ['/src/img', '/server'];
21const staticFiles = [
22    '/src/index.html',
23    '/src/base-ui/icon.svg'
24];
25
26function cpDir(sourcePath, targetPath) {
27    fs.readdir(sourcePath, async (err, files) => {
28        if (err) {
29            console.error('unable read dir', err);
30            return;
31        }
32        for (const file of files) {
33            const source = `${sourcePath}/${file}`;
34            const target = `${targetPath}/${file}`;
35            await cpFile(source, target);
36        }
37    });
38}
39
40async function cpFile(source, target) {
41    if (fs.lstatSync(source).isFile()) {
42        const dirPath = path.dirname(target);
43        if (!fs.existsSync(dirPath)) {
44            await fs.promises.mkdir(dirPath, {recursive: true});
45        }
46        await fs.promises.copyFile(source, target);
47    }
48}
49
50function clearDirectory(directoryPath) {
51    let files = [];
52    if (fs.existsSync(directoryPath)) {
53        files = fs.readdirSync(directoryPath);
54        files.forEach((file, index) => {
55            let curPath = directoryPath + '/' + file;
56            if (fs.statSync(curPath).isDirectory()) {
57                clearDirectory(curPath);
58            } else {
59                fs.unlinkSync(curPath);
60            }
61        });
62        fs.rmdirSync(directoryPath);
63    }
64}
65
66
67module.exports = (env, argv) => {
68    const outPath = path.normalize(path.join(__dirname, '/', 'dist'));
69    clearDirectory(outPath);
70    staticPath.forEach((value) => {
71        let pa = path.join(__dirname, value);
72        let distPath;
73        if (value.startsWith('/src')) {
74            distPath = path.join(outPath, value.substring(4, value.length + 1));
75        } else if (value.startsWith('/server')) {
76            distPath = path.join(outPath, value.substring(7, value.length + 1));
77        }
78        cpDir(pa, distPath);
79    });
80    staticFiles.forEach((value) => {
81        let filePath = path.join(__dirname, value);
82        let distFile;
83        if (value.startsWith('/src')) {
84            distFile = path.join(outPath, value.substring(4, value.length + 1));
85        } else if (value.startsWith('/server')) {
86            distFile = path.join(outPath, value.substring(7, value.length + 1));
87        }
88        cpFile(filePath, distFile);
89    });
90
91    return {
92        mode: 'production',
93        entry: './src/index.js',
94        output: {
95            path: path.resolve(__dirname, 'dist'),
96            filename: 'index.js',
97        }
98    };
99};
100