UNPKG

6.01 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getTempName = getTempName;
7exports.TmpDir = void 0;
8
9function _bluebirdLst() {
10 const data = _interopRequireDefault(require("bluebird-lst"));
11
12 _bluebirdLst = function () {
13 return data;
14 };
15
16 return data;
17}
18
19function _fsExtraP() {
20 const data = require("fs-extra-p");
21
22 _fsExtraP = function () {
23 return data;
24 };
25
26 return data;
27}
28
29function _lazyVal() {
30 const data = require("lazy-val");
31
32 _lazyVal = function () {
33 return data;
34 };
35
36 return data;
37}
38
39function _os() {
40 const data = require("os");
41
42 _os = function () {
43 return data;
44 };
45
46 return data;
47}
48
49var path = _interopRequireWildcard(require("path"));
50
51function _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
53function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
54
55let tmpFileCounter = 0;
56const tmpDirManagers = new Set(); // add date to avoid use stale temp dir
57
58const tempDirPrefix = `${process.pid.toString(36)}-${Date.now().toString(36)}`;
59
60function getTempName(prefix) {
61 return `${prefix == null ? "" : `${prefix}-`}${tempDirPrefix}-${(tmpFileCounter++).toString(36)}`;
62}
63
64const 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
89function 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 } // each instead of map to avoid fs overload
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
119function handleError(e, file) {
120 if (e.code !== "EPERM" && e.code !== "ENOENT") {
121 // use only console.* instead of our warn on exit (otherwise nodeEmoji can be required on request)
122 console.warn(`Cannot delete temporary "${file}": ${(e.stack || e).toString()}`);
123 }
124}
125
126class TmpDir {
127 constructor(debugName = "") {
128 this.debugName = debugName;
129 this.tempFiles = [];
130 this.registered = false;
131 } // noinspection JSMethodCanBeStatic
132 // noinspection JSUnusedGlobalSymbols
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 // noinspection JSIgnoredPromiseFromCall
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
227exports.TmpDir = TmpDir;
228
229function nullize(s) {
230 return s == null || s.length === 0 ? null : s;
231}
232// __ts-babel@6.0.4
233//# sourceMappingURL=main.js.map
\No newline at end of file