Skip to content

Commit

Permalink
Rename
Browse files Browse the repository at this point in the history
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
  • Loading branch information
Richienb committed Apr 18, 2022
1 parent ff63910 commit d5ad456
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 95 deletions.
52 changes: 26 additions & 26 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@ Get a temporary file path you can write to.
@example
```
import {tempyFile, tempyDirectory} from 'tempy';
import {temporaryFile, temporaryDirectory} from 'tempy';
tempyFile();
temporaryFile();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
tempyFile({extension: 'png'});
temporaryFile({extension: 'png'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'
tempyFile({name: 'unicorn.png'});
temporaryFile({name: 'unicorn.png'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'
tempyDirectory();
temporaryDirectory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
export function tempyFile(options?: FileOptions): string;
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.
Expand All @@ -69,31 +69,31 @@ The `callback` resolves with a temporary file path you can write to. The file is
@example
```
import {tempyFileTask} from 'tempy';
import {temporaryFileTask} from 'tempy';
await tempyFileTask(tempFile => {
await temporaryFileTask(tempFile => {
console.log(tempFile);
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
});
```
*/
export function tempyFileTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise <ReturnValueType>;
export function temporaryFileTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise <ReturnValueType>;

/**
Get a temporary directory path. The directory is created for you.
@example
```
import {tempyDirectory} from 'tempy';
import {temporaryDirectory} from 'tempy';
tempyDirectory();
temporaryDirectory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
tempyDirectory({prefix: 'a'});
temporaryDirectory({prefix: 'a'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
```
*/
export function tempyDirectory(options?: DirectoryOptions): string;
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.
Expand All @@ -102,27 +102,27 @@ The `callback` resolves with a temporary directory path you can write to. The di
@example
```
import {tempyDirectoryTask} from 'tempy';
import {temporaryDirectoryTask} from 'tempy';
await tempyDirectoryTask(tempDirectory => {
await temporaryDirectoryTask(tempDirectory => {
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
})
```
*/
export function tempyDirectoryTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: DirectoryOptions): Promise<ReturnValueType>;
export function temporaryDirectoryTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: DirectoryOptions): Promise<ReturnValueType>;

/**
Write data to a random temp file.
@example
```
import {tempyWrite} from 'tempy';
import {temporaryWrite} from 'tempy';
await tempyWrite('🦄');
await temporaryWrite('🦄');
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
export function tempyWrite(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: FileOptions): Promise<string>;
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.
Expand All @@ -131,26 +131,26 @@ Write data to a random temp file. The file is automatically cleaned up after the
@example
```
import {tempyWriteTask} from 'tempy';
import {temporaryWriteTask} from 'tempy';
await tempyWriteTask('🦄', tempFile => {
await temporaryWriteTask('🦄', tempFile => {
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
});
```
*/
export function tempyWriteTask<ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise<ReturnValueType>;
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 {tempyWriteSync} from 'tempy';
import {temporaryWriteSync} from 'tempy';
tempyWriteSync('🦄');
temporaryWriteSync('🦄');
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
export function tempyWriteSync(fileContent: string | Buffer | TypedArray | DataView, options?: FileOptions): string;
export function temporaryWriteSync(fileContent: string | Buffer | TypedArray | DataView, options?: FileOptions): string;

export {default as tempyRoot} from 'temp-dir';
export {default as temporaryRoot} from 'temp-dir';
22 changes: 11 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,41 @@ async function runTask(temporaryPath, callback) {
}
}

export function tempyFile({name, extension} = {}) {
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(tempyDirectory(), name);
return path.join(temporaryDirectory(), name);
}

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

export const tempyFileTask = async (callback, options) => runTask(tempyFile(options), callback);
export const temporaryFileTask = async (callback, options) => runTask(temporaryFile(options), callback);

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

export const tempyDirectoryTask = async (callback, options) => runTask(tempyDirectory(options), callback);
export const temporaryDirectoryTask = async (callback, options) => runTask(temporaryDirectory(options), callback);

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

export const tempyWriteTask = async (fileContent, callback, options) => runTask(await tempyWrite(fileContent, options), callback);
export const temporaryWriteTask = async (fileContent, callback, options) => runTask(await temporaryWrite(fileContent, options), callback);

export function tempyWriteSync(fileContent, options) {
const filename = tempyFile(options);
export function temporaryWriteSync(fileContent, options) {
const filename = temporaryFile(options);
fs.writeFileSync(filename, fileContent);
return filename;
}

export {default as tempyRoot} from 'temp-dir';
export {default as temporaryRoot} 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 {tempyFile, tempyFileTask, tempyDirectory, tempyDirectoryTask, tempyWrite, tempyWriteTask, tempyWriteSync, tempyRoot, FileOptions} from './index.js';
import {temporaryFile, temporaryFileTask, temporaryDirectory, temporaryDirectoryTask, temporaryWrite, temporaryWriteTask, temporaryWriteSync, temporaryRoot, FileOptions} from './index.js';

const options: FileOptions = {}; // eslint-disable-line @typescript-eslint/no-unused-vars
expectType<string>(tempyDirectory());
expectType<string>(tempyDirectory({prefix: 'name_'}));
expectType<string>(tempyFile());
expectType<Promise<void>>(tempyFileTask(temporaryFile => {
expectType<string>(temporaryDirectory());
expectType<string>(temporaryDirectory({prefix: 'name_'}));
expectType<string>(temporaryFile());
expectType<Promise<void>>(temporaryFileTask(temporaryFile => {
expectType<string>(temporaryFile);
}));
expectType<Promise<void>>(tempyDirectoryTask(temporaryDirectory => {
expectType<Promise<void>>(temporaryDirectoryTask(temporaryDirectory => {
expectType<string>(temporaryDirectory);
}));
expectType<string>(tempyFile({extension: 'png'}));
expectType<string>(tempyFile({name: 'afile.txt'}));
expectError(tempyFile({extension: 'png', name: 'afile.txt'}));
expectType<string>(tempyRoot);
expectType<string>(temporaryFile({extension: 'png'}));
expectType<string>(temporaryFile({name: 'afile.txt'}));
expectError(temporaryFile({extension: 'png', name: 'afile.txt'}));
expectType<string>(temporaryRoot);

expectType<Promise<string>>(tempyWrite('unicorn'));
expectType<Promise<string>>(tempyWrite('unicorn', {name: 'pony.png'}));
expectType<Promise<string>>(tempyWrite(process.stdin, {name: 'pony.png'}));
expectType<Promise<string>>(tempyWrite(Buffer.from('pony'), {name: 'pony.png'}));
expectType<Promise<void>>(tempyWriteTask('', 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>(tempyWriteSync('unicorn'));
expectType<string>(tempyWriteSync(Buffer.from('unicorn')));
expectType<string>(tempyWriteSync('unicorn', {name: 'pony.png'}));
expectType<string>(temporaryWriteSync('unicorn'));
expectType<string>(temporaryWriteSync(Buffer.from('unicorn')));
expectType<string>(temporaryWriteSync('unicorn', {name: 'pony.png'}));
28 changes: 14 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ $ npm install tempy
## Usage

```js
import {tempyFile, tempyDirectory} from 'tempy';
import {temporaryFile, temporaryDirectory} from 'tempy';

tempyFile();
temporaryFile();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'

tempyFile({extension: 'png'});
temporaryFile({extension: 'png'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'

tempyFile({name: 'unicorn.png'});
temporaryFile({name: 'unicorn.png'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'

tempyDirectory();
temporaryDirectory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'

tempyDirectory({prefix: 'name'});
temporaryDirectory({prefix: 'name'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
```

## API

### tempyFile(options?)
### temporaryFile(options?)

Get a temporary file path you can write to.

### tempyFileTask(callback, options?)
### temporaryFileTask(callback, options?)

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.

Expand Down Expand Up @@ -63,11 +63,11 @@ Type: `string`

Filename. Mutually exclusive with the `extension` option.

### tempyDirectory(options?)
### temporaryDirectory(options?)

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

### tempyDirectoryTask(callback, options?)
### temporaryDirectoryTask(callback, options?)

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.

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

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

### tempyWrite(fileContent, options?)
### temporaryWrite(fileContent, options?)

Write data to a random temp file.

### tempyWriteTask(fileContent, callback, options?)
### temporaryWriteTask(fileContent, callback, options?)

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.

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

See [options](#options).

### tempyWriteSync(fileContent, options?)
### temporaryWriteSync(fileContent, options?)

Synchronously write data to a random temp file.

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

See [options](#options).

### tempyRoot
### temporaryRoot

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

0 comments on commit d5ad456

Please sign in to comment.