Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 18, 2021
1 parent 0e498c9 commit ca35430
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 109 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
95 changes: 47 additions & 48 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
/// <reference types="node"/>
/* eslint-disable @typescript-eslint/member-ordering */
import {Buffer} from 'node:buffer';
import {MergeExclusive, TypedArray} from 'type-fest';

declare namespace tempy {
type FileOptions = MergeExclusive<
{
/**
File extension.
Mutually exclusive with the `name` option.
export type FileOptions = MergeExclusive<
{
/**
File extension.
_You usually won't need this option. Specify it only when actually needed._
*/
readonly extension?: string;
},
{
/**
Filename.
Mutually exclusive with the `name` option.
Mutually exclusive with the `extension` option.
_You usually won't need this option. Specify it only when actually needed._
*/
readonly extension?: string;
},
{
/**
Filename.
_You usually won't need this option. Specify it only when actually needed._
*/
readonly name?: string;
}
>;
Mutually exclusive with the `extension` option.
type DirectoryOptions = {
/**
_You usually won't need this option. Specify it only when actually needed._
_You usually won't need this option. Specify it only when actually needed._
*/
readonly name?: string;
}
>;

Directory prefix.
export type DirectoryOptions = {
/**
Directory prefix.
Useful for testing by making it easier to identify cache directories that are created.
*/
readonly prefix?: string;
};
_You usually won't need this option. Specify it only when actually needed._
/**
The temporary path created by the function. Can be asynchronous.
Useful for testing by making it easier to identify cache directories that are created.
*/
type TaskCallback<ReturnValueType> = (tempPath: string) => Promise<ReturnValueType> | ReturnValueType;
}
readonly prefix?: string;
};

/**
The temporary path created by the function. Can be asynchronous.
*/
export type TaskCallback<ReturnValueType> = (temporaryPath: string) => Promise<ReturnValueType> | ReturnValueType;

declare const tempy: {
file: {
Expand All @@ -51,22 +50,22 @@ declare const tempy: {
@example
```
import tempy = require('tempy');
import tempy from 'tempy';
await tempy.file.task(tempFile => {
console.log(tempFile);
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
});
```
*/
task: <ReturnValueType>(callback: tempy.TaskCallback<ReturnValueType>, options?: tempy.FileOptions) => Promise<ReturnValueType>;
task: <ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: FileOptions) => Promise<ReturnValueType>;

/**
Get a temporary file path you can write to.
@example
```
import tempy = require('tempy');
import tempy from 'tempy';
tempy.file();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
Expand All @@ -81,7 +80,7 @@ declare const tempy: {
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
(options?: tempy.FileOptions): string;
(options?: FileOptions): string;
};

directory: {
Expand All @@ -92,21 +91,21 @@ declare const tempy: {
@example
```
import tempy = require('tempy');
import tempy from 'tempy';
await tempy.directory.task(tempDirectory => {
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
})
```
*/
task: <ReturnValueType>(callback: tempy.TaskCallback<ReturnValueType>, options?: tempy.DirectoryOptions) => Promise<ReturnValueType>;
task: <ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: DirectoryOptions) => Promise<ReturnValueType>;

/**
Get a temporary directory path. The directory is created for you.
@example
```
import tempy = require('tempy');
import tempy from 'tempy';
tempy.directory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
Expand All @@ -115,7 +114,7 @@ declare const tempy: {
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
```
*/
(options?: tempy.DirectoryOptions): string;
(options?: DirectoryOptions): string;
};

write: {
Expand All @@ -126,41 +125,41 @@ declare const tempy: {
@example
```
import tempy = require('tempy');
import tempy from 'tempy';
await tempy.write.task('🦄', tempFile => {
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
});
```
*/
task: <ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: tempy.TaskCallback<ReturnValueType>, options?: tempy.FileOptions) => Promise<ReturnValueType>;
task: <ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: TaskCallback<ReturnValueType>, options?: FileOptions) => Promise<ReturnValueType>;

