| | |
| | | const fs = require('fs-extra'); |
| | | const path = require('path'); |
| | | const { copyFile, distDir, customerList, moveProjectsToBakDir, logError, restoreProjectDir } = require('./helper'); |
| | | const fs = require('fs-extra'); |
| | | |
| | | const { |
| | | copyFile, |
| | | distDir, |
| | | customerList, |
| | | updateImportGlob, |
| | | logError, |
| | | restoreImportGlob, |
| | | replaceFileContent, |
| | | exit, |
| | | firstCustomerName, |
| | | checkCustomer, |
| | | formatDate, |
| | | logSuccess, |
| | | logWarn, |
| | | rootDir, |
| | | publicDir, |
| | | changeBranch |
| | | } = require('./helper'); |
| | | const { execSync } = require('child_process'); |
| | | if (customerList?.length !== 1) { |
| | | logError(`请正确使用命令 “npm run build customer”(目前只支持一次部署一个)`); |
| | | process.exit(1); // 退出脚本 |
| | | exit(); // 退出脚本 |
| | | } |
| | | // 删除其余项目文件 |
| | | moveProjectsToBakDir('npm run build'); |
| | | try { |
| | | copyFile('npm run build'); |
| | | } catch (error) { |
| | | // 报错时要恢复其余项目文件 |
| | | restoreProjectDir(); |
| | | throw error; |
| | | } |
| | | |
| | | checkCustomer('npm run build', firstCustomerName); |
| | | |
| | | copyFile(); |
| | | |
| | | |
| | | |
| | | // 更改 import.meta.glob |
| | | updateImportGlob(); |
| | | try { |
| | | execSync('vite build', { stdio: 'inherit' }); |
| | | } catch (error) {}finally{ |
| | | // 构建完成之后,恢复项目文件夹 |
| | | restoreProjectDir(); |
| | | } catch (err) { |
| | | logError(err); |
| | | } finally { |
| | | // 恢复文件 |
| | | restoreImportGlob(); |
| | | } |
| | | |
| | | //#region ====================== 修改 dist 文件中 index.html 中的引入 js 的 v 值 ====================== |
| | | |
| | | const htmlFile = path.join(distDir, 'index.html'); |
| | | |
| | | // 读取 index.html 文件内容 |
| | | fs.readFile(htmlFile, 'utf8', (err, data) => { |
| | | if (err) { |
| | | console.error(chalk.red(`读取“${htmlFile}”失败:`, err)); |
| | | return; |
| | | } |
| | | |
| | | replaceFileContent(htmlFile, (data) => { |
| | | // 替换为新的 v 值 |
| | | const currentStamp = new Date().getTime() + ''; |
| | | const newData = data.replace(/(?<=\/static\/config\/.*.js\?v=).*(?=")/g, currentStamp); |
| | | // 将修改后的内容写回 index.html 文件 |
| | | fs.writeFile(htmlFile, newData, 'utf8', (err) => { |
| | | if (err) { |
| | | console.error(chalk.red(`修改“${htmlFile}”中的 v 值失败:`, err)); |
| | | return; |
| | | } |
| | | }); |
| | | return newData; |
| | | }); |
| | | logSuccess(`${formatDate(new Date(), 'HH:MM:SS')} > 🎉🎉🎉【${customerList.join(',')}】项目已成功构建!🎉🎉🎉`); |
| | | |
| | | //#endregion |