Skip to content

Commit f7fc51f

Browse files
Richienbsindresorhus
andauthoredJul 18, 2020
Allow setting a prefix for temp directory (#20)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 1a64f8b commit f7fc51f

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed
 

‎index.d.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import {MergeExclusive, TypedArray} from 'type-fest';
33

44
declare namespace tempy {
5-
type Options = MergeExclusive<
5+
type FileOptions = MergeExclusive<
66
{
77
/**
88
File extension.
@@ -24,6 +24,17 @@ declare namespace tempy {
2424
readonly name?: string;
2525
}
2626
>;
27+
28+
type DirectoryOptions = {
29+
/**
30+
_You usually won't need this option. Specify it only when actually needed._
31+
32+
Directory prefix.
33+
34+
Useful for testing by making it easier to identify cache directories that are created.
35+
*/
36+
readonly prefix?: string;
37+
}
2738
}
2839

2940
declare const tempy: {
@@ -47,7 +58,7 @@ declare const tempy: {
4758
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
4859
```
4960
*/
50-
file(options?: tempy.Options): string;
61+
file(options?: tempy.FileOptions): string;
5162

5263
/**
5364
Get a temporary directory path. The directory is created for you.
@@ -58,9 +69,12 @@ declare const tempy: {
5869
5970
tempy.directory();
6071
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
72+
73+
tempy.directory({prefix: 'a'});
74+
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
6175
```
6276
*/
63-
directory(): string;
77+
directory(options?: tempy.DirectoryOptions): string;
6478

6579
/**
6680
Write data to a random temp file.
@@ -73,7 +87,7 @@ declare const tempy: {
7387
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
7488
```
7589
*/
76-
write(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: tempy.Options): Promise<string>;
90+
write(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: tempy.FileOptions): Promise<string>;
7791

7892
/**
7993
Synchronously write data to a random temp file.
@@ -86,7 +100,7 @@ declare const tempy: {
86100
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
87101
```
88102
*/
89-
writeSync(fileContent: string | Buffer | TypedArray | DataView, options?: tempy.Options): string;
103+
writeSync(fileContent: string | Buffer | TypedArray | DataView, options?: tempy.FileOptions): string;
90104

91105
/**
92106
Get the root temporary directory path.

‎index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {promisify} = require('util');
1010
const pipeline = promisify(stream.pipeline);
1111
const {writeFile} = fs.promises;
1212

13-
const getPath = () => path.join(tempDir, uniqueString());
13+
const getPath = (prefix = '') => path.join(tempDir, prefix + uniqueString());
1414

1515
const writeStream = async (filePath, data) => pipeline(data, fs.createWriteStream(filePath));
1616

@@ -30,8 +30,8 @@ module.exports.file = options => {
3030
return getPath() + (options.extension === undefined || options.extension === null ? '' : '.' + options.extension.replace(/^\./, ''));
3131
};
3232

33-
module.exports.directory = () => {
34-
const directory = getPath();
33+
module.exports.directory = ({prefix = ''} = {}) => {
34+
const directory = getPath(prefix);
3535
fs.mkdirSync(directory);
3636
return directory;
3737
};

‎index.test-d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {expectType, expectError} from 'tsd';
22
import tempy = require('.');
33

4-
const options: tempy.Options = {};
4+
const options: tempy.FileOptions = {};
55
expectType<string>(tempy.directory());
6+
expectType<string>(tempy.directory({prefix: 'name_'}));
67
expectType<string>(tempy.file());
78
expectType<string>(tempy.file({extension: 'png'}));
89
expectType<string>(tempy.file({name: 'afile.txt'}));

‎readme.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ tempy.file({name: 'unicorn.png'});
2424

2525
tempy.directory();
2626
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
27+
28+
tempy.directory({prefix: 'name'});
29+
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
2730
```
2831

2932
## API
@@ -50,10 +53,24 @@ Type: `string`
5053

5154
Filename. Mutually exclusive with the `extension` option.
5255

53-
### tempy.directory()
56+
### tempy.directory([options])
5457

5558
Get a temporary directory path. The directory is created for you.
5659

60+
#### options
61+
62+
Type: `Object`
63+
64+
##### prefix
65+
66+
_You usually won't need this option. Specify it only when actually needed._
67+
68+
Type: `string`
69+
70+
Directory prefix.
71+
72+
Useful for testing by making it easier to identify cache directories that are created.
73+
5774
### tempy.write(fileContent, options?)
5875

5976
Write data to a random temp file.

‎test.js

+3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ test('.file()', t => {
3333
});
3434

3535
test('.directory()', t => {
36+
const prefix = 'name_';
37+
3638
t.true(tempy.directory().includes(tempDir));
39+
t.true(path.basename(tempy.directory({prefix})).startsWith(prefix));
3740
});
3841

3942
test('.write(string)', async t => {

0 commit comments

Comments
 (0)
Please sign in to comment.