Skip to content

Commit ca35430

Browse files
committed
Require Node.js 12.20 and move to ESM
1 parent 0e498c9 commit ca35430

File tree

8 files changed

+109
-109
lines changed

8 files changed

+109
-109
lines changed

.github/funding.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

.github/workflows/main.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
13+
- 16
1614
steps:
1715
- uses: actions/checkout@v2
18-
- uses: actions/setup-node@v1
16+
- uses: actions/setup-node@v2
1917
with:
2018
node-version: ${{ matrix.node-version }}
2119
- run: npm install

index.d.ts

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
1-
/// <reference types="node"/>
1+
/* eslint-disable @typescript-eslint/member-ordering */
2+
import {Buffer} from 'node:buffer';
23
import {MergeExclusive, TypedArray} from 'type-fest';
34

4-
declare namespace tempy {
5-
type FileOptions = MergeExclusive<
6-
{
7-
/**
8-
File extension.
9-
10-
Mutually exclusive with the `name` option.
5+
export type FileOptions = MergeExclusive<
6+
{
7+
/**
8+
File extension.
119
12-
_You usually won't need this option. Specify it only when actually needed._
13-
*/
14-
readonly extension?: string;
15-
},
16-
{
17-
/**
18-
Filename.
10+
Mutually exclusive with the `name` option.
1911
20-
Mutually exclusive with the `extension` option.
12+
_You usually won't need this option. Specify it only when actually needed._
13+
*/
14+
readonly extension?: string;
15+
},
16+
{
17+
/**
18+
Filename.
2119
22-
_You usually won't need this option. Specify it only when actually needed._
23-
*/
24-
readonly name?: string;
25-
}
26-
>;
20+
Mutually exclusive with the `extension` option.
2721
28-
type DirectoryOptions = {
29-
/**
30-
_You usually won't need this option. Specify it only when actually needed._
22+
_You usually won't need this option. Specify it only when actually needed._
23+
*/
24+
readonly name?: string;
25+
}
26+
>;
3127

32-
Directory prefix.
28+
export type DirectoryOptions = {
29+
/**
30+
Directory prefix.
3331
34-
Useful for testing by making it easier to identify cache directories that are created.
35-
*/
36-
readonly prefix?: string;
37-
};
32+
_You usually won't need this option. Specify it only when actually needed._
3833
39-
/**
40-
The temporary path created by the function. Can be asynchronous.
34+
Useful for testing by making it easier to identify cache directories that are created.
4135
*/
42-
type TaskCallback<ReturnValueType> = (tempPath: string) => Promise<ReturnValueType> | ReturnValueType;
43-
}
36+
readonly prefix?: string;
37+
};
38+
39+
/**
40+
The temporary path created by the function. Can be asynchronous.
41+
*/
42+
export type TaskCallback<ReturnValueType> = (temporaryPath: string) => Promise<ReturnValueType> | ReturnValueType;
4443

