All files / fs / unstable_make_temp_file.ts

0.00% Branches 0/2
16.28% Lines 14/86
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
 
 
x5
x5
 
x5
x5
x5
x5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x5
x15
x45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x5
x15
x45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x15







































I








































































I









































// Copyright 2018-2025 the Deno authors. MIT license.

import { getNodeOs, getNodePath, isDeno, randomId } from "./_utils.ts";
import { mapError } from "./_map_error.ts";
import type { MakeTempOptions } from "./unstable_types.ts";
import {
  writeTextFile,
  writeTextFileSync,
} from "./unstable_write_text_file.ts";

/**
 * Creates a new temporary file in the default directory for temporary files,
 * unless `dir` is specified.
 *
 * Other options include prefixing and suffixing the directory name with
 * `prefix` and `suffix` respectively.
 *
 * This call resolves to the full path to the newly created file.
 *
 * Multiple programs calling this function simultaneously will create different
 * files. It is the caller's responsibility to remove the file when no longer
 * needed.
 *
 * Requires `allow-write` permission.
 *
 * @example Usage
 * ```ts ignore
 * import { makeTempFile } from "@std/fs/unstable-make-temp-file";
 * const tmpFileName0 = await makeTempFile();  // e.g. /tmp/419e0bf2
 * const tmpFileName1 = await makeTempFile({ prefix: 'my_temp' });  // e.g. /tmp/my_temp754d3098
 * ```
 *
 * @tags allow-write
 *
 * @param options The options specified when creating a temporary file.
 * @returns A Promise that resolves to a file path to the temporary file.
 */
export async function makeTempFile(options?: MakeTempOptions): Promise<string> {
  if (isDeno) {
    return Deno.makeTempFile({ ...options });
  } else {
    const {
      dir,
      prefix,
      suffix,
    } = options ?? {};
    try {
      const { tmpdir } = getNodeOs();
      const { join } = getNodePath();

      let tempFilePath;
      if (!options) {
        tempFilePath = join(tmpdir(), randomId());
        await writeTextFile(tempFilePath, "", { mode: 0o600 });
        return tempFilePath;
      }

      tempFilePath = tmpdir();
      if (dir != null) {
        tempFilePath = typeof dir === "string" ? dir : ".";
        if (tempFilePath === "") {
          tempFilePath = ".";
        }
      }

      if (prefix != null && typeof prefix === "string") {
        tempFilePath = join(tempFilePath, prefix + randomId());
      } else {
        tempFilePath = join(tempFilePath, randomId());
      }

      if (suffix != null && typeof suffix === "string") {
        tempFilePath += suffix;
      }

      await writeTextFile(tempFilePath, "", { mode: 0o600 });
      return tempFilePath;
    } catch (error) {
      throw mapError(error);
    }
  }
}

/**
 * Synchronously creates a new temporary file in the default directory for
 * temporary files, unless `dir` is specified.
 *
 * Other options include prefixing and suffixing the directory name with
 * `prefix` and `suffix` respectively.
 *
 * The full path to the newly created file is returned.
 *
 * Multiple programs calling this function simultaneously will create different
 * files. It is the caller's responsibility to remove the file when no longer
 * needed.
 *
 * Requires `allow-write` permission.
 *
 * @example Usage
 * ```ts ignore
 * import { makeTempFileSync } from "@std/fs/unstable-make-temp-file";
 * const tempFileName0 = makeTempFileSync(); // e.g. /tmp/419e0bf2
 * const tempFileName1 = makeTempFileSync({ prefix: 'my_temp' });  // e.g. /tmp/my_temp754d3098
 * ```
 *
 * @tags allow-write
 *
 * @param options The options specified when creating a temporary file.
 * @returns The file path to the temporary file.
 */
export function makeTempFileSync(options?: MakeTempOptions): string {
  if (isDeno) {
    return Deno.makeTempFileSync({ ...options });
  } else {
    const {
      dir,
      prefix,
      suffix,
    } = options ?? {};

    try {
      const { tmpdir } = getNodeOs();
      const { join } = getNodePath();

      let tempFilePath;
      if (!options) {
        tempFilePath = join(tmpdir(), randomId());
        writeTextFileSync(tempFilePath, "", { mode: 0o600 });
        return tempFilePath;
      }

      tempFilePath = tmpdir();
      if (dir != null) {
        tempFilePath = typeof dir === "string" ? dir : ".";
        if (tempFilePath === "") {
          tempFilePath = ".";
        }
      }

      if (prefix != null && typeof prefix === "string") {
        tempFilePath = join(tempFilePath, prefix + randomId());
      } else {
        tempFilePath = join(tempFilePath, randomId());
      }

      if (suffix != null && typeof suffix === "string") {
        tempFilePath += suffix;
      }

      writeTextFileSync(tempFilePath, "", { mode: 0o600 });
      return tempFilePath;
    } catch (error) {
      throw mapError(error);
    }
  }
}