UNPKG

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