| 1 | # tempy [](https://travis-ci.com/github/sindresorhus/tempy)
|
|---|
| 2 |
|
|---|
| 3 | > Get a random temporary file or directory path
|
|---|
| 4 |
|
|---|
| 5 | ## Install
|
|---|
| 6 |
|
|---|
| 7 | ```
|
|---|
| 8 | $ npm install tempy
|
|---|
| 9 | ```
|
|---|
| 10 |
|
|---|
| 11 | ## Usage
|
|---|
| 12 |
|
|---|
| 13 | ```js
|
|---|
| 14 | const tempy = require('tempy');
|
|---|
| 15 |
|
|---|
| 16 | tempy.file();
|
|---|
| 17 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
|
|---|
| 18 |
|
|---|
| 19 | tempy.file({extension: 'png'});
|
|---|
| 20 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'
|
|---|
| 21 |
|
|---|
| 22 | tempy.file({name: 'unicorn.png'});
|
|---|
| 23 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'
|
|---|
| 24 |
|
|---|
| 25 | tempy.directory();
|
|---|
| 26 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
|
|---|
| 27 |
|
|---|
| 28 | tempy.directory({prefix: 'name'});
|
|---|
| 29 | //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
|
|---|
| 30 | ```
|
|---|
| 31 |
|
|---|
| 32 | ## API
|
|---|
| 33 |
|
|---|
| 34 | ### tempy.file(options?)
|
|---|
| 35 |
|
|---|
| 36 | Get a temporary file path you can write to.
|
|---|
| 37 |
|
|---|
| 38 | #### options
|
|---|
| 39 |
|
|---|
| 40 | Type: `object`
|
|---|
| 41 |
|
|---|
| 42 | *You usually won't need either the `extension` or `name` option. Specify them only when actually needed.*
|
|---|
| 43 |
|
|---|
| 44 | ##### extension
|
|---|
| 45 |
|
|---|
| 46 | Type: `string`
|
|---|
| 47 |
|
|---|
| 48 | File extension.
|
|---|
| 49 |
|
|---|
| 50 | ##### name
|
|---|
| 51 |
|
|---|
| 52 | Type: `string`
|
|---|
| 53 |
|
|---|
| 54 | Filename. Mutually exclusive with the `extension` option.
|
|---|
| 55 |
|
|---|
| 56 | ### tempy.directory([options])
|
|---|
| 57 |
|
|---|
| 58 | Get a temporary directory path. The directory is created for you.
|
|---|
| 59 |
|
|---|
| 60 | #### options
|
|---|
| 61 |
|
|---|
| 62 | Type: `Object`
|
|---|
| 63 |
|
|---|
| 64 | ##### prefix
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 | Type: `string`
|
|---|
| 68 |
|
|---|
| 69 | Directory prefix.
|
|---|
| 70 |
|
|---|
| 71 | Useful for testing by making it easier to identify cache directories that are created.
|
|---|
| 72 |
|
|---|
| 73 | *You usually won't need this option. Specify it only when actually needed.*
|
|---|
| 74 |
|
|---|
| 75 | ### tempy.write(fileContent, options?)
|
|---|
| 76 |
|
|---|
| 77 | Write data to a random temp file.
|
|---|
| 78 |
|
|---|
| 79 | ##### fileContent
|
|---|
| 80 |
|
|---|
| 81 | Type: `string | Buffer | TypedArray | DataView | stream.Readable`
|
|---|
| 82 |
|
|---|
| 83 | Data to write to the temp file.
|
|---|
| 84 |
|
|---|
| 85 | ##### options
|
|---|
| 86 |
|
|---|
| 87 | See [options](#options).
|
|---|
| 88 |
|
|---|
| 89 | ### tempy.writeSync(fileContent, options?)
|
|---|
| 90 |
|
|---|
| 91 | Synchronously write data to a random temp file.
|
|---|
| 92 |
|
|---|
| 93 | ##### fileContent
|
|---|
| 94 |
|
|---|
| 95 | Type: `string | Buffer | TypedArray | DataView`
|
|---|
| 96 |
|
|---|
| 97 | Data to write to the temp file.
|
|---|
| 98 |
|
|---|
| 99 | ##### options
|
|---|
| 100 |
|
|---|
| 101 | See [options](#options).
|
|---|
| 102 |
|
|---|
| 103 | ### tempy.root
|
|---|
| 104 |
|
|---|
| 105 | Get the root temporary directory path. For example: `/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T`
|
|---|
| 106 |
|
|---|
| 107 | ## FAQ
|
|---|
| 108 |
|
|---|
| 109 | #### Why doesn't it have a cleanup method?
|
|---|
| 110 |
|
|---|
| 111 | Temp files will be periodically cleaned up on macOS. Most Linux distros will clean up on reboot. If you're generating a lot of temp files, it's recommended to use a complementary module like [`del`](https://github.com/sindresorhus/del) for cleanup.
|
|---|