UNPKG

3.44 kBMarkdownView Raw
1# tempy
2
3> Get a random temporary file or directory path
4
5## Install
6
7```sh
8npm install tempy
9```
10
11## Usage
12
13```js
14import {temporaryFile, temporaryDirectory} from 'tempy';
15
16temporaryFile();
17//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
18
19temporaryFile({extension: 'png'});
20//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'
21
22temporaryFile({name: 'unicorn.png'});
23//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'
24
25temporaryDirectory();
26//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
27
28temporaryDirectory({prefix: 'name'});
29//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
30```
31
32## API
33
34### temporaryFile(options?)
35
36Get a temporary file path you can write to.
37
38### temporaryFileTask(callback, options?)
39
40The `callback` resolves with a temporary file path you can write to. The file is automatically cleaned up after the callback is executed. Returns a promise that resolves with the return value of the callback after it is executed and the file is cleaned up.
41
42#### callback
43
44Type: `(tempPath: string) => void`
45
46A callback that is executed with the temp file path. Can be asynchronous.
47
48#### options
49
50Type: `object`
51
52*You usually won't need either the `extension` or `name` option. Specify them only when actually needed.*
53
54##### extension
55
56Type: `string`
57
58File extension.
59
60##### name
61
62Type: `string`
63
64Filename. Mutually exclusive with the `extension` option.
65
66### temporaryDirectory(options?)
67
68Get a temporary directory path. The directory is created for you.
69
70### temporaryDirectoryTask(callback, options?)
71
72The `callback` resolves with a temporary directory path you can write to. The directory is automatically cleaned up after the callback is executed. Returns a promise that resolves with the return value of the callback after it is executed and the directory is cleaned up.
73
74##### callback
75
76Type: `(tempPath: string) => void`
77
78A callback that is executed with the temp directory path. Can be asynchronous.
79
80#### options
81
82Type: `Object`
83
84##### prefix
85
86Type: `string`
87
88Directory prefix.
89
90Useful for testing by making it easier to identify cache directories that are created.
91
92*You usually won't need this option. Specify it only when actually needed.*
93
94### temporaryWrite(fileContent, options?)
95
96Write data to a random temp file.
97
98### temporaryWriteTask(fileContent, callback, options?)
99
100Write data to a random temp file. The file is automatically cleaned up after the callback is executed. Returns a promise that resolves with the return value of the callback after it is executed and the file is cleaned up.
101
102##### fileContent
103
104Type: `string | Buffer | TypedArray | DataView | stream.Readable`
105
106Data to write to the temp file.
107
108##### callback
109
110Type: `(tempPath: string) => void`
111
112A callback that is executed with the temp file path. Can be asynchronous.
113
114##### options
115
116See [options](#options).
117
118### temporaryWriteSync(fileContent, options?)
119
120Synchronously write data to a random temp file.
121
122##### fileContent
123
124Type: `string | Buffer | TypedArray | DataView`
125
126Data to write to the temp file.
127
128##### options
129
130See [options](#options).
131
132### rootTemporaryDirectory
133
134Get the root temporary directory path. For example: `/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T`