Skip to content

Commit 4b3e859

Browse files
authored
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'}));

0 commit comments

Comments
 (0)