UNPKG

7.27 kBTypeScriptView Raw
1export interface TmpNameOptions {
2 /**
3 * The optional temporary directory that must be relative to the system's default
4 * temporary directory. Absolute paths are fine as long as they point to a
5 * location under the system's default temporary directory. Any directories along
6 * the so specified path must exist, otherwise a `ENOENT` error will be thrown
7 * upon access, as tmp will not check the availability of the path, nor will it
8 * establish the requested path for you.
9 */
10 dir?: string | undefined;
11 /**
12 * A fixed name that overrides random name generation, the name must
13 * be relative and must not contain path segments.
14 */
15 name?: string | undefined;
16 /**
17 * The optional prefix.
18 * @default 'tmp'
19 */
20 prefix?: string | undefined;
21 /**
22 * The optional postfix.
23 * @default ''
24 */
25 postfix?: string | undefined;
26 /**
27 * [`mkstemp`](http://www.kernel.org/doc/man-pages/online/pages/man3/mkstemp.3.html)
28 * like filename template, no default, must include `XXXXXX` once for random name generation,
29 * e.g. `'foo-bar-XXXXXX'`.
30 */
31 template?: string | undefined;
32 /**
33 * Allows you to override the system's root tmp directory.
34 */
35 tmpdir?: string | undefined;
36 /**
37 * How many times should the function try to get a unique filename before giving up.
38 * @default 3
39 */
40 tries?: number | undefined;
41}
42
43export interface FileOptions extends TmpNameOptions {
44 /**
45 * Detaches the file descriptor, caller is responsible for closing the file, `tmp` will no
46 * longer try closing the file during garbage collection.
47 * @default false
48 */
49 detachDescriptor?: boolean | undefined;
50 /**
51 * Discards the file descriptor (closes file, fd is -1), `tmp` will no longer try closing
52 * the file during garbage collection.
53 * @default false
54 */
55 discardDescriptor?: boolean | undefined;
56 /**
57 * Signals that the temporary file or directory should not be deleted on exit
58 *
59 * - In order to clean up, you will have to call the provided `cleanupCallback` function manually.
60 *
61 * @default false
62 */
63 keep?: boolean | undefined;
64 /**
65 * The file mode to create with.
66 * @default 0o600
67 */
68 mode?: number | undefined;
69}
70
71export interface FileOptionsDiscardFd extends FileOptions {
72 discardDescriptor: true;
73}
74
75export interface DirOptions extends TmpNameOptions {
76 /**
77 * Signals that the temporary file or directory should not be deleted on exit
78 *
79 * - In order to clean up, you will have to call the provided `cleanupCallback` function manually.
80 *
81 * @default false
82 */
83 keep?: boolean | undefined;
84 /**
85 * The file mode to create with.
86 * @default 0o700
87 */
88 mode?: number | undefined;
89 /**
90 * Recursively removes the created temporary directory, even when it's not empty.
91 * @default false
92 */
93 unsafeCleanup?: boolean | undefined;
94}
95
96export interface FileResult {
97 name: string;
98 fd: number;
99 removeCallback: () => void;
100}
101
102export type FileResultNoFd = Omit<FileResult, "fd">;
103
104export interface DirResult {
105 name: string;
106 removeCallback: () => void;
107}
108
109export type FileCallback = (err: Error | null, name: string, fd: number, removeCallback: () => void) => void;
110export type FileCallbackNoFd = (err: Error | null, name: string, fd: undefined, removeCallback: () => void) => void;
111
112export type DirCallback = (err: Error | null, name: string, removeCallback: () => void) => void;
113
114export type TmpNameCallback = (err: Error | null, name: string) => void;
115
116export const tmpdir: string;
117
118/**
119 * Asynchronous file creation.
120 *
121 * Simple temporary file creation, the file will be closed and unlinked on process exit.
122 *
123 * @example
124 * import * as tmp from 'tmp';
125 *
126 * tmp.file((err, path, fd, cleanupCallback) => {
127 * if (err) throw err;
128 *
129 * console.log('File: ', path);
130 * console.log('Filedescriptor: ', fd);
131 *
132 * // If we don't need the file anymore we could manually call the cleanupCallback
133 * // But that is not necessary if we didn't pass the keep option because the library
134 * // will clean after itself.
135 * cleanupCallback();
136 * });
137 */
138export function file(options: FileOptionsDiscardFd, cb: FileCallbackNoFd): void;
139export function file(options: FileOptions, cb: FileCallback): void;
140export function file(cb: FileCallback): void;
141
142/**
143 * Synchronous file creation.
144 *
145 * Simple synchronous temporary file creation, the file will be closed and unlinked on process exit.
146 *
147 * @example
148 * import * as tmp from 'tmp';
149 *
150 * const tmpobj = tmp.fileSync();
151 * console.log('File: ', tmpobj.name);
152 * console.log('Filedescriptor: ', tmpobj.fd);
153 *
154 * // If we don't need the file anymore we could manually call the removeCallback
155 * // But that is not necessary if we didn't pass the keep option because the library
156 * // will clean after itself.
157 * tmpobj.removeCallback();
158 */
159export function fileSync(options: FileOptionsDiscardFd): FileResultNoFd;
160export function fileSync(options?: FileOptions): FileResult;
161
162/**
163 * Asynchronous directory creation.
164 *
165 * Simple temporary directory creation, it will be removed on process exit.
166 *
167 * If the directory still contains items on process exit, then it won't be removed.
168 *
169 * @example
170 * import * as tmp from 'tmp';
171 *
172 * tmp.dir((err, path, cleanupCallback) => {
173 * if (err) throw err;
174 *
175 * console.log('Dir: ', path);
176 *
177 * // Manual cleanup
178 * cleanupCallback();
179 * });
180 */
181export function dir(options: DirOptions, cb: DirCallback): void;
182export function dir(cb: DirCallback): void;
183
184/**
185 * Synchronous directory creation.
186 *
187 * Simple synchronous temporary directory creation, it will be removed on process exit.
188 *
189 * If the directory still contains items on process exit, then it won't be removed.
190 *
191 * @example
192 * import * as tmp from 'tmp';
193 *
194 * const tmpobj = tmp.dirSync();
195 * console.log('Dir: ', tmpobj.name);
196 * // Manual cleanup
197 * tmpobj.removeCallback();
198 */
199export function dirSync(options?: DirOptions): DirResult;
200
201/**
202 * Asynchronous filename generation.
203 *
204 * Generates a unique filename in the specified directory.
205 *
206 * @example
207 * import * as tmp from 'tmp';
208 *
209 * tmp.tmpName((err, path) => {
210 * if (err) throw err;
211 *
212 * console.log('Created temporary filename: ', path);
213 * });
214 */
215export function tmpName(options: TmpNameOptions, cb: TmpNameCallback): void;
216export function tmpName(cb: TmpNameCallback): void;
217
218/**
219 * Synchronous filename generation.
220 *
221 * Synchronously generates a unique filename in the specified directory.
222 *
223 * @example
224 * import * as tmp from 'tmp';
225 *
226 * const name = tmp.tmpNameSync();
227 * console.log('Created temporary filename: ', name);
228 */
229export function tmpNameSync(options?: TmpNameOptions): string;
230
231/**
232 * Graceful cleanup.
233 *
234 * If graceful cleanup is set, `tmp` will remove all controlled temporary objects on process exit,
235 * otherwise the temporary objects will remain in place, waiting to be cleaned up on system
236 * restart or otherwise scheduled temporary object removal.
237 *
238 * @example
239 * import * as tmp from 'tmp';
240 *
241 * tmp.setGracefulCleanup();
242 */
243export function setGracefulCleanup(): void;