MCPcopy Create free account
hub / github.com/denoland/std / makeTempFile

Function makeTempFile

fs/unstable_make_temp_file.ts:38–82  ·  view source on GitHub ↗
(options?: MakeTempOptions)

Source from the content-addressed store, hash-verified

36 * @returns A Promise that resolves to a file path to the temporary file.
37 */
38export async function makeTempFile(options?: MakeTempOptions): Promise<string> {
39 if (isDeno) {
40 return Deno.makeTempFile({ ...options });
41 } else {
42 const {
43 dir,
44 prefix,
45 suffix,
46 } = options ?? {};
47 try {
48 const { tmpdir } = getNodeOs();
49 const { join } = getNodePath();
50
51 let tempFilePath;
52 if (!options) {
53 tempFilePath = join(tmpdir(), randomId());
54 await writeTextFile(tempFilePath, "", { mode: 0o600 });
55 return tempFilePath;
56 }
57
58 tempFilePath = tmpdir();
59 if (dir != null) {
60 tempFilePath = typeof dir === "string" ? dir : ".";
61 if (tempFilePath === "") {
62 tempFilePath = ".";
63 }
64 }
65
66 if (prefix != null && typeof prefix === "string") {
67 tempFilePath = join(tempFilePath, prefix + randomId());
68 } else {
69 tempFilePath = join(tempFilePath, randomId());
70 }
71
72 if (suffix != null && typeof suffix === "string") {
73 tempFilePath += suffix;
74 }
75
76 await writeTextFile(tempFilePath, "", { mode: 0o600 });
77 return tempFilePath;
78 } catch (error) {
79 throw mapError(error);
80 }
81 }
82}
83
84/**
85 * Synchronously creates a new temporary file in the default directory for

Calls 6

getNodeOsFunction · 0.90
getNodePathFunction · 0.90
randomIdFunction · 0.90
writeTextFileFunction · 0.90
mapErrorFunction · 0.90
joinFunction · 0.50

Tested by

no test coverage detected