wujingjing
2024-09-05 aa8db3c9e5a94053247ce5a750e9ec894b873ad5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
const fs = require('fs-extra');
const { execSync } = require('child_process');
const os = require('os');
const path = require('path');
const chalk = require('chalk');
 
// 获取当前脚本所在的目录
const scriptDir = __dirname;
const rootDir = path.resolve(scriptDir, '..');
const argv2 = process.argv[2];
const customerList = argv2?.split(' ') ?? '';
const publicDir = path.join(rootDir, 'public');
const distDir = path.join(rootDir, 'dist');
const customerListDir = path.join(rootDir, 'customer_list');
const customerProjectListDir = path.join(rootDir, 'src', 'views', 'project');
const firstCustomerName = customerList[0]?.split(':')[0];
/** 公共文件夹,所有客户文件夹共享文件 */
const commonDir = path.join(customerListDir, 'common');
 
const homeDir = os.homedir();
// const deployEnv = process.argv[3];
 
const logColor = (text, color) => {
    console.log(chalk[color](text));
};
 
const logError = (text) => {
    logColor(text, 'red');
};
 
const logSuccess = (text) => {
    logColor(text, 'green');
};
 
const logWarn = (text) => {
    logColor(text, 'yellow');
};
 
/**
 * 退出脚本
 */
const exit = () => {
    process.exit(1); // 退出脚本
};
 
/**
 * 检查文件是否存在
 * @param {*} file
 * @param {*} fileText
 */
const checkFileExist = (file, fileText = '') => {
    // 验证源文件夹是否存在
    if (!fs.existsSync(file)) {
        console.error(chalk.red(`${fileText ? fileText + ' ' : ''}"${file}" 不存在!`));
        exit(); // 退出脚本
    }
};
 
const checkCustomerDirExist = (customer) => {
    const customerDir = path.join(customerListDir, customer);
    checkFileExist(customerDir, '客户文件夹');
};
 
/**
 * 检查命令,是否加上用户名。以及用户文件夹是否存在
 * @param {*} command
 * @param {*} customer
 */
const checkCustomer = (command, customer = firstCustomerName) => {
    if (!customer) {
        console.error(chalk.red(`请正确使用命令 “${command} [customer]” `));
 
        exit(); // 退出脚本
    }
    checkCustomerDirExist(customer);
};
 
const replaceFileContent = (path, callback) => {
    const data = fs.readFileSync(path, 'utf8');
    if (!data) return;
    const newData = callback(data);
    fs.writeFileSync(path, newData, 'utf8');
};
 
const replaceGlobImportPattern = /import.meta.glob\s*\((\s*.*\/views\/\*\*.*)\)/;
const matchAllPattern = '../views/**/*.{vue,tsx}';
 
/**
 * 修改 src/router/backEnd.ts 文件,只导入当前项目文件
 * @param {*} command
 */
const updateImportGlob = () => {
    const backEndFilePath = path.join(rootDir, 'src', 'router', 'backEnd.ts');
    checkFileExist(backEndFilePath, `${backEndFilePath}文件`);
    const subFile = fs.readdirSync(customerListDir);
    const filterSubFile = subFile?.filter((item) => item !== firstCustomerName);
 
    replaceFileContent(backEndFilePath, (data) => {
        const excludeCustomer = filterSubFile && filterSubFile.length > 0 ? `/(${filterSubFile.join('|')})` : '';
        const excludePattern = `!../views/project${excludeCustomer}/**/*`;
        const replaceStr = `import.meta.glob(['${matchAllPattern}', '${excludePattern}'])`;
        const newData = data.replace(replaceGlobImportPattern, replaceStr);
        return newData;
    });
};
 
const restoreImportGlob = () => {
    const backEndFilePath = path.join(rootDir, 'src', 'router', 'backEnd.ts');
    checkFileExist(backEndFilePath, `${backEndFilePath}文件`);
 
    replaceFileContent(backEndFilePath, (data) => {
        const replaceStr = `import.meta.glob('${matchAllPattern}')`;
        const newData = data.replace(replaceGlobImportPattern, replaceStr);
        return newData;
    });
};
 
/**
 * 获取当前日期是第几周
 * @param dateTime 当前传入的日期值
 * @returns 返回第几周数字值
 */
