UNPKG

3.11 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.digest = exports.BuildCacheManager = void 0;
4const bluebird_lst_1 = require("bluebird-lst");
5const builder_util_1 = require("builder-util");
6const fs_1 = require("builder-util/out/fs");
7const promise_1 = require("builder-util/out/promise");
8const fs_extra_1 = require("fs-extra");
9const promises_1 = require("fs/promises");
10const path = require("path");
11class 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 promise_1.orNullIfFileNotExist(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 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 promises_1.mkdir(this.cacheDir, { recursive: true });
55 await Promise.all([fs_extra_1.writeJson(this.cacheInfoFile, this.cacheInfo), 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}
62exports.BuildCacheManager = BuildCacheManager;
63BuildCacheManager.VERSION = "0";
64async 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 => promises_1.readFile(it))) {
67 hash.update(content);
68 }
69 hash.update(BuildCacheManager.VERSION);
70 return hash.digest("base64");
71}
72exports.digest = digest;
73//# sourceMappingURL=cacheManager.js.map
\No newline at end of file