|
1 | 1 | import { tmpdir } from "os"
|
2 |
| -import { remove, mkdirs, removeSync } from "fs-extra-p" |
| 2 | +import { remove, mkdirs, removeSync, mkdtemp } from "fs-extra-p" |
3 | 3 | import * as path from "path"
|
4 | 4 | import { getTempName } from "./util"
|
5 | 5 | import BluebirdPromise from "bluebird-lst-c"
|
6 | 6 | import { warn } from "./log"
|
7 |
| - |
8 |
| -const mkdtemp: any | null = require("fs-extra-p").mkdtemp |
| 7 | +import { all } from "./promise" |
9 | 8 |
|
10 | 9 | process.setMaxListeners(30)
|
11 | 10 |
|
12 |
| -export class TmpDir { |
13 |
| - private tmpFileCounter = 0 |
14 |
| - private tempDirectoryPromise: Promise<string> |
15 |
| - |
16 |
| - private dir: string | null |
| 11 | +let tempDirPromise: Promise<string> | null |
| 12 | +let tempDir: string | null |
17 | 13 |
|
18 |
| - getTempFile(suffix: string | null): Promise<string> { |
19 |
| - if (this.tempDirectoryPromise == null) { |
20 |
| - let promise: Promise<string> |
21 |
| - if (mkdtemp == null) { |
22 |
| - const dir = path.join(tmpdir(), getTempName("electron-builder")) |
23 |
| - promise = mkdirs(dir, {mode: 448}).then(() => dir) |
24 |
| - } |
25 |
| - else { |
26 |
| - promise = mkdtemp(`${path.join(process.env.TEST_DIR || tmpdir(), "electron-builder")}-`) |
27 |
| - } |
| 14 | +function getTempDir() { |
| 15 | + if (tempDirPromise == null) { |
| 16 | + let promise: Promise<string> |
| 17 | + const systemTmpDir = process.env.TEST_DIR || tmpdir() |
| 18 | + if (mkdtemp == null) { |
| 19 | + const dir = path.join(systemTmpDir, getTempName("electron-builder")) |
| 20 | + promise = mkdirs(dir, {mode: 448}).then(() => dir) |
| 21 | + } |
| 22 | + else { |
| 23 | + promise = mkdtemp(`${path.join(systemTmpDir, "electron-builder")}-`) |
| 24 | + } |
28 | 25 |
|
29 |
| - this.tempDirectoryPromise = promise |
30 |
| - .then(dir => { |
31 |
| - this.dir = dir |
32 |
| - const cleanup = () => { |
33 |
| - if (this.dir == null) { |
34 |
| - return |
35 |
| - } |
| 26 | + tempDirPromise = promise |
| 27 | + .then(dir => { |
| 28 | + tempDir = dir |
| 29 | + const cleanup = () => { |
| 30 | + if (tempDir == null) { |
| 31 | + return |
| 32 | + } |
36 | 33 |
|
37 |
| - this.dir = null |
38 |
| - try { |
39 |
| - removeSync(dir) |
40 |
| - } |
41 |
| - catch (e) { |
42 |
| - if (e.code !== "EPERM") { |
43 |
| - warn(`Cannot delete temporary dir "${dir}": ${(e.stack || e).toString()}`) |
44 |
| - } |
| 34 | + tempDir = null |
| 35 | + try { |
| 36 | + removeSync(dir) |
| 37 | + } |
| 38 | + catch (e) { |
| 39 | + if (e.code !== "EPERM") { |
| 40 | + warn(`Cannot delete temporary dir "${dir}": ${(e.stack || e).toString()}`) |
45 | 41 | }
|
46 | 42 | }
|
47 |
| - process.on("exit", cleanup) |
48 |
| - process.on("uncaughtException", cleanup) |
49 |
| - process.on("SIGINT", cleanup) |
50 |
| - return dir |
51 |
| - }) |
| 43 | + } |
| 44 | + process.on("exit", cleanup) |
| 45 | + process.on("uncaughtException", cleanup) |
| 46 | + process.on("SIGINT", cleanup) |
| 47 | + return dir |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + return tempDirPromise |
| 52 | +} |
| 53 | + |
| 54 | +let tmpFileCounter = 0 |
| 55 | + |
| 56 | +export class TmpDir { |
| 57 | + private tempPrefixPromise: Promise<string> | null |
| 58 | + private tempFiles: Array<string> = [] |
| 59 | + |
| 60 | + getTempFile(suffix: string): Promise<string> { |
| 61 | + if (this.tempPrefixPromise == null) { |
| 62 | + this.tempPrefixPromise = getTempDir().then(it => path.join(it, (tmpFileCounter++).toString(16))) |
52 | 63 | }
|
53 | 64 |
|
54 |
| - return this.tempDirectoryPromise |
55 |
| - .then(it => suffix == null ? it : path.join(it, `t-${process.pid.toString(16)}-${(this.tmpFileCounter++).toString(16)}${suffix.startsWith(".") ? suffix : `-${suffix}`}`)) |
| 65 | + return this.tempPrefixPromise |
| 66 | + .then(it => { |
| 67 | + const result = `${it}-${(tmpFileCounter++).toString(16)}${suffix.length === 0 || suffix.startsWith(".") ? suffix : `-${suffix}`}` |
| 68 | + this.tempFiles.push(result) |
| 69 | + return result |
| 70 | + }) |
56 | 71 | }
|
57 | 72 |
|
58 | 73 | cleanup(): Promise<any> {
|
59 |
| - const dir = this.dir |
60 |
| - if (dir == null) { |
| 74 | + const tempFiles = this.tempFiles |
| 75 | + if (tempFiles.length === 0) { |
61 | 76 | return BluebirdPromise.resolve()
|
62 | 77 | }
|
63 | 78 |
|
64 |
| - this.dir = null |
65 |
| - return remove(dir) |
| 79 | + this.tempFiles = [] |
| 80 | + this.tempPrefixPromise = null |
| 81 | + |
| 82 | + return all(tempFiles.map(it => remove(it) |
66 | 83 | .catch(e => {
|
67 |
| - if (e.code === "EPERM") { |
68 |
| - this.dir = dir |
69 |
| - } |
70 |
| - else { |
71 |
| - warn(`Cannot delete temporary dir "${dir}": ${(e.stack || e).toString()}`) |
| 84 | + if (e.code !== "EPERM") { |
| 85 | + warn(`Cannot delete temporary dir "${it}": ${(e.stack || e).toString()}`) |
72 | 86 | }
|
73 | 87 | })
|
| 88 | + )) |
74 | 89 | }
|
75 | 90 | }
|
0 commit comments