Skip to content

Commit 4b3e859

Browse files
authoredApr 18, 2022
Require Node.js 14 (#41)
1 parent 578a8ac commit 4b3e859

File tree

6 files changed

+196
-232
lines changed

6 files changed

+196
-232
lines changed
 

‎index.d.ts

+107-123
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/member-ordering */
21
import {Buffer} from 'node:buffer';
32
import {MergeExclusive, TypedArray} from 'type-fest';
43

@@ -41,132 +40,117 @@ The temporary path created by the function. Can be asynchronous.
4140
*/
4241
export type TaskCallback<ReturnValueType> = (temporaryPath: string) => Promise<ReturnValueType> | ReturnValueType;
4342

44-
declare const tempy: {
45-
file: {
46-
/**
47-
The `callback` resolves with a temporary file path you can write to. The file is automatically cleaned up after the callback is executed.
48-
49-
@returns A promise that resolves after the callback is executed and the file is cleaned up.
50-
51-
@example
52-
```
53-
import tempy from 'tempy';
54-
55-
await tempy.file.task(tempFile => {
56-
console.log(tempFile);
57-
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
58-
});
59-
```
60-
*/
61-
task: <ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: FileOptions) => Promise<ReturnValueType>;
62-
63-
/**
64-
Get a temporary file path you can write to.
65-
66-
@example
67-
```
68-
import tempy from 'tempy';
69-
70-
tempy.file();
71-
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
72-
73-
tempy.file({extension: 'png'});
74-
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'
75-
76-
tempy.file({name: 'unicorn.png'});
77-
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'
78-
79-
tempy.directory();
80-
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
81-
```
82-
*/
83-
(options?: FileOptions): string;
84-
};
85-
86-
directory: {
87-
/**
88-
The `callback` resolves with a temporary directory path you can write to. The directory is automatically cleaned up after the callback is executed.
89-
90-
@returns A promise that resolves after the callback is executed and the directory is cleaned up.
91-
92-
@example
93-
```
94-
import tempy from 'tempy';
95-
96-
await tempy.directory.task(tempDirectory => {
97-
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
98-
})
99-
```
100-
*/
101-
task: <ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: DirectoryOptions) => Promise<ReturnValueType>;
102-
103-
/**
104-
Get a temporary directory path. The directory is created for you.
105-
106-
@example
107-
```
108-
import tempy from 'tempy';
109-
110-
tempy.directory();
111-
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
112-
113-
tempy.directory({prefix: 'a'});
114-
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
115-
```
116-
*/
117-
(options?: DirectoryOptions): string;
118-
};
119-
120-
write: {
121-
/**
122-
Write data to a random temp file. The file is automatically cleaned up after the callback is executed.
123-
124-
@returns A promise that resolves after the callback is executed and the file is cleaned up.
125-
126-
@example
127-
```
128-
import tempy from 'tempy';
129-
130-
await tempy.write.task('🦄', tempFile => {
131-
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
132-
});
133-
```
134-
*/
135-
task: <ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: TaskCallback<ReturnValueType>, options?: FileOptions) => Promise<ReturnValueType>;
136-
137-
/**
138-
Write data to a random temp file.
139-
140-
@example
141-
```
142-
import tempy from 'tempy';
143-
144-
await tempy.write('🦄');
145-
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
146-
```
147-
*/
148-
(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: FileOptions): Promise<string>;
149-
};
43+
/**
44+
Get a temporary file path you can write to.
15045
151-
/**
152-
Synchronously write data to a random temp file.
46+
@example
47+
```
48+
import {temporaryFile, temporaryDirectory} from 'tempy';
49+
50+
temporaryFile();
51+
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
52+
53+
temporaryFile({extension: 'png'});
54+
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'
55+
56+
temporaryFile({name: 'unicorn.png'});
57+
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'
58+
59+
temporaryDirectory();
60+
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
61+
```
62+
*/
63+
export function temporaryFile(options?: FileOptions): string;
64+
65+
/**
66+
The `callback` resolves with a temporary file path you can write to. The file is automatically cleaned up after the callback is executed.
67+
68+
@returns A promise that resolves after the callback is executed and the file is cleaned up.
69+
70+
@example
71+
```
72+
import {temporaryFileTask} from 'tempy';
73+
74+
await temporaryFileTask(tempFile => {
75+
console.log(tempFile);
76+
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
77+
});
78+
```
79+
*/
80+
export function temporaryFileTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise <ReturnValueType>;
81+
82+
/**
83+
Get a temporary directory path. The directory is created for you.
84+
85+
@example
86+
```
87+
import {temporaryDirectory} from 'tempy';
88+
89+
temporaryDirectory();
90+
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
91+
92+
temporaryDirectory({prefix: 'a'});
93+
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
94+
```
95+
*/
96+
export function temporaryDirectory(options?: DirectoryOptions): string;
97+
98+
/**
99+
The `callback` resolves with a temporary directory path you can write to. The directory is automatically cleaned up after the callback is executed.
153100
154-
@example
155-
```
156-
import tempy from 'tempy';
101+
@returns A promise that resolves after the callback is executed and the directory is cleaned up.
157102
158-
tempy.writeSync('🦄');
103+
@example
104+
```
105+
import {temporaryDirectoryTask} from 'tempy';
106+
107+
await temporaryDirectoryTask(tempDirectory => {
159108
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
160-
```
161-
*/
162-
writeSync: (fileContent: string | Buffer | TypedArray | DataView, options?: FileOptions) => string;
109+
})
110+
```
111+
*/
112+
export function temporaryDirectoryTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: DirectoryOptions): Promise<ReturnValueType>;
163113

164-
/**
165-
Get the root temporary directory path.
114+
/**
115+
Write data to a random temp file.
166116
167-
For example: `/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T`.
168-
*/
169-
readonly root: string;
170-
};
117+
@example
118+
```
119+
import {temporaryWrite} from 'tempy';
120+
121+
await temporaryWrite('🦄');
122+
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
123+
```
124+
*/
125+
export function temporaryWrite(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: FileOptions): Promise<string>;
126+
127+
/**
128+
Write data to a random temp file. The file is automatically cleaned up after the callback is executed.
129+
130+
@returns A promise that resolves after the callback is executed and the file is cleaned up.
131+
132+
@example
133+
```
134+
import {temporaryWriteTask} from 'tempy';
135+
136+
await temporaryWriteTask('🦄', tempFile => {
137+
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
138+
});
139+
```
140+
*/
141+
export function temporaryWriteTask<ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise<ReturnValueType>;
142+
143+
/**
144+
Synchronously write data to a random temp file.
145+
146+
@example
147+
```
148+
import {temporaryWriteSync} from 'tempy';
149+
150+
temporaryWriteSync('🦄');
151+
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
152+
```
153+
*/
154+
export function temporaryWriteSync(fileContent: string | Buffer | TypedArray | DataView, options?: FileOptions): string;
171155

172-
export default tempy;
156+
export {default as rootTemporaryDirectory} from 'temp-dir';

‎index.js

+25-41
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,56 @@ import {promisify} from 'node:util';
55
import uniqueString from 'unique-string';
66
import tempDir from 'temp-dir';
77
import {isStream} from 'is-stream';
8-
import del from 'del'; // TODO: Replace this with `fs.rm` when targeting Node.js 14.
98

109
const pipeline = promisify(stream.pipeline); // TODO: Use `node:stream/promises` when targeting Node.js 16.
1110

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

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

16-
const createTask = (tempyFunction, {extraArguments = 0} = {}) => async (...arguments_) => {
17-
const [callback, options] = arguments_.slice(extraArguments);
18-
const result = await tempyFunction(...arguments_.slice(0, extraArguments), options);
19-
15+
async function runTask(temporaryPath, callback) {
2016
try {
21-
return await callback(result);
17+
return await callback(temporaryPath);
2218
} finally {
23-
await del(result, {force: true});
19+
await fsPromises.rm(temporaryPath, {recursive: true, force: true});
2420
}
25-
};
26-
27-
const tempy = {};
21+
}
2822

29-
tempy.file = options => {
30-
options = {
31-
...options,
32-
};
33-
34-
if (options.name) {
35-
if (options.extension !== undefined && options.extension !== null) {
23+
export function temporaryFile({name, extension} = {}) {
24+
if (name) {
25+
if (extension !== undefined && extension !== null) {
3626
throw new Error('The `name` and `extension` options are mutually exclusive');
3727
}
3828

39-
return path.join(tempy.directory(), options.name);
29+
return path.join(temporaryDirectory(), name);
4030
}
4131

42-
return getPath() + (options.extension === undefined || options.extension === null ? '' : '.' + options.extension.replace(/^\./, ''));
43-
};
32+
return getPath() + (extension === undefined || extension === null ? '' : '.' + extension.replace(/^\./, ''));
33+
}
4434

45-
tempy.file.task = createTask(tempy.file);
35+
export const temporaryFileTask = async (callback, options) => runTask(temporaryFile(options), callback);
4636

47-
tempy.directory = ({prefix = ''} = {}) => {
37+
export function temporaryDirectory({prefix = ''} = {}) {
4838
const directory = getPath(prefix);
4939
fs.mkdirSync(directory);
5040
return directory;
51-
};
41+
}
5242

53-
tempy.directory.task = createTask(tempy.directory);
43+
export const temporaryDirectoryTask = async (callback, options) => runTask(temporaryDirectory(options), callback);
5444

55-
tempy.write = async (data, options) => {
56-
const filename = tempy.file(options);
57-
const write = isStream(data) ? writeStream : fsPromises.writeFile;
58-
await write(filename, data);
45+
export async function temporaryWrite(fileContent, options) {
46+
const filename = temporaryFile(options);
47+
const write = isStream(fileContent) ? writeStream : fsPromises.writeFile;
48+
await write(filename, fileContent);
5949
return filename;
60-
};
50+
}
6151

62-
tempy.write.task = createTask(tempy.write, {extraArguments: 1});
52+
export const temporaryWriteTask = async (fileContent, callback, options) => runTask(await temporaryWrite(fileContent, options), callback);
6353

64-
tempy.writeSync = (data, options) => {
65-
const filename = tempy.file(options);
66-
fs.writeFileSync(filename, data);
54+
export function temporaryWriteSync(fileContent, options) {
55+
const filename = temporaryFile(options);
56+
fs.writeFileSync(filename, fileContent);
6757
return filename;
68-
};
69-
70-
Object.defineProperty(tempy, 'root', {
71-
get() {
72-
return tempDir;
73-
},
74-
});
58+
}
7559

76-
export default tempy;
60+
export {default as rootTemporaryDirectory} from 'temp-dir';

‎index.test-d.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
import process from 'node:process';
22
import {Buffer} from 'node:buffer';
33
import {expectType, expectError} from 'tsd';
4-
import tempy, {FileOptions} from './index.js';
4+
import {temporaryFile, temporaryFileTask, temporaryDirectory, temporaryDirectoryTask, temporaryWrite, temporaryWriteTask, temporaryWriteSync, rootTemporaryDirectory, FileOptions} from './index.js';
55

66
const options: FileOptions = {}; // eslint-disable-line @typescript-eslint/no-unused-vars
7-
expectType<string>(tempy.directory());
8-
expectType<string>(tempy.directory({prefix: 'name_'}));
9-
expectType<string>(tempy.file());
10-
expectType<Promise<void>>(tempy.file.task(temporaryFile => {
7+
expectType<string>(temporaryDirectory());
8+
expectType<string>(temporaryDirectory({prefix: 'name_'}));
9+
expectType<string>(temporaryFile());
10+
expectType<Promise<void>>(temporaryFileTask(temporaryFile => {
1111
expectType<string>(temporaryFile);
1212
}));
13-
expectType<Promise<void>>(tempy.directory.task(temporaryDirectory => {
13+
expectType<Promise<void>>(temporaryDirectoryTask(temporaryDirectory => {
1414
expectType<string>(temporaryDirectory);
1515
}));
16-
expectType<string>(tempy.file({extension: 'png'}));
17-
expectType<string>(tempy.file({name: 'afile.txt'}));
18-
expectError(tempy.file({extension: 'png', name: 'afile.txt'}));
19-
expectType<string>(tempy.root);
16+
expectType<string>(temporaryFile({extension: 'png'}));
17+
expectType<string>(temporaryFile({name: 'afile.txt'}));
18+
expectError(temporaryFile({extension: 'png', name: 'afile.txt'}));
19+
expectType<string>(rootTemporaryDirectory);
2020

21-
expectType<Promise<string>>(tempy.write('unicorn'));
22-
expectType<Promise<string>>(tempy.write('unicorn', {name: 'pony.png'}));
23-
expectType<Promise<string>>(tempy.write(process.stdin, {name: 'pony.png'})); // eslint-disable-line @typescript-eslint/no-unsafe-member-access
24-
expectType<Promise<string>>(tempy.write(Buffer.from('pony'), {name: 'pony.png'}));
25-
expectType<Promise<void>>(tempy.write.task('', temporaryFile => {
21+
expectType<Promise<string>>(temporaryWrite('unicorn'));
22+
expectType<Promise<string>>(temporaryWrite('unicorn', {name: 'pony.png'}));
23+
expectType<Promise<string>>(temporaryWrite(process.stdin, {name: 'pony.png'}));
24+
expectType<Promise<string>>(temporaryWrite(Buffer.from('pony'), {name: 'pony.png'}));
25+
expectType<Promise<void>>(temporaryWriteTask('', temporaryFile => {
2626
expectType<string>(temporaryFile);
2727
}));
2828

29-
expectType<string>(tempy.writeSync('unicorn'));
30-
expectType<string>(tempy.writeSync(Buffer.from('unicorn')));
31-
expectType<string>(tempy.writeSync('unicorn', {name: 'pony.png'}));
29+
expectType<string>(temporaryWriteSync('unicorn'));
30+
expectType<string>(temporaryWriteSync(Buffer.from('unicorn')));
31+
expectType<string>(temporaryWriteSync('unicorn', {name: 'pony.png'}));

‎package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"type": "module",
1414
"exports": "./index.js",
1515
"engines": {
16-
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
16+
"node": ">=14.14"
1717
},
1818
"scripts": {
1919
"test": "xo && ava && tsd"
@@ -37,17 +37,17 @@
3737
"unique"
3838
],
3939
"dependencies": {
40-
"del": "^6.0.0",
4140
"is-stream": "^3.0.0",
4241
"temp-dir": "^2.0.0",
43-
"type-fest": "^2.0.0",
42+
"type-fest": "^2.12.2",
4443
"unique-string": "^3.0.0"
4544
},
4645
"devDependencies": {
47-
"ava": "^4.0.0-alpha.2",
46+
"@types/node": "^17.0.24",
47+
"ava": "^4.2.0",
4848
"path-exists": "^5.0.0",
4949
"touch": "^3.1.0",
50-
"tsd": "^0.17.0",
51-
"xo": "^0.44.0"
50+
"tsd": "^0.20.0",
51+
"xo": "^0.48.0"
5252
}
5353
}

‎readme.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@ $ npm install tempy
1111
## Usage
1212

1313
```js
14-
import tempy from 'tempy';
14+
import {temporaryFile, temporaryDirectory} from 'tempy';
1515