const getWeek = (dateTime) => {
    const temptTime = new Date(dateTime.getTime());
    // 周几
    const weekday = temptTime.getDay() || 7;
    // 周1+5天=周六
    temptTime.setDate(temptTime.getDate() - weekday + 1 + 5);
    let firstDay = new Date(temptTime.getFullYear(), 0, 1);
    const dayOfWeek = firstDay.getDay();
    let spendDay = 1;
    if (dayOfWeek != 0) spendDay = 7 - dayOfWeek + 1;
    firstDay = new Date(temptTime.getFullYear(), 0, 1 + spendDay);
    const d = Math.ceil((temptTime.valueOf() - firstDay.valueOf()) / 86400000);
    const result = Math.ceil(d / 7);
    return result;
};
 
/**
 * 时间日期转换
 * @param date 当前时间,new Date() 格式
 * @param format 需要转换的时间格式字符串,默认值YYYY-mm-dd HH:MM:SS
 * @description format 字符串随意,如 `YYYY-mm、YYYY-mm-dd`
 * @description format 季度:"YYYY-mm-dd HH:MM:SS QQQQ"
 * @description format 星期:"YYYY-mm-dd HH:MM:SS WWW"
 * @description format 几周:"YYYY-mm-dd HH:MM:SS ZZZ"
 * @description format 季度 + 星期 + 几周:"YYYY-mm-dd HH:MM:SS WWW QQQQ ZZZ"
 * @returns 返回拼接后的时间字符串
 */
const formatDate = (date, format = 'YYYY-mm-dd HH:MM:SS') => {
    const we = date.getDay(); // 星期
    const z = getWeek(date); // 周
    const qut = Math.floor((date.getMonth() + 3) / 3).toString(); // 季度
    const opt = {
        'Y+': date.getFullYear().toString(), // 年
        'm+': (date.getMonth() + 1).toString(), // 月(月份从0开始,要+1)
        'd+': date.getDate().toString(), // 日
        'H+': date.getHours().toString(), // 时
        'M+': date.getMinutes().toString(), // 分
        'S+': date.getSeconds().toString(), // 秒
        'q+': qut, // 季度
    };
    // 中文数字 (星期)
    const week = {
        0: '日',
        1: '一',
        2: '二',
        3: '三',
        4: '四',
        5: '五',
        6: '六',
    };
    // 中文数字(季度)
    const quarter = {
        1: '一',
        2: '二',
        3: '三',
        4: '四',
    };
    if (/(W+)/.test(format))
        format = format.replace(RegExp.$1, RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '星期' + week[we] : '周' + week[we]) : week[we]);
    if (/(Q+)/.test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 4 ? '第' + quarter[qut] + '季度' : quarter[qut]);
    if (/(Z+)/.test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 3 ? '第' + z + '周' : z + '');
    for (const k in opt) {
        const r = new RegExp('(' + k + ')').exec(format);
        // 若输入的长度不为1,则前面补零
        if (r) format = format.replace(r[1], RegExp.$1.length == 1 ? opt[k] : opt[k].padStart(RegExp.$1.length, '0'));
    }
    return format;
};
 
const copyAmisEditor = () => {
    // 复制amis-editor 到 public文件夹中
    const amisEditorPath = path.join(rootDir, 'amis-editor');
    const targetPath = path.join(publicDir, 'amis-editor');
    const isExist = fs.pathExistsSync(amisEditorPath);
    if (!isExist) {
        logWarn('amis-editor 不存在!');
    } else {
        const isExist = fs.pathExistsSync(targetPath);
        if (!isExist) {
            fs.mkdirSync(targetPath);
        }
        const res = fs.copySync(amisEditorPath, targetPath);
        res ?? logSuccess('========= 已集成 amis-editor =========');
    }
};
 
/**
 * 复制文件到 public 文件夹中
 * (只能复制一个客户文件配置)
 * @param {*} command
 * @param {*} customer
 */
function copyFile() {
    // 确保 public 文件,不存在则创建
    fs.ensureDirSync(publicDir);
    // 清空 public 文件夹
    fs.emptyDirSync(publicDir);
 
    // 使用 filter 过滤器,排除特定文件
    const filter = (src, dest) => {
        const fileName = path.basename(src);
        // 排除特定文件
        if (fileName === 'deploy.json') {
            return false;
        }
        // 其他文件保留
        return true;
    };
 
    const customerDir = path.join(customerListDir, firstCustomerName);
    // 复制源文件夹中的所有文件到 public 文件夹
    fs.copySync(customerDir, publicDir, { filter });
    fs.copySync(commonDir, publicDir);
    copyAmisEditor();
}
 
/**
 * 上传文件到服务器
 * @param {*} command
 */
