UNPKG

4.38 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.digest = exports.BuildCacheManager = void 0;
7
8function _bluebirdLst() {
9 const data = _interopRequireWildcard(require("bluebird-lst"));
10
11 _bluebirdLst = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _builderUtil() {
19 const data = require("builder-util");
20
21 _builderUtil = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _fs() {
29 const data = require("builder-util/out/fs");
30
31 _fs = function () {
32 return data;
33 };
34
35 return data;
36}
37
38function _promise() {
39 const data = require("builder-util/out/promise");
40
41 _promise = function () {
42 return data;
43 };
44
45 return data;
46}
47
48function _fsExtraP() {
49 const data = require("fs-extra-p");
50
51 _fsExtraP = function () {
52 return data;
53 };
54
55 return data;
56}
57
58var path = _interopRequireWildcard(require("path"));
59
60function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
61
62class BuildCacheManager {
63 constructor(outDir, executableFile, arch) {
64 this.executableFile = executableFile;
65 this.cacheInfo = null;
66 this.newDigest = null;
67 this.cacheDir = path.join(outDir, ".cache", _builderUtil().Arch[arch]);
68 this.cacheFile = path.join(this.cacheDir, "app.exe");
69 this.cacheInfoFile = path.join(this.cacheDir, "info.json");
70 }
71
72 copyIfValid(digest) {
73 var _this = this;
74
75 return (0, _bluebirdLst().coroutine)(function* () {
76 _this.newDigest = digest;
77 _this.cacheInfo = yield (0, _promise().orNullIfFileNotExist)((0, _fsExtraP().readJson)(_this.cacheInfoFile));
78 const oldDigest = _this.cacheInfo == null ? null : _this.cacheInfo.executableDigest;
79
80 if (oldDigest !== digest) {
81 _builderUtil().log.debug({
82 oldDigest,
83 newDigest: digest
84 }, "no valid cached executable found");
85
86 return false;
87 }
88
89 _builderUtil().log.debug({
90 cacheFile: _this.cacheFile,
91 file: _this.executableFile
92 }, `copying cached executable`);
93
94 try {
95 yield (0, _fs().copyFile)(_this.cacheFile, _this.executableFile, false);
96 return true;
97 } catch (e) {
98 if (e.code === "ENOENT" || e.code === "ENOTDIR") {
99 _builderUtil().log.debug({
100 error: e.code
101 }, "copy cached executable failed");
102 } else {
103 _builderUtil().log.warn({
104 error: e.stack || e
105 }, `cannot copy cached executable`);
106 }
107 }
108
109 return false;
110 })();
111 }
112
113 save() {
114 var _this2 = this;
115
116 return (0, _bluebirdLst().coroutine)(function* () {
117 if (_this2.newDigest == null) {
118 throw new Error("call copyIfValid before");
119 }
120
121 if (_this2.cacheInfo == null) {
122 _this2.cacheInfo = {
123 executableDigest: _this2.newDigest
124 };
125 } else {
126 _this2.cacheInfo.executableDigest = _this2.newDigest;
127 }
128
129 try {
130 yield (0, _fsExtraP().ensureDir)(_this2.cacheDir);
131 yield Promise.all([(0, _fsExtraP().writeJson)(_this2.cacheInfoFile, _this2.cacheInfo), (0, _fs().copyFile)(_this2.executableFile, _this2.cacheFile, false)]);
132 } catch (e) {
133 _builderUtil().log.warn({
134 error: e.stack || e
135 }, `cannot save build cache`);
136 }
137 })();
138 }
139
140}
141
142exports.BuildCacheManager = BuildCacheManager;
143BuildCacheManager.VERSION = "0";
144
145let digest = (() => {
146 var _ref = (0, _bluebirdLst().coroutine)(function* (hash, files) {
147 // do not use pipe - better do bulk file read (https://github.com/yarnpkg/yarn/commit/7a63e0d23c46a4564bc06645caf8a59690f04d01)
148 for (const content of yield _bluebirdLst().default.map(files, it => (0, _fsExtraP().readFile)(it))) {
149 hash.update(content);
150 }
151
152 hash.update(BuildCacheManager.VERSION);
153 return hash.digest("base64");
154 });
155
156 return function digest(_x, _x2) {
157 return _ref.apply(this, arguments);
158 };
159})(); exports.digest = digest;
160//# sourceMappingURL=cacheManager.js.map
\No newline at end of file