UNPKG

4.79 kBTypeScriptView Raw
1import {type Buffer} from 'node:buffer';
2import {type MergeExclusive, type TypedArray} from 'type-fest';
3
4export type FileOptions = MergeExclusive<
5{
6 /**
7 File extension.
8
9 Mutually exclusive with the `name` option.
10
11 _You usually won't need this option. Specify it only when actually needed._
12 */
13 readonly extension?: string;
14},
15{
16 /**
17 Filename.
18
19 Mutually exclusive with the `extension` option.
20
21 _You usually won't need this option. Specify it only when actually needed._
22 */
23 readonly name?: string;
24}
25>;
26
27export type DirectoryOptions = {
28 /**
29 Directory prefix.
30
31 _You usually won't need this option. Specify it only when actually needed._
32
33 Useful for testing by making it easier to identify cache directories that are created.
34 */
35 readonly prefix?: string;
36};
37
38/**
39The temporary path created by the function. Can be asynchronous.
40*/
41export type TaskCallback<ReturnValueType> = (temporaryPath: string) => Promise<ReturnValueType> | ReturnValueType;
42
43/**
44Get a temporary file path you can write to.
45
46@example
47```
48import {temporaryFile, temporaryDirectory} from 'tempy';
49
50temporaryFile();
51//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
52
53temporaryFile({extension: 'png'});
54//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'
55
56temporaryFile({name: 'unicorn.png'});
57//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'
58
59temporaryDirectory();
60//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
61```
62*/
63export function temporaryFile(options?: FileOptions): string;
64
65/**
66The `callback` resolves with a temporary file path you can write to. The file is automatically cleaned up after the callback is executed.
67
68@returns A promise that resolves after the callback is executed and the file is cleaned up.
69
70@example
71```
72import {temporaryFileTask} from 'tempy';
73
74await temporaryFileTask(tempFile => {
75 console.log(tempFile);
76 //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
77});
78```
79*/
80export function temporaryFileTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise <ReturnValueType>;
81
82/**
83Get a temporary directory path. The directory is created for you.
84
85@example
86```
87import {temporaryDirectory} from 'tempy';
88
89temporaryDirectory();
90//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
91
92temporaryDirectory({prefix: 'a'});
93//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
94```
95*/
96export function temporaryDirectory(options?: DirectoryOptions): string;
97
98/**
99The `callback` resolves with a temporary directory path you can write to. The directory is automatically cleaned up after the callback is executed.
100
101@returns A promise that resolves after the callback is executed and the directory is cleaned up.
102
103@example
104```
105import {temporaryDirectoryTask} from 'tempy';
106
107await temporaryDirectoryTask(tempDirectory => {
108 //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
109})
110```
111*/
112export function temporaryDirectoryTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: DirectoryOptions): Promise<ReturnValueType>;
113
114/**
115Write data to a random temp file.
116
117@example
118```
119import {temporaryWrite} from 'tempy';
120
121await temporaryWrite('🦄');
122//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
123```
124*/
125export function temporaryWrite(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: FileOptions): Promise<string>;
126
127/**
128Write data to a random temp file. The file is automatically cleaned up after the callback is executed.
129
130@returns A promise that resolves after the callback is executed and the file is cleaned up.
131
132@example
133```
134import {temporaryWriteTask} from 'tempy';
135
136await temporaryWriteTask('🦄', tempFile => {
137 //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
138});
139```
140*/
141export function temporaryWriteTask<ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise<ReturnValueType>;
142
143/**
144Synchronously write data to a random temp file.
145
146@example
147```
148import {temporaryWriteSync} from 'tempy';
149
150temporaryWriteSync('🦄');
151//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
152```
153*/
154export function temporaryWriteSync(fileContent: string | Buffer | TypedArray | DataView, options?: FileOptions): string;
155
156export {default as rootTemporaryDirectory} from 'temp-dir';