4544
declare const tempy: {
4645
file: {
@@ -51,22 +50,22 @@ declare const tempy: {
5150
5251
@example
5352
```
54-
import tempy = require('tempy');
53+
import tempy from 'tempy';
5554
5655
await tempy.file.task(tempFile => {
5756
console.log(tempFile);
5857
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
5958
});
6059
```
6160
*/
62-
task: <ReturnValueType>(callback: tempy.TaskCallback<ReturnValueType>, options?: tempy.FileOptions) => Promise<ReturnValueType>;
61+
task: <ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: FileOptions) => Promise<ReturnValueType>;
6362

6463
/**
6564
Get a temporary file path you can write to.
6665
6766
@example
6867
```
69-
import tempy = require('tempy');
68+
import tempy from 'tempy';
7069
7170
tempy.file();
7271
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
@@ -81,7 +80,7 @@ declare const tempy: {
8180
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
8281
```
8382
*/
84-
(options?: tempy.FileOptions): string;
83+
(options?: FileOptions): string;
8584
};
8685

8786
directory: {
@@ -92,21 +91,21 @@ declare const tempy: {
9291
9392
@example
9493
```
95-
import tempy = require('tempy');
94+
import tempy from 'tempy';
9695
9796
await tempy.directory.task(tempDirectory => {
9897
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
9998
})
10099
```
101100
*/
102-
task: <ReturnValueType>(callback: tempy.TaskCallback<ReturnValueType>, options?: tempy.DirectoryOptions) => Promise<ReturnValueType>;
101+
task: <ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: DirectoryOptions) => Promise<ReturnValueType>;
103102

104103
/**
105104
Get a temporary directory path. The directory is created for you.
106105
107106
@example
108107
```
109-
import tempy = require('tempy');
108+
import tempy from 'tempy';
110109
111110
tempy.directory();
112111
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
@@ -115,7 +114,7 @@ declare const tempy: {
115114
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
116115
```
117116
*/
118-
(options?: tempy.DirectoryOptions): string;
117+
(options?: DirectoryOptions): string;
119118
};
120119

121120
write: {
@@ -126,41 +125,41 @@ declare const tempy: {
126125
127126
@example
128127
```
129-
import tempy = require('tempy');
128+
import tempy from 'tempy';
130129
131130
await tempy.write.task('🦄', tempFile => {
132131
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
133132
});
134133
```
135134
*/
136-
task: <ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: tempy.TaskCallback<ReturnValueType>, options?: tempy.FileOptions) => Promise<ReturnValueType>;
135+
task: <ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: TaskCallback<ReturnValueType>, options?: FileOptions) => Promise<ReturnValueType>;
137136

138137
/**
139138
Write data to a random temp file.
140139
141140
@example
142141
```
143-
import tempy = require('tempy');
142+
import tempy from 'tempy';
144143
145144
await tempy.write('🦄');
146145
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
147146
```
148147
*/
149-
(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: tempy.FileOptions): Promise<string>;
148+
(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: FileOptions): Promise<string>;
150149
};
151150

152151
/**
153152
Synchronously write data to a random temp file.
154153
155154
@example
156155
```
157-
import tempy = require('tempy');
156+
import tempy from 'tempy';
158157
159158
tempy.writeSync('🦄');
160159
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
161160
```
162161
*/
163-
writeSync: (fileContent: string | Buffer | TypedArray | DataView, options?: tempy.FileOptions) => string;
162+
writeSync: (fileContent: string | Buffer | TypedArray | DataView, options?: FileOptions) => string;
164163

