UNPKG

4.28 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.TmpDir = undefined;
7
8var _bluebirdLst;
9
10function _load_bluebirdLst() {
11 return _bluebirdLst = _interopRequireDefault(require("bluebird-lst"));
12}
13
14var _fsExtraP;
15
16function _load_fsExtraP() {
17 return _fsExtraP = require("fs-extra-p");
18}
19
20var _os;
21
22function _load_os() {
23 return _os = require("os");
24}
25
26var _path = _interopRequireWildcard(require("path"));
27
28var _fs;
29
30function _load_fs() {
31 return _fs = require("./fs");
32}
33
34var _log;
35
36function _load_log() {
37 return _log = require("./log");
38}
39
40var _util;
41
42function _load_util() {
43 return _util = require("./util");
44}
45
46function _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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
47
48function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
49
50process.setMaxListeners(30);
51let tempDirPromise;
52let tempDir;
53function getTempDir() {
54 if (tempDirPromise == null) {
55 let promise;
56 const systemTmpDir = process.env.TEST_DIR || (0, (_os || _load_os()).tmpdir)();
57 if ((_fsExtraP || _load_fsExtraP()).mkdtemp == null) {
58 const dir = _path.join(systemTmpDir, (0, (_util || _load_util()).getTempName)("electron-builder"));
59 promise = (0, (_fsExtraP || _load_fsExtraP()).mkdirs)(dir, { mode: 448 }).then(() => dir);
60 } else {
61 promise = (0, (_fsExtraP || _load_fsExtraP()).mkdtemp)(`${_path.join(systemTmpDir, "electron-builder")}-`);
62 }
63 tempDirPromise = promise.then(dir => {
64 tempDir = dir;
65 const cleanup = () => {
66 if (tempDir == null) {
67 return;
68 }
69 tempDir = null;
70 try {
71 (0, (_fsExtraP || _load_fsExtraP()).removeSync)(dir);
72 } catch (e) {
73 if (e.code !== "EPERM") {
74 (0, (_log || _load_log()).warn)(`Cannot delete temporary dir "${dir}": ${(e.stack || e).toString()}`);
75 }
76 }
77 };
78 process.once("beforeExit", () => {
79 if (tempDir == null) {
80 return;
81 }
82 tempDir = null;
83 try {
84 (0, (_fsExtraP || _load_fsExtraP()).remove)(dir);
85 } catch (e) {
86 if (e.code !== "EPERM") {
87 (0, (_log || _load_log()).warn)(`Cannot delete temporary dir "${dir}": ${(e.stack || e).toString()}`);
88 }
89 }
90 });
91 process.on("exit", cleanup);
92 process.on("uncaughtException", cleanup);
93 process.on("SIGINT", cleanup);
94 return dir;
95 });
96 }
97 return tempDirPromise;
98}
99let tmpFileCounter = 0;
100class TmpDir {
101 constructor() {
102 this.tempFiles = [];
103 }
104 getTempFile(suffix) {
105 if (this.tempPrefixPromise == null) {
106 this.tempPrefixPromise = getTempDir().then(it => _path.join(it, (tmpFileCounter++).toString(16)));
107 }
108 return this.tempPrefixPromise.then(it => {
109 const result = `${it}-${(tmpFileCounter++).toString(16)}${suffix.length === 0 || suffix.startsWith(".") ? suffix : `-${suffix}`}`;
110 this.tempFiles.push(result);
111 return result;
112 });
113 }
114 cleanup() {
115 const tempFiles = this.tempFiles;
116 if (tempFiles.length === 0) {
117 return (_bluebirdLst || _load_bluebirdLst()).default.resolve();
118 }
119 this.tempFiles = [];
120 this.tempPrefixPromise = null;
121 return (_bluebirdLst || _load_bluebirdLst()).default.map(tempFiles, it => {
122 (0, (_fsExtraP || _load_fsExtraP()).remove)(it).catch(e => {
123 if (e.code !== "EPERM") {
124 (0, (_log || _load_log()).warn)(`Cannot delete temporary dir "${it}": ${(e.stack || e).toString()}`);
125 }
126 });
127 }, (_fs || _load_fs()).CONCURRENCY);
128 }
129}
130exports.TmpDir = TmpDir; //# sourceMappingURL=tmp.js.map
\No newline at end of file