Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting a prefix for temp directory #20

Merged
merged 15 commits into from
Jul 18, 2020
24 changes: 19 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {MergeExclusive, TypedArray} from 'type-fest';

declare namespace tempy {
type Options = MergeExclusive<
type FileOptions = MergeExclusive<
{
/**
File extension.
Expand All @@ -24,6 +24,17 @@ declare namespace tempy {
readonly name?: string;
}
>;

type DirectoryOptions = {
/**
_You usually won't need this option. Specify it only when actually needed._
Richienb marked this conversation as resolved.
Show resolved Hide resolved

Directory prefix.
Richienb marked this conversation as resolved.
Show resolved Hide resolved

Useful for testing by making it easier to identify cache directories that are created.
*/
readonly prefix?: string;
}
}

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

/**
Get a temporary directory path. The directory is created for you.
Expand All @@ -58,9 +69,12 @@ declare const tempy: {

tempy.directory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'

tempy.directory({prefix: 'a'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
```
*/
directory(): string;
directory(options?: tempy.DirectoryOptions): string;

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

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

/**
Get the root temporary directory path.
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {promisify} = require('util');
const pipeline = promisify(stream.pipeline);
const {writeFile} = fs.promises;

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

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

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

module.exports.directory = () => {
const directory = getPath();
module.exports.directory = ({prefix = ''} = {}) => {
const directory = getPath(prefix);
fs.mkdirSync(directory);
return directory;
};
Expand Down
3 changes: 2 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {expectType, expectError} from 'tsd';
import tempy = require('.');

const options: tempy.Options = {};
const options: tempy.FileOptions = {};
expectType<string>(tempy.directory());
expectType<string>(tempy.directory({prefix: 'name_'}));
expectType<string>(tempy.file());
expectType<string>(tempy.file({extension: 'png'}));
expectType<string>(tempy.file({name: 'afile.txt'}));
Expand Down
19 changes: 18 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ tempy.file({name: 'unicorn.png'});

tempy.directory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'

tempy.directory({prefix: 'name'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
```

## API
Expand All @@ -50,10 +53,24 @@ Type: `string`

Filename. Mutually exclusive with the `extension` option.

### tempy.directory()
### tempy.directory([options])

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

#### options

Type: `Object`

##### prefix

_You usually won't need this option. Specify it only when actually needed._

Type: `string`

Directory prefix.

Useful for testing by making it easier to identify cache directories that are created.

### tempy.write(fileContent, options?)

Write data to a random temp file.
Expand Down
3 changes: 3 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ test('.file()', t => {
});

test('.directory()', t => {
const prefix = 'name_';

t.true(tempy.directory().includes(tempDir));
t.true(path.basename(tempy.directory({prefix})).startsWith(prefix));
});

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