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