You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.7 KiB

2 years ago
  1. 'use strict';
  2. const fileHepler = require('./../fileHepler');
  3. const basePath = require('./../path');
  4. const clog = require('./../clog');
  5. // const path = require('path');
  6. let cssContentArray = [];
  7. clog('\n\n\n');
  8. clog('---开始拷贝--------------', 'red');
  9. /**
  10. * 替换文件index
  11. * @param content
  12. * @param item
  13. * @returns {*}
  14. */
  15. function replaceHash(content, item){
  16. let cssReg = /((\..*?)\.css)$/;
  17. let contentarr = item.match(cssReg);
  18. let version = `.css?version=${Math.floor(new Date().getTime() / 1000)}`;
  19. let replce = {
  20. oldPath: item,
  21. newPath : item.replace(new RegExp(contentarr[2]),''),
  22. };
  23. cssContentArray.push(replce);
  24. return content.replace(new RegExp(contentarr[1]), version);
  25. }
  26. function init(){
  27. fileHepler.readFile(basePath.indexHtmlFilePath)
  28. .then((res) => {
  29. let cssReg = /\/static\/.*?(\..*?)\.css/ig;
  30. let contentarr = res.match(cssReg);
  31. if (!contentarr) {
  32. clog('文件无更新', 'black');
  33. return;
  34. }
  35. for (let i = 0; i < contentarr.length; i++) {
  36. res = replaceHash(res, contentarr[i]);
  37. }
  38. fileHepler.writeFile(basePath.indexHtmlFilePath, res)
  39. .then(() => {
  40. clog('更新首页样式路径成功!', 'green');
  41. })
  42. .catch();
  43. for (let k = 0; k < cssContentArray.length; k++) {
  44. let olpPath = basePath.cssOutputRoot + cssContentArray[k]['oldPath'];
  45. let newPath = basePath.cssOutputRoot + cssContentArray[k]['newPath'];
  46. fileHepler.renameFile(olpPath, newPath)
  47. .then(() => {
  48. clog(cssContentArray[k], 'green');
  49. })
  50. .catch()
  51. }
  52. });
  53. }
  54. setTimeout(() => {
  55. init()
  56. }, 2000);