build.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const fs = require('fs');
  2. const clean = function (file) {
  3. fs.writeFileSync(file, JSON.stringify({}));
  4. };
  5. const hasBuildInfo = function (file, path) {
  6. const cacheFile = require(file);
  7. return Boolean(cacheFile[path]);
  8. };
  9. const writeBuildInfo = function (file, path, id) {
  10. const cacheFile = require(file);
  11. cacheFile[path] = id;
  12. fs.writeFileSync(file, JSON.stringify(cacheFile));
  13. };
  14. const getCacheFile = function (file, path) {
  15. const cacheFile = require(file);
  16. return cacheFile[path] || 0;
  17. };
  18. const isPwdFile = (path) => {
  19. const cwd = __dirname.split('/').splice(-1, 1).toString();
  20. const pathArray = path.split('/');
  21. const map = new Map();
  22. const reverseMap = new Map();
  23. pathArray.forEach((it, indx) => {
  24. map.set(it, indx);
  25. reverseMap.set(indx, it);
  26. });
  27. if (pathArray.length - 2 == map.get(cwd)) {
  28. return reverseMap.get(pathArray.length - 1).replace(/\.js/, '');
  29. }
  30. return '';
  31. };
  32. module.exports = {
  33. hasBuildInfo,
  34. writeBuildInfo,
  35. getCacheFile,
  36. clean,
  37. isPwdFile,
  38. };