(options?: MakeTempOptions)
| 36 | * @returns A Promise that resolves to a file path to the temporary file. |
| 37 | */ |
| 38 | export 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 |
no test coverage detected