cacheManager.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.digest = exports.BuildCacheManager = void 0;
  4. const bluebird_lst_1 = require("bluebird-lst");
  5. const builder_util_1 = require("builder-util");
  6. const fs_1 = require("builder-util/out/fs");
  7. const promise_1 = require("builder-util/out/promise");
  8. const fs_extra_1 = require("fs-extra");
  9. const promises_1 = require("fs/promises");
  10. const path = require("path");
  11. class BuildCacheManager {
  12. constructor(outDir, executableFile, arch) {
  13. this.executableFile = executableFile;
  14. this.cacheInfo = null;
  15. this.newDigest = null;
  16. this.cacheDir = path.join(outDir, ".cache", builder_util_1.Arch[arch]);
  17. this.cacheFile = path.join(this.cacheDir, "app.exe");
  18. this.cacheInfoFile = path.join(this.cacheDir, "info.json");
  19. }
  20. async copyIfValid(digest) {
  21. this.newDigest = digest;
  22. this.cacheInfo = await (0, promise_1.orNullIfFileNotExist)((0, fs_extra_1.readJson)(this.cacheInfoFile));
  23. const oldDigest = this.cacheInfo == null ? null : this.cacheInfo.executableDigest;
  24. if (oldDigest !== digest) {
  25. builder_util_1.log.debug({ oldDigest, newDigest: digest }, "no valid cached executable found");
  26. return false;
  27. }
  28. builder_util_1.log.debug({ cacheFile: this.cacheFile, file: this.executableFile }, `copying cached executable`);
  29. try {
  30. await (0, fs_1.copyFile)(this.cacheFile, this.executableFile, false);
  31. return true;
  32. }
  33. catch (e) {
  34. if (e.code === "ENOENT" || e.code === "ENOTDIR") {
  35. builder_util_1.log.debug({ error: e.code }, "copy cached executable failed");
  36. }
  37. else {
  38. builder_util_1.log.warn({ error: e.stack || e }, `cannot copy cached executable`);
  39. }
  40. }
  41. return false;
  42. }
  43. async save() {
  44. if (this.newDigest == null) {
  45. throw new Error("call copyIfValid before");
  46. }
  47. if (this.cacheInfo == null) {
  48. this.cacheInfo = { executableDigest: this.newDigest };
  49. }
  50. else {
  51. this.cacheInfo.executableDigest = this.newDigest;
  52. }
  53. try {
  54. await (0, promises_1.mkdir)(this.cacheDir, { recursive: true });
  55. await Promise.all([(0, fs_extra_1.writeJson)(this.cacheInfoFile, this.cacheInfo), (0, fs_1.copyFile)(this.executableFile, this.cacheFile, false)]);
  56. }
  57. catch (e) {
  58. builder_util_1.log.warn({ error: e.stack || e }, `cannot save build cache`);
  59. }
  60. }
  61. }
  62. exports.BuildCacheManager = BuildCacheManager;
  63. BuildCacheManager.VERSION = "0";
  64. async function digest(hash, files) {
  65. // do not use pipe - better do bulk file read (https://github.com/yarnpkg/yarn/commit/7a63e0d23c46a4564bc06645caf8a59690f04d01)
  66. for (const content of await bluebird_lst_1.default.map(files, it => (0, promises_1.readFile)(it))) {
  67. hash.update(content);
  68. }
  69. hash.update(BuildCacheManager.VERSION);
  70. return hash.digest("base64");
  71. }
  72. exports.digest = digest;
  73. //# sourceMappingURL=cacheManager.js.map