const uploadFiles = async () => {
    let customerConfig = {};
    for (let index = 0; index < customerList.length; index++) {
        const item = customerList[index];
        const customerSplit = item.split(':');
        const customerName = customerSplit[0];
        checkCustomerDirExist(customerName);
        const deployJSON = path.join(customerListDir, customerName, 'deploy.json');
        // 验证部署配置文件是否存在
        checkFileExist(deployJSON, '配置文件');
        // 读取 JSON 文件
        const config = await fs.readJson(deployJSON).catch((err) => {
            console.error(`读取配置文件“${deployJSON}”出错:`, err);
            exit();
        });
 
        let deployConfig = null;
        const deployEnv = customerSplit[1];
        if (deployEnv === 'pro') {
            deployConfig = config?.product;
        } else {
            deployConfig = config?.test;
        }
        if (!deployConfig || Object.values(deployConfig).some((item) => !item)) {
            console.error(chalk.red(`${customerName} ${deployEnv} 配置不完整!`));
            exit(); // 退出脚本
        }
        // 缓存部署配置
        customerConfig[item] = deployConfig;
    }
    for (const customerDeployName in customerConfig) {
        if (Object.hasOwnProperty.call(customerConfig, customerDeployName)) {
            const customerName = customerDeployName.split(':')[0];
            const deployConfig = customerConfig[customerDeployName];
            const remoteFolderPath = deployConfig.path;
            // 获取用户临时目录
            const userTempDir = os.tmpdir();
            const timeStamp = new Date().getTime();
            const uploadScriptFile = path.join(userTempDir, `psftpBatchFile${timeStamp}${customerDeployName}`);
            // 构建 sftp 命令
            const sftpCommand = 'psftp';
            // 上传 dist 中的 assets 和 index.html
            let uploadCommand = `put -r  ${path.join(distDir, 'assets')} ${remoteFolderPath + '/assets'}
            put  ${path.join(distDir, 'index.html')} ${remoteFolderPath + '/index.html'}\n`;
 
            const customerFolder = path.join(customerListDir, customerName);
            // 客户文件夹中除 deploy.json 全部上传
            try {
                const contents = fs.readdirSync(customerFolder);
                for (let index = 0; index < contents.length; index++) {
                    const item = contents[index];
                    if (item === 'deploy.json') {
                        continue;
                    }
                    const itemPath = path.join(customerFolder, item);
                    const remotePath = remoteFolderPath + `/${item}`;
                    const stats = fs.statSync(itemPath);
 
                    // 文件夹需要加 -r
                    if (stats.isDirectory()) {
                        uploadCommand += `put -r  ${itemPath} ${remotePath}\n`;
                        // 文件不需要加
                    } else if (stats.isFile()) {
                        uploadCommand += `put  ${itemPath} ${remotePath}\n`;
                    }
                }
            } catch (error) {
                console.error(`读取“${customerFolder}”失败`, error);
            }
 
            try {
                // 批量备份文件夹到本地
                // FIXME: 文件夹如果是中文,会乱码
                // uploadCommand=`get -r /D:/IStation.SQI.WebAirp E:\\139.224.246.185_bak\\IStation.SQI.WebAirp`
                fs.writeFileSync(uploadScriptFile, uploadCommand);
                const sftpArgs = [
                    `${deployConfig.username}@${deployConfig.host} -P ${deployConfig.port} -pw ${deployConfig.password} -b ${uploadScriptFile}`,
                ];
                // 完整的执行命令
                const fullCommand = `${sftpCommand} ${sftpArgs.join(' ')}`;
                logSuccess(`正在上传【${customerName}】项目至服务器【${deployConfig.host}:${remoteFolderPath}】...`);
                execSync(fullCommand, { stdio: ['pipe', 'inherit', 'inherit'] });
            } catch (error) {
                console.error(chalk.red(error));
            } finally {
                if (fs.existsSync(uploadScriptFile)) {
                    fs.unlinkSync(uploadScriptFile);
                }
            }
        }
    }
    logSuccess(`${formatDate(new Date(), 'HH:MM:SS')} > 🎉🎉🎉【${customerList.join(',')}】项目已成功部署!🎉🎉🎉`);
};
 
module.exports = {
    firstCustomerName,
    exit,
    customerList,
    replaceFileContent,
    checkFileExist,
    //#region ====================== 文件路径 ======================
    rootDir,
    scriptDir,
    publicDir,
    distDir,
    customerListDir,
    //#endregion
 
    //#region ====================== 工具函数 ======================
    checkCustomerDirExist,
    //#endregion
    checkCustomer,
    copyFile,
    uploadFiles,
 
    //#region ====================== 打印 ======================
    logError,
    logSuccess,
    logWarn,
    //#endregion
    formatDate,
 
    updateImportGlob,
    restoreImportGlob,
};