16-
tempy.file();
16+
temporaryFile();
1717
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
1818

19-
tempy.file({extension: 'png'});
19+
temporaryFile({extension: 'png'});
2020
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'
2121

22-
tempy.file({name: 'unicorn.png'});
22+
temporaryFile({name: 'unicorn.png'});
2323
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'
2424

25-
tempy.directory();
25+
temporaryDirectory();
2626
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
2727

28-
tempy.directory({prefix: 'name'});
28+
temporaryDirectory({prefix: 'name'});
2929
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
3030
```
3131

3232
## API
3333

34-
### tempy.file(options?)
34+
### temporaryFile(options?)
3535

3636
Get a temporary file path you can write to.
3737

38-
### tempy.file.task(callback, options?)
38+
### temporaryFileTask(callback, options?)
3939

4040
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.
4141

@@ -63,11 +63,11 @@ Type: `string`
6363

6464
Filename. Mutually exclusive with the `extension` option.
6565

66-
### tempy.directory(options?)
66+
### temporaryDirectory(options?)
6767

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

70-
### tempy.directory.task(callback, options?)
70+
### temporaryDirectoryTask(callback, options?)
7171

7272
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.
7373

@@ -91,11 +91,11 @@ Useful for testing by making it easier to identify cache directories that are cr
9191

9292
*You usually won't need this option. Specify it only when actually needed.*
9393

94-
### tempy.write(fileContent, options?)
94+
### temporaryWrite(fileContent, options?)
9595

9696
Write data to a random temp file.
9797

98-
### tempy.write.task(fileContent, callback, options?)
98+
### temporaryWriteTask(fileContent, callback, options?)
9999

100100
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.
101101

@@ -115,7 +115,7 @@ A callback that is executed with the temp file path. Can be asynchronous.
115115

116116
See [options](#options).
117117

118-
### tempy.writeSync(fileContent, options?)
118+
### temporaryWriteSync(fileContent, options?)
119119

120120
Synchronously write data to a random temp file.
121121

@@ -129,6 +129,6 @@ Data to write to the temp file.
129129

130130
See [options](#options).
131131

132-
### tempy.root
132+
### rootTemporaryDirectory
133133

134134
Get the root temporary directory path. For example: `/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T`

‎test.js

+26-30
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,38 @@ import tempDir from 'temp-dir';
66
import {pathExists} from 'path-exists';
77
import touch from 'touch';
88
import test from 'ava';
9-
import tempy from './index.js';
9+
import {temporaryFile, temporaryFileTask, temporaryDirectory, temporaryDirectoryTask, temporaryWrite, temporaryWriteTask, temporaryWriteSync, rootTemporaryDirectory} from './index.js';
1010

1111
test('.file()', t => {
12-
t.true(tempy.file().includes(tempDir));
13-
t.false(tempy.file().endsWith('.'));
14-
t.false(tempy.file({extension: undefined}).endsWith('.'));
15-
t.false(tempy.file({extension: null}).endsWith('.'));
16-
t.true(tempy.file({extension: 'png'}).endsWith('.png'));
17-
t.true(tempy.file({extension: '.png'}).endsWith('.png'));
18-
t.false(tempy.file({extension: '.png'}).endsWith('..png'));
19-
t.true(tempy.file({name: 'custom-name.md'}).endsWith('custom-name.md'));
12+
t.true(temporaryFile().includes(tempDir));
13+
t.false(temporaryFile().endsWith('.'));
14+
t.false(temporaryFile({extension: undefined}).endsWith('.'));
15+
t.false(temporaryFile({extension: null}).endsWith('.'));
16+
t.true(temporaryFile({extension: 'png'}).endsWith('.png'));
17+
t.true(temporaryFile({extension: '.png'}).endsWith('.png'));
18+
t.false(temporaryFile({extension: '.png'}).endsWith('..png'));
19+
t.true(temporaryFile({name: 'custom-name.md'}).endsWith('custom-name.md'));
2020

2121
t.throws(() => {
22-
tempy.file({name: 'custom-name.md', extension: '.ext'});
22+
temporaryFile({name: 'custom-name.md', extension: '.ext'});
2323
});
2424

2525
t.throws(() => {
26-
tempy.file({name: 'custom-name.md', extension: ''});
26+
temporaryFile({name: 'custom-name.md', extension: ''});
2727
});
2828

2929
t.notThrows(() => {
30-
tempy.file({name: 'custom-name.md', extension: undefined});
30+
temporaryFile({name: 'custom-name.md', extension: undefined});
3131
});
3232

3333
t.notThrows(() => {
34-
tempy.file({name: 'custom-name.md', extension: null});
34+
temporaryFile({name: 'custom-name.md', extension: null});
3535
});
3636
});
3737

3838
test('.file.task()', async t => {
3939
let temporaryFilePath;
40-
t.is(await tempy.file.task(async temporaryFile => {
40+
t.is(await temporaryFileTask(async temporaryFile => {
4141
await touch(temporaryFile);
4242
temporaryFilePath = temporaryFile;
4343
return temporaryFile;
@@ -47,7 +47,7 @@ test('.file.task()', async t => {
4747

4848
test('.task() - cleans up even if callback throws', async t => {
4949
let temporaryDirectoryPath;
50-
await t.throwsAsync(tempy.directory.task(async temporaryDirectory => {
50+
await t.throwsAsync(temporaryDirectoryTask(async temporaryDirectory => {
5151
temporaryDirectoryPath = temporaryDirectory;
5252
throw new Error('Catch me if you can!');
5353
}), {
@@ -61,36 +61,36 @@ test('.task() - cleans up even if callback throws', async t => {
6161
test('.directory()', t => {
6262
const prefix = 'name_';
6363

64-
t.true(tempy.directory().includes(tempDir));
65-
t.true(path.basename(tempy.directory({prefix})).startsWith(prefix));
64+
t.true(temporaryDirectory().includes(tempDir));
65+
t.true(path.basename(temporaryDirectory({prefix})).startsWith(prefix));
6666
});
6767

6868
test('.directory.task()', async t => {
6969
let temporaryDirectoryPath;
70-
t.is(await tempy.directory.task(async temporaryDirectory => {
70+
t.is(await temporaryDirectoryTask(async temporaryDirectory => {
7171
temporaryDirectoryPath = temporaryDirectory;
7272
return temporaryDirectory;
7373
}), temporaryDirectoryPath);
7474
t.false(await pathExists(temporaryDirectoryPath));
7575
});
7676

7777
test('.write(string)', async t => {
78-
const filePath = await tempy.write('unicorn', {name: 'test.png'});
78+
const filePath = await temporaryWrite('unicorn', {name: 'test.png'});
7979
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
8080
t.is(path.basename(filePath), 'test.png');
8181
});
8282

8383
test('.write.task(string)', async t => {
8484
let temporaryFilePath;
85-
t.is(await tempy.write.task('', async temporaryFile => {
85+
t.is(await temporaryWriteTask('', async temporaryFile => {
8686
temporaryFilePath = temporaryFile;
8787
return temporaryFile;
8888
}), temporaryFilePath);
8989
t.false(await pathExists(temporaryFilePath));
9090
});
9191

9292
test('.write(buffer)', async t => {
93-
const filePath = await tempy.write(Buffer.from('unicorn'));
93+
const filePath = await temporaryWrite(Buffer.from('unicorn'));
9494
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
9595
});
9696

@@ -101,7 +101,7 @@ test('.write(stream)', async t => {
101101
readable.push('unicorn');
102102
readable.push(null); // eslint-disable-line unicorn/no-array-push-push
103103

104-
const filePath = await tempy.write(readable);
104+
const filePath = await temporaryWrite(readable);
105105
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
106106
});
107107

@@ -117,21 +117,17 @@ test('.write(stream) failing stream', async t => {
117117
readable.push(null);
118118
});
119119

120-
await t.throwsAsync(tempy.write(readable), {
120+
await t.throwsAsync(temporaryWrite(readable), {
121121
instanceOf: Error,
122122
message: 'Catch me if you can!',
123123
});
124124
});
125125

126126
test('.writeSync()', t => {
127-
t.is(fs.readFileSync(tempy.writeSync('unicorn'), 'utf8'), 'unicorn');
127+
t.is(fs.readFileSync(temporaryWriteSync('unicorn'), 'utf8'), 'unicorn');
128128
});
129129

130130
test('.root', t => {
131-
t.true(tempy.root.length > 0);
132-
t.true(path.isAbsolute(tempy.root));
133-
134-
t.throws(() => {
135-
tempy.root = 'foo';
136-
});
131+
t.true(rootTemporaryDirectory.length > 0);
132+
t.true(path.isAbsolute(rootTemporaryDirectory));
137133
});

0 commit comments

Comments
 (0)
Please sign in to comment.