Skip to content

Commit

Permalink
Require Node.js 14 (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb committed Apr 18, 2022
1 parent 578a8ac commit 4b3e859
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 232 deletions.
230 changes: 107 additions & 123 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/member-ordering */
import {Buffer} from 'node:buffer';
import {MergeExclusive, TypedArray} from 'type-fest';

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

declare const tempy: {
file: {
/**
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 after the callback is executed and the file is cleaned up.
@example
```
import tempy from 'tempy';
await tempy.file.task(tempFile => {
console.log(tempFile);
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
});
```
*/
task: <ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: FileOptions) => Promise<ReturnValueType>;

/**
Get a temporary file path you can write to.
@example
```
import tempy from 'tempy';
tempy.file();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
tempy.file({extension: 'png'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'
tempy.file({name: 'unicorn.png'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'
tempy.directory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
(options?: FileOptions): string;
};

directory: {
/**
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 after the callback is executed and the directory is cleaned up.
@example
```
import tempy from 'tempy';
await tempy.directory.task(tempDirectory => {
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
})
```
*/
task: <ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: DirectoryOptions) => Promise<ReturnValueType>;

/**
Get a temporary directory path. The directory is created for you.
@example
```
import tempy from 'tempy';
tempy.directory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
tempy.directory({prefix: 'a'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
```
*/
(options?: DirectoryOptions): string;
};

write: {
/**
Write data to a random temp file. The file is automatically cleaned up after the callback is executed.
@returns A promise that resolves after the callback is executed and the file is cleaned up.
@example
```
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: TaskCallback<ReturnValueType>, options?: FileOptions) => Promise<ReturnValueType>;

/**
Write data to a random temp file.
@example
```
import tempy from 'tempy';
await tempy.write('🦄');
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: FileOptions): Promise<string>;
};
/**
Get a temporary file path you can write to.
/**
Synchronously write data to a random temp file.
@example
```
import {temporaryFile, temporaryDirectory} from 'tempy';
temporaryFile();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
temporaryFile({extension: 'png'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'
temporaryFile({name: 'unicorn.png'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'
temporaryDirectory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
export function temporaryFile(options?: FileOptions): string;

/**
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 after the callback is executed and the file is cleaned up.
@example
```
import {temporaryFileTask} from 'tempy';
await temporaryFileTask(tempFile => {
console.log(tempFile);
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
});
```
*/
export function temporaryFileTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise <ReturnValueType>;

/**
Get a temporary directory path. The directory is created for you.
@example
```
import {temporaryDirectory} from 'tempy';
temporaryDirectory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
temporaryDirectory({prefix: 'a'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
```
*/
export function temporaryDirectory(options?: DirectoryOptions): string;

/**
The `callback` resolves with a temporary directory path you can write to. The directory is automatically cleaned up after the callback is executed.
@example
```
import tempy from 'tempy';
@returns A promise that resolves after the callback is executed and the directory is cleaned up.
tempy.writeSync('🦄');
@example
```
import {temporaryDirectoryTask} from 'tempy';
await temporaryDirectoryTask(tempDirectory => {
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
writeSync: (fileContent: string | Buffer | TypedArray | DataView, options?: FileOptions) => string;
})
```
*/
export function temporaryDirectoryTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: DirectoryOptions): Promise<ReturnValueType>;

