build.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const path = require('path');
  2. const fs = require('fs');
  3. // 获取当前命令行上下文路径
  4. const currentDirectory = process.cwd();
  5. console.log(`----------------------------`)
  6. console.log(`正在处理编译打包前的准备...`)
  7. //删除上次打包相关的文件
  8. console.log(`正在删除上次打包相关的文件...`)
  9. const distZipPath = path.join(currentDirectory, '/zip/ui-doc.zip');
  10. if(fs.existsSync(distZipPath)) {
  11. fs.unlinkSync(distZipPath);
  12. }
  13. console.log(`准备删除 .docs/.vitepress/dist 文件夹...`)
  14. const docs_dist = path.join(currentDirectory, '/.docs/.vitepress/dist');
  15. if(fs.existsSync(docs_dist)) {
  16. delallfilerid(docs_dist)
  17. fs.rmdirSync(docs_dist)
  18. console.log(`.docs/.vitepress/dist 文件夹,删除成功...`)
  19. } else {
  20. console.log(`.docs/.vitepress/dist 文件夹不存在,无需移除...`)
  21. }
  22. //删除文件夹下的所有文件
  23. function delallfilerid(fuckpath) {
  24. fs.readdirSync(fuckpath).forEach((file, index, arr) => {
  25. if (fs.lstatSync(`${fuckpath}/${file}`).isFile()) {
  26. fs.unlinkSync(`${fuckpath}/${file}`)
  27. }else if(fs.lstatSync(`${fuckpath}/${file}`).isDirectory()){
  28. delallfilerid(`${fuckpath}/${file}`)
  29. fs.rmdirSync(`${fuckpath}/${file}`)
  30. }
  31. })
  32. }
  33. console.log(`----------------------------`)
  34. //时间格式化
  35. function dateFormat(date) {
  36. let format = 'yyyyMMddhhmmss';
  37. let o = {
  38. "M+": date.getMonth() + 1, //month
  39. "d+": date.getDate(), //day
  40. "h+": date.getHours(), //hour
  41. "m+": date.getMinutes(), //minute
  42. "s+": date.getSeconds(), //second
  43. "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
  44. "S": date.getMilliseconds() //millisecond
  45. }
  46. if (/(y+)/.test(format)) {
  47. format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  48. }
  49. for (let k in o) {
  50. if (new RegExp("(" + k + ")").test(format)) {
  51. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  52. }
  53. }
  54. return format;
  55. }