1 | import {type Buffer} from 'node:buffer';
|
2 | import {type MergeExclusive, type TypedArray} from 'type-fest';
|
3 |
|
4 | export 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 |
|
27 | export 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 | /**
|
39 | The temporary path created by the function. Can be asynchronous.
|
40 | */
|
41 | export type TaskCallback<ReturnValueType> = (temporaryPath: string) => Promise<ReturnValueType> | ReturnValueType;
|
42 |
|
43 | /**
|
44 | Get a temporary file path you can write to.
|
45 |
|
46 | @example
|
47 | ```
|
48 | import {temporaryFile, temporaryDirectory} from 'tempy';
|
49 |
|
50 | temporaryFile();
|
51 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
|
52 |
|
53 | temporaryFile({extension: 'png'});
|
54 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'
|
55 |
|
56 | temporaryFile({name: 'unicorn.png'});
|
57 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'
|
58 |
|
59 | temporaryDirectory();
|
60 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
|
61 | ```
|
62 | */
|
63 | export function temporaryFile(options?: FileOptions): string;
|
64 |
|
65 | /**
|
66 | The `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 | ```
|
72 | import {temporaryFileTask} from 'tempy';
|
73 |
|
74 | await temporaryFileTask(tempFile => {
|
75 | console.log(tempFile);
|
76 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
|
77 | });
|
78 | ```
|
79 | */
|
80 | export function temporaryFileTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise <ReturnValueType>;
|
81 |
|
82 | /**
|
83 | Get a temporary directory path. The directory is created for you.
|
84 |
|
85 | @example
|
86 | ```
|
87 | import {temporaryDirectory} from 'tempy';
|
88 |
|
89 | temporaryDirectory();
|
90 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
|
91 |
|
92 | temporaryDirectory({prefix: 'a'});
|
93 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
|
94 | ```
|
95 | */
|
96 | export function temporaryDirectory(options?: DirectoryOptions): string;
|
97 |
|
98 | /**
|
99 | The `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 | ```
|
105 | import {temporaryDirectoryTask} from 'tempy';
|
106 |
|
107 | await temporaryDirectoryTask(tempDirectory => {
|
108 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
|
109 | })
|
110 | ```
|
111 | */
|
112 | export function temporaryDirectoryTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: DirectoryOptions): Promise<ReturnValueType>;
|
113 |
|
114 | /**
|
115 | Write data to a random temp file.
|
116 |
|
117 | @example
|
118 | ```
|
119 | import {temporaryWrite} from 'tempy';
|
120 |
|
121 | await temporaryWrite('🦄');
|
122 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
|
123 | ```
|
124 | */
|
125 | export function temporaryWrite(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: FileOptions): Promise<string>;
|
126 |
|
127 | /**
|
128 | Write 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 | ```
|
134 | import {temporaryWriteTask} from 'tempy';
|
135 |
|
136 | await temporaryWriteTask('🦄', tempFile => {
|
137 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
|
138 | });
|
139 | ```
|
140 | */
|
141 | export function temporaryWriteTask<ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise<ReturnValueType>;
|
142 |
|
143 | /**
|
144 | Synchronously write data to a random temp file.
|
145 |
|
146 | @example
|
147 | ```
|
148 | import {temporaryWriteSync} from 'tempy';
|
149 |
|
150 | temporaryWriteSync('🦄');
|
151 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
|
152 | ```
|
153 | */
|
154 | export function temporaryWriteSync(fileContent: string | Buffer | TypedArray | DataView, options?: FileOptions): string;
|
155 |
|
156 | export {default as rootTemporaryDirectory} from 'temp-dir';
|