/**
Get the root temporary directory path.
/**
Write data to a random temp file.
For example: `/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T`.
*/
readonly root: string;
};
@example
```
import {temporaryWrite} from 'tempy';
await temporaryWrite('🦄');
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
export function temporaryWrite(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: FileOptions): Promise<string>;

/**
Write data to a random temp file. The file is automatically cleaned up after the callback is executed.
@returns A promise that resolves after the callback is executed and the file is cleaned up.
@example
```
import {temporaryWriteTask} from 'tempy';
await temporaryWriteTask('🦄', tempFile => {
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
});
```
*/
export function temporaryWriteTask<ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise<ReturnValueType>;

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

export default tempy;
export {default as rootTemporaryDirectory} from 'temp-dir';
66 changes: 25 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,56 @@ 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());

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

const createTask = (tempyFunction, {extraArguments = 0} = {}) => async (...arguments_) => {
const [callback, options] = arguments_.slice(extraArguments);
const result = await tempyFunction(...arguments_.slice(0, extraArguments), options);

async function runTask(temporaryPath, callback) {
try {
return await callback(result);
return await callback(temporaryPath);
} finally {
await del(result, {force: true});
await fsPromises.rm(temporaryPath, {recursive: true, force: true});
}
};

const tempy = {};
}

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

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

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

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

tempy.file.task = createTask(tempy.file);
export const temporaryFileTask = async (callback, options) => runTask(temporaryFile(options), callback);

tempy.directory = ({prefix = ''} = {}) => {
export function temporaryDirectory({prefix = ''} = {}) {
const directory = getPath(prefix);
fs.mkdirSync(directory);
return directory;
};
}

tempy.directory.task = createTask(tempy.directory);
export const temporaryDirectoryTask = async (callback, options) => runTask(temporaryDirectory(options), callback);

tempy.write = async (data, options) => {
const filename = tempy.file(options);
const write = isStream(data) ? writeStream : fsPromises.writeFile;
await write(filename, data);
export async function temporaryWrite(fileContent, options) {
const filename = temporaryFile(options);
const write = isStream(fileContent) ? writeStream : fsPromises.writeFile;
await write(filename, fileContent);
return filename;
};
}

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

tempy.writeSync = (data, options) => {
const filename = tempy.file(options);
fs.writeFileSync(filename, data);
export function temporaryWriteSync(fileContent, options) {
const filename = temporaryFile(options);
fs.writeFileSync(filename, fileContent);
return filename;
};

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

export default tempy;
export {default as rootTemporaryDirectory} from 'temp-dir';
36 changes: 18 additions & 18 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import process from 'node:process';
import {Buffer} from 'node:buffer';
import {expectType, expectError} from 'tsd';
import tempy, {FileOptions} from './index.js';
import {temporaryFile, temporaryFileTask, temporaryDirectory, temporaryDirectoryTask, temporaryWrite, temporaryWriteTask, temporaryWriteSync, rootTemporaryDirectory, FileOptions} from './index.js';

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());
expectType<Promise<void>>(tempy.file.task(temporaryFile => {
expectType<string>(temporaryDirectory());
expectType<string>(temporaryDirectory({prefix: 'name_'}));
expectType<string>(temporaryFile());
expectType<Promise<void>>(temporaryFileTask(temporaryFile => {
expectType<string>(temporaryFile);
}));
expectType<Promise<void>>(tempy.directory.task(temporaryDirectory => {
expectType<Promise<void>>(temporaryDirectoryTask(temporaryDirectory => {
expectType<string>(temporaryDirectory);
}));
expectType<string>(tempy.file({extension: 'png'}));
expectType<string>(tempy.file({name: 'afile.txt'}));
expectError(tempy.file({extension: 'png', name: 'afile.txt'}));
expectType<string>(tempy.root);
expectType<string>(temporaryFile({extension: 'png'}));
expectType<string>(temporaryFile({name: 'afile.txt'}));
expectError(temporaryFile({extension: 'png', name: 'afile.txt'}));
expectType<string>(rootTemporaryDirectory);

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'})); // 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<Promise<string>>(temporaryWrite('unicorn'));
expectType<Promise<string>>(temporaryWrite('unicorn', {name: 'pony.png'}));
expectType<Promise<string>>(temporaryWrite(process.stdin, {name: 'pony.png'}));
expectType<Promise<string>>(temporaryWrite(Buffer.from('pony'), {name: 'pony.png'}));
expectType<Promise<void>>(temporaryWriteTask('', temporaryFile => {
expectType<string>(temporaryFile);
}));

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

0 comments on commit 4b3e859

Please sign in to comment.