-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathtemp.ts
More file actions
116 lines (103 loc) · 3.1 KB
/
temp.ts
File metadata and controls
116 lines (103 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* temp.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { debug, info } from "../deno_ral/log.ts";
import { join } from "../deno_ral/path.ts";
import { ensureDirSync, existsSync } from "../deno_ral/fs.ts";
import { normalizePath, removeIfExists, safeRemoveIfExists } from "./path.ts";
import { TempContext } from "./temp-types.ts";
import { isWindows } from "../deno_ral/platform.ts";
export type { TempContext } from "./temp-types.ts";
let tempDir: string | undefined;
let tempContext: TempContext | undefined;
export function initSessionTempDir() {
// if TMPDIR exists and has been removed (sometimes occurs for stale TMPDIR values
// in resurrected RStudio terminal sessions) then try to re-create it
const tmpEnv = Deno.env.get("TMPDIR");
if (tmpEnv) {
try {
if (!existsSync(tmpEnv)) {
ensureDirSync(tmpEnv);
}
} catch (err) {
if (!(err instanceof Error)) throw err;
if (err.message) {
debug("Error attempting to create TMPDIR: " + err.message);
}
}
}
tempDir = Deno.makeTempDirSync({ prefix: "quarto-session" });
}
export function cleanupSessionTempDir() {
if (tempContext) {
tempContext.cleanup();
tempContext = undefined;
}
if (tempDir) {
removeIfExists(tempDir);
tempDir = undefined;
}
}
export function globalTempContext() {
if (tempContext === undefined) {
tempContext = createTempContext();
}
return tempContext;
}
export function createTempContext(options?: Deno.MakeTempOptions): TempContext {
if (options?.dir) {
ensureDirSync(options?.dir);
}
let dir: string | undefined = Deno.makeTempDirSync({
dir: tempDir,
...options,
});
const tempContextCleanupHandlers: VoidFunction[] = [];
const result: TempContext = {
baseDir: dir,
createFileFromString: (content: string, options?: Deno.MakeTempOptions) => {
const file = result.createFile(options);
Deno.writeTextFileSync(file, content);
return file;
},
createFile: (options?: Deno.MakeTempOptions) => {
return Deno.makeTempFileSync({ ...options, dir });
},
createDir: (options?: Deno.MakeTempOptions) => {
return Deno.makeTempDirSync({ ...options, dir });
},
cleanup: () => {
if (dir) {
// Not using .reverse() to not mutate the original array
for (let i = tempContextCleanupHandlers.length - 1; i >= 0; i--) {
const handler = tempContextCleanupHandlers[i];
try {
handler();
} catch (error) {
info("Error occurred during tempContext handler cleanup: " + error);
}
}
safeRemoveIfExists(dir);
dir = undefined;
}
},
onCleanup(handler: VoidFunction) {
tempContextCleanupHandlers.push(handler);
},
};
return result;
}
export function systemTempDir(name: string) {
const dir = join(rootTempDir(), name);
ensureDirSync(dir);
return dir;
}
function rootTempDir() {
const tempDir = isWindows
? Deno.env.get("TMP") || Deno.env.get("TEMP") ||
Deno.env.get("USERPROFILE") || ""
: Deno.env.get("TMPDIR") || "/tmp";
return normalizePath(tempDir);
}