1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.getTempName = getTempName;
|
7 | exports.TmpDir = void 0;
|
8 |
|
9 | function _bluebirdLst() {
|
10 | const data = _interopRequireDefault(require("bluebird-lst"));
|
11 |
|
12 | _bluebirdLst = function () {
|
13 | return data;
|
14 | };
|
15 |
|
16 | return data;
|
17 | }
|
18 |
|
19 | function _fsExtraP() {
|
20 | const data = require("fs-extra-p");
|
21 |
|
22 | _fsExtraP = function () {
|
23 | return data;
|
24 | };
|
25 |
|
26 | return data;
|
27 | }
|
28 |
|
29 | function _lazyVal() {
|
30 | const data = require("lazy-val");
|
31 |
|
32 | _lazyVal = function () {
|
33 | return data;
|
34 | };
|
35 |
|
36 | return data;
|
37 | }
|
38 |
|
39 | function _os() {
|
40 | const data = require("os");
|
41 |
|
42 | _os = function () {
|
43 | return data;
|
44 | };
|
45 |
|
46 | return data;
|
47 | }
|
48 |
|
49 | var path = _interopRequireWildcard(require("path"));
|
50 |
|
51 | function _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; } }
|
52 |
|
53 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
54 |
|
55 | let tmpFileCounter = 0;
|
56 | const tmpDirManagers = new Set();
|
57 |
|
58 | const tempDirPrefix = `${process.pid.toString(36)}-${Date.now().toString(36)}`;
|
59 |
|
60 | function getTempName(prefix) {
|
61 | return `${prefix == null ? "" : `${prefix}-`}${tempDirPrefix}-${(tmpFileCounter++).toString(36)}`;
|
62 | }
|
63 |
|
64 | const tempDir = new (_lazyVal().Lazy)(() => {
|
65 | let promise;
|
66 | const systemTmpDir = process.env.TEST_TMP_DIR || process.env.ELECTRON_BUILDER_TMP_DIR || process.env.ELECTRON_BUILDER_TEST_DIR || (0, _os().tmpdir)();
|
67 | const isEnsureRemovedOnExit = process.env.TMP_DIR_MANAGER_ENSURE_REMOVED_ON_EXIT !== "false";
|
68 |
|
69 | if (!isEnsureRemovedOnExit) {
|
70 | promise = Promise.resolve(systemTmpDir);
|
71 | } else if (_fsExtraP().mkdtemp == null) {
|
72 | const dir = path.join(systemTmpDir, getTempName("temp-files"));
|
73 | promise = (0, _fsExtraP().ensureDir)(dir, {
|
74 | mode: 448
|
75 | }).then(() => dir);
|
76 | } else {
|
77 | promise = (0, _fsExtraP().mkdtemp)(`${path.join(systemTmpDir, "temp-dir")}-`);
|
78 | }
|
79 |
|
80 | return promise.then(it => (0, _fsExtraP().realpath)(it)).then(dir => {
|
81 | if (isEnsureRemovedOnExit) {
|
82 | addExitHook(dir);
|
83 | }
|
84 |
|
85 | return dir;
|
86 | });
|
87 | });
|
88 |
|
89 | function addExitHook(dir) {
|
90 | require("async-exit-hook")(callback => {
|
91 | const managers = Array.from(tmpDirManagers);
|
92 | tmpDirManagers.clear();
|
93 |
|
94 | if (callback == null) {
|
95 | for (const manager of managers) {
|
96 | manager.cleanupSync();
|
97 | }
|
98 |
|
99 | try {
|
100 | (0, _fsExtraP().removeSync)(dir);
|
101 | } catch (e) {
|
102 | handleError(e, dir);
|
103 | }
|
104 |
|
105 | return;
|
106 | }
|
107 |
|
108 |
|
109 | _bluebirdLst().default.each(managers, it => it.cleanup()).then(() => (0, _fsExtraP().remove)(dir)).then(() => callback()).catch(e => {
|
110 | try {
|
111 | handleError(e, dir);
|
112 | } finally {
|
113 | callback();
|
114 | }
|
115 | });
|
116 | });
|
117 | }
|
118 |
|
119 | function handleError(e, file) {
|
120 | if (e.code !== "EPERM" && e.code !== "ENOENT") {
|
121 |
|
122 | console.warn(`Cannot delete temporary "${file}": ${(e.stack || e).toString()}`);
|
123 | }
|
124 | }
|
125 |
|
126 | class TmpDir {
|
127 | constructor(debugName = "") {
|
128 | this.debugName = debugName;
|
129 | this.tempFiles = [];
|
130 | this.registered = false;
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 | get rootTempDir() {
|
136 | return tempDir.value;
|
137 | }
|
138 |
|
139 | getTempDir(options) {
|
140 | return this.getTempFile(options, true);
|
141 | }
|
142 |
|
143 | createTempDir(options) {
|
144 | return this.getTempFile(options, true).then(it => (0, _fsExtraP().ensureDir)(it).then(() => it));
|
145 | }
|
146 |
|
147 | getTempFile(options, isDir = false) {
|
148 | return tempDir.value.then(it => {
|
149 | if (!this.registered) {
|
150 | this.registered = true;
|
151 | tmpDirManagers.add(this);
|
152 | }
|
153 |
|
154 | const prefix = nullize(options == null ? null : options.prefix);
|
155 | const suffix = nullize(options == null ? null : options.suffix);
|
156 | const namePrefix = prefix == null ? "" : `${prefix}-`;
|
157 | const nameSuffix = suffix == null ? "" : suffix.startsWith(".") ? suffix : `-${suffix}`;
|
158 | const result = `${it}${path.sep}${namePrefix}${(tmpFileCounter++).toString(36)}${nameSuffix}`;
|
159 | this.tempFiles.push({
|
160 | path: result,
|
161 | isDir,
|
162 | disposer: options == null ? null : options.disposer
|
163 | });
|
164 | return result;
|
165 | });
|
166 | }
|
167 |
|
168 | cleanupSync() {
|
169 | const tempFiles = this.tempFiles;
|
170 | tmpDirManagers.delete(this);
|
171 | this.registered = false;
|
172 |
|
173 | if (tempFiles.length === 0) {
|
174 | return;
|
175 | }
|
176 |
|
177 | this.tempFiles = [];
|
178 |
|
179 | for (const file of tempFiles) {
|
180 | if (file.disposer != null) {
|
181 |
|
182 | file.disposer(file.path);
|
183 | continue;
|
184 | }
|
185 |
|
186 | try {
|
187 | if (file.isDir) {
|
188 | (0, _fsExtraP().removeSync)(file.path);
|
189 | } else {
|
190 | (0, _fsExtraP().unlinkSync)(file.path);
|
191 | }
|
192 | } catch (e) {
|
193 | handleError(e, file.path);
|
194 | }
|
195 | }
|
196 | }
|
197 |
|
198 | cleanup() {
|
199 | const tempFiles = this.tempFiles;
|
200 | tmpDirManagers.delete(this);
|
201 | this.registered = false;
|
202 |
|
203 | if (tempFiles.length === 0) {
|
204 | return Promise.resolve();
|
205 | }
|
206 |
|
207 | this.tempFiles = [];
|
208 | return _bluebirdLst().default.map(tempFiles, it => {
|
209 | if (it.disposer != null) {
|
210 | return it.disposer(it.path);
|
211 | }
|
212 |
|
213 | return (it.isDir ? (0, _fsExtraP().remove)(it.path) : (0, _fsExtraP().unlink)(it.path)).catch(e => {
|
214 | handleError(e, it.path);
|
215 | });
|
216 | }, {
|
217 | concurrency: 8
|
218 | });
|
219 | }
|
220 |
|
221 | toString() {
|
222 | return this.debugName;
|
223 | }
|
224 |
|
225 | }
|
226 |
|
227 | exports.TmpDir = TmpDir;
|
228 |
|
229 | function nullize(s) {
|
230 | return s == null || s.length === 0 ? null : s;
|
231 | }
|
232 |
|
\ | No newline at end of file |