const path = require('path'); const fs = require('fs'); // 获取当前命令行上下文路径 const currentDirectory = process.cwd(); console.log(`----------------------------`) console.log(`正在处理编译打包前的准备...`) //删除上次打包相关的文件 console.log(`正在删除上次打包相关的文件...`) const distZipPath = path.join(currentDirectory, '/zip/ui-doc.zip'); if(fs.existsSync(distZipPath)) { fs.unlinkSync(distZipPath); } console.log(`准备删除 .docs/.vitepress/dist 文件夹...`) const docs_dist = path.join(currentDirectory, '/.docs/.vitepress/dist'); if(fs.existsSync(docs_dist)) { delallfilerid(docs_dist) fs.rmdirSync(docs_dist) console.log(`.docs/.vitepress/dist 文件夹,删除成功...`) } else { console.log(`.docs/.vitepress/dist 文件夹不存在,无需移除...`) } //删除文件夹下的所有文件 function delallfilerid(fuckpath) { fs.readdirSync(fuckpath).forEach((file, index, arr) => { if (fs.lstatSync(`${fuckpath}/${file}`).isFile()) { fs.unlinkSync(`${fuckpath}/${file}`) }else if(fs.lstatSync(`${fuckpath}/${file}`).isDirectory()){ delallfilerid(`${fuckpath}/${file}`) fs.rmdirSync(`${fuckpath}/${file}`) } }) } console.log(`----------------------------`) //时间格式化 function dateFormat(date) { let format = 'yyyyMMddhhmmss'; let o = { "M+": date.getMonth() + 1, //month "d+": date.getDate(), //day "h+": date.getHours(), //hour "m+": date.getMinutes(), //minute "s+": date.getSeconds(), //second "q+": Math.floor((date.getMonth() + 3) / 3), //quarter "S": date.getMilliseconds() //millisecond } if (/(y+)/.test(format)) { format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); } for (let k in o) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); } } return format; }