165164
/**
166165
Get the root temporary directory path.
@@ -170,4 +169,4 @@ declare const tempy: {
170169
readonly root: string;
171170
};
172171

173-
export = tempy;
172+
export default tempy;

index.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
'use strict';
2-
const fs = require('fs');
3-
const path = require('path');
4-
const uniqueString = require('unique-string');
5-
const tempDir = require('temp-dir');
6-
const isStream = require('is-stream');
7-
const del = require('del');
8-
const stream = require('stream');
9-
const {promisify} = require('util');
10-
11-
const pipeline = promisify(stream.pipeline);
12-
const {writeFile} = fs.promises;
1+
import fs, {promises as fsPromises} from 'node:fs';
2+
import path from 'node:path';
3+
import stream from 'node:stream';
4+
import {promisify} from 'node:util';
5+
import uniqueString from 'unique-string';
6+
import tempDir from 'temp-dir';
7+
import {isStream} from 'is-stream';
8+
import del from 'del'; // TODO: Replace this with `fs.rm` when targeting Node.js 14.
9+
10+
const pipeline = promisify(stream.pipeline); // TODO: Use `node:stream/promises` when targeting Node.js 16.
1311

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

@@ -26,49 +24,53 @@ const createTask = (tempyFunction, {extraArguments = 0} = {}) => async (...argum
2624
}
2725
};
2826

29-
module.exports.file = options => {
27+
const tempy = {};
28+
29+
tempy.file = options => {
3030
options = {
31-
...options
31+
...options,
3232
};
3333

3434
if (options.name) {
3535
if (options.extension !== undefined && options.extension !== null) {
3636
throw new Error('The `name` and `extension` options are mutually exclusive');
3737
}
3838

39-
return path.join(module.exports.directory(), options.name);
39+
return path.join(tempy.directory(), options.name);
4040
}
4141

4242
return getPath() + (options.extension === undefined || options.extension === null ? '' : '.' + options.extension.replace(/^\./, ''));
4343
};
4444

45-
module.exports.file.task = createTask(module.exports.file);
45+
tempy.file.task = createTask(tempy.file);
4646

47-
module.exports.directory = ({prefix = ''} = {}) => {
47+
tempy.directory = ({prefix = ''} = {}) => {
4848
const directory = getPath(prefix);
4949
fs.mkdirSync(directory);
5050
return directory;
5151
};
5252

53-
module.exports.directory.task = createTask(module.exports.directory);
53+
tempy.directory.task = createTask(tempy.directory);
5454

55-
module.exports.write = async (data, options) => {
56-
const filename = module.exports.file(options);
57-
const write = isStream(data) ? writeStream : writeFile;
55+
tempy.write = async (data, options) => {
56+
const filename = tempy.file(options);
57+
const write = isStream(data) ? writeStream : fsPromises.writeFile;
5858
await write(filename, data);
5959
return filename;
6060
};
6161

62-
module.exports.write.task = createTask(module.exports.write, {extraArguments: 1});
62+
tempy.write.task = createTask(tempy.write, {extraArguments: 1});
6363

64-
module.exports.writeSync = (data, options) => {
65-
const filename = module.exports.file(options);
64+
tempy.writeSync = (data, options) => {
65+
const filename = tempy.file(options);
6666
fs.writeFileSync(filename, data);
6767
return filename;
6868
};
6969

70-
Object.defineProperty(module.exports, 'root', {
70+
Object.defineProperty(tempy, 'root', {
7171
get() {
7272
return tempDir;
73-
}
73+
},
7474
});
75+
76+
export default tempy;

index.test-d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import process from 'node:process';
2+
import {Buffer} from 'node:buffer';
13
import {expectType, expectError} from 'tsd';
2-
import tempy = require('.');
4+
import tempy, {FileOptions} from './index.js';
35

4-
const options: tempy.FileOptions = {}; // eslint-disable-line @typescript-eslint/no-unused-vars
6+
const options: FileOptions = {}; // eslint-disable-line @typescript-eslint/no-unused-vars
57
expectType<string>(tempy.directory());
68
expectType<string>(tempy.directory({prefix: 'name_'}));
79
expectType<string>(tempy.file());
@@ -18,7 +20,7 @@ expectType<string>(tempy.root);
1820

1921
expectType<Promise<string>>(tempy.write('unicorn'));
2022
expectType<Promise<string>>(tempy.write('unicorn', {name: 'pony.png'}));
21-
expectType<Promise<string>>(tempy.write(process.stdin, {name: 'pony.png'}));
23+
expectType<Promise<string>>(tempy.write(process.stdin, {name: 'pony.png'})); // eslint-disable-line @typescript-eslint/no-unsafe-member-access
2224
expectType<Promise<string>>(tempy.write(Buffer.from('pony'), {name: 'pony.png'}));
2325
expectType<Promise<void>>(tempy.write.task('', temporaryFile => {
2426
expectType<string>(temporaryFile);

package.json

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
"email": "sindresorhus@gmail.com",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=10"
16+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1517
},
1618
"scripts": {
1719
"test": "xo && ava && tsd"
@@ -36,21 +38,16 @@
3638
],
3739
"dependencies": {
3840
"del": "^6.0.0",
39-
"is-stream": "^2.0.0",
41+
"is-stream": "^3.0.0",
4042
"temp-dir": "^2.0.0",
41-
"type-fest": "^0.16.0",
42-
"unique-string": "^2.0.0"
43+
"type-fest": "^2.0.0",
44+
"unique-string": "^3.0.0"
4345
},
4446
"devDependencies": {
45-
"ava": "^2.4.0",
46-
"path-exists": "^4.0.0",
47+
"ava": "^4.0.0-alpha.2",
48+
"path-exists": "^5.0.0",
4749
"touch": "^3.1.0",
48-
"tsd": "^0.13.1",
49-
"xo": "^0.33.1"
50-
},
51-
"xo": {
52-
"rules": {
53-
"node/no-unsupported-features/node-builtins": "off"
54-
}
50+
"tsd": "^0.17.0",
51+
"xo": "^0.44.0"
5552
}
5653
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ $ npm install tempy
1111
## Usage
1212

1313
```js
14-
const tempy = require('tempy');
14+
import tempy from 'tempy';
1515

1616
tempy.file();
1717
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'

0 commit comments

Comments
 (0)