/**
Write data to a random temp file.
@example
```
import tempy = require('tempy');
import tempy from 'tempy';
await tempy.write('🦄');
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: tempy.FileOptions): Promise<string>;
(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: FileOptions): Promise<string>;
};

/**
Synchronously write data to a random temp file.
@example
```
import tempy = require('tempy');
import tempy from 'tempy';
tempy.writeSync('🦄');
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
writeSync: (fileContent: string | Buffer | TypedArray | DataView, options?: tempy.FileOptions) => string;
writeSync: (fileContent: string | Buffer | TypedArray | DataView, options?: FileOptions) => string;

/**
Get the root temporary directory path.
Expand All @@ -170,4 +169,4 @@ declare const tempy: {
readonly root: string;
};

export = tempy;
export default tempy;
54 changes: 28 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict';
const fs = require('fs');
const path = require('path');
const uniqueString = require('unique-string');
const tempDir = require('temp-dir');
const isStream = require('is-stream');
const del = require('del');
const stream = require('stream');
const {promisify} = require('util');

const pipeline = promisify(stream.pipeline);
const {writeFile} = fs.promises;
import fs, {promises as fsPromises} from 'node:fs';
import path from 'node:path';
import stream from 'node:stream';
import {promisify} from 'node:util';
import uniqueString from 'unique-string';
import tempDir from 'temp-dir';
import {isStream} from 'is-stream';
import del from 'del'; // TODO: Replace this with `fs.rm` when targeting Node.js 14.

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

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

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

module.exports.file = options => {
const tempy = {};

tempy.file = options => {
options = {
...options
...options,
};

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

return path.join(module.exports.directory(), options.name);
return path.join(tempy.directory(), options.name);
}

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

module.exports.file.task = createTask(module.exports.file);
tempy.file.task = createTask(tempy.file);

module.exports.directory = ({prefix = ''} = {}) => {
tempy.directory = ({prefix = ''} = {}) => {
const directory = getPath(prefix);
fs.mkdirSync(directory);
return directory;
};

module.exports.directory.task = createTask(module.exports.directory);
tempy.directory.task = createTask(tempy.directory);

module.exports.write = async (data, options) => {
const filename = module.exports.file(options);
const write = isStream(data) ? writeStream : writeFile;
tempy.write = async (data, options) => {
const filename = tempy.file(options);
const write = isStream(data) ? writeStream : fsPromises.writeFile;
await write(filename, data);
return filename;
};

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

module.exports.writeSync = (data, options) => {
const filename = module.exports.file(options);
tempy.writeSync = (data, options) => {
const filename = tempy.file(options);
fs.writeFileSync(filename, data);
return filename;
};

Object.defineProperty(module.exports, 'root', {
Object.defineProperty(tempy, 'root', {
get() {
return tempDir;
}
},
});

export default tempy;
8 changes: 5 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import process from 'node:process';
import {Buffer} from 'node:buffer';
import {expectType, expectError} from 'tsd';
import tempy = require('.');
import tempy, {FileOptions} from './index.js';

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

expectType<Promise<string>>(tempy.write('unicorn'));
expectType<Promise<string>>(tempy.write('unicorn', {name: 'pony.png'}));
expectType<Promise<string>>(tempy.write(process.stdin, {name: 'pony.png'}));
expectType<Promise<string>>(tempy.write(process.stdin, {name: 'pony.png'})); // eslint-disable-line @typescript-eslint/no-unsafe-member-access
expectType<Promise<string>>(tempy.write(Buffer.from('pony'), {name: 'pony.png'}));
expectType<Promise<void>>(tempy.write.task('', temporaryFile => {
expectType<string>(temporaryFile);
Expand Down
23 changes: 10 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -36,21 +38,16 @@
],
"dependencies": {
"del": "^6.0.0",
"is-stream": "^2.0.0",
"is-stream": "^3.0.0",
"temp-dir": "^2.0.0",
"type-fest": "^0.16.0",
"unique-string": "^2.0.0"
"type-fest": "^2.0.0",
"unique-string": "^3.0.0"
},
"devDependencies": {
"ava": "^2.4.0",
"path-exists": "^4.0.0",
"ava": "^4.0.0-alpha.2",
"path-exists": "^5.0.0",
"touch": "^3.1.0",
"tsd": "^0.13.1",
"xo": "^0.33.1"
},
"xo": {
"rules": {
"node/no-unsupported-features/node-builtins": "off"
}
"tsd": "^0.17.0",
"xo": "^0.44.0"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $ npm install tempy
## Usage

```js
const tempy = require('tempy');
import tempy from 'tempy';

tempy.file();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
Expand Down
Loading

0 comments on commit ca35430

Please sign in to comment.