diff --git a/index.d.ts b/index.d.ts index 4868cef..25cb52b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2,7 +2,7 @@ import {MergeExclusive, TypedArray} from 'type-fest'; declare namespace tempy { - type Options = MergeExclusive< + type FileOptions = MergeExclusive< { /** File extension. @@ -24,6 +24,17 @@ declare namespace tempy { readonly name?: string; } >; + + type DirectoryOptions = { + /** + _You usually won't need this option. Specify it only when actually needed._ + + Directory prefix. + + Useful for testing by making it easier to identify cache directories that are created. + */ + readonly prefix?: string; + } } declare const tempy: { @@ -47,7 +58,7 @@ declare const tempy: { //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' ``` */ - file(options?: tempy.Options): string; + file(options?: tempy.FileOptions): string; /** Get a temporary directory path. The directory is created for you. @@ -58,9 +69,12 @@ declare const tempy: { tempy.directory(); //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' + + tempy.directory({prefix: 'a'}); + //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41' ``` */ - directory(): string; + directory(options?: tempy.DirectoryOptions): string; /** Write data to a random temp file. @@ -73,7 +87,7 @@ declare const tempy: { //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' ``` */ - write(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: tempy.Options): Promise; + write(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: tempy.FileOptions): Promise; /** Synchronously write data to a random temp file. @@ -86,7 +100,7 @@ declare const tempy: { //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' ``` */ - writeSync(fileContent: string | Buffer | TypedArray | DataView, options?: tempy.Options): string; + writeSync(fileContent: string | Buffer | TypedArray | DataView, options?: tempy.FileOptions): string; /** Get the root temporary directory path. diff --git a/index.js b/index.js index 4bdbe9c..1960918 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,7 @@ const {promisify} = require('util'); const pipeline = promisify(stream.pipeline); const {writeFile} = fs.promises; -const getPath = () => path.join(tempDir, uniqueString()); +const getPath = (prefix = '') => path.join(tempDir, prefix + uniqueString()); const writeStream = async (filePath, data) => pipeline(data, fs.createWriteStream(filePath)); @@ -30,8 +30,8 @@ module.exports.file = options => { return getPath() + (options.extension === undefined || options.extension === null ? '' : '.' + options.extension.replace(/^\./, '')); }; -module.exports.directory = () => { - const directory = getPath(); +module.exports.directory = ({prefix = ''} = {}) => { + const directory = getPath(prefix); fs.mkdirSync(directory); return directory; }; diff --git a/index.test-d.ts b/index.test-d.ts index 54eac11..11cd3e9 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,8 +1,9 @@ import {expectType, expectError} from 'tsd'; import tempy = require('.'); -const options: tempy.Options = {}; +const options: tempy.FileOptions = {}; expectType(tempy.directory()); +expectType(tempy.directory({prefix: 'name_'})); expectType(tempy.file()); expectType(tempy.file({extension: 'png'})); expectType(tempy.file({name: 'afile.txt'})); diff --git a/readme.md b/readme.md index ce3df83..244d70f 100644 --- a/readme.md +++ b/readme.md @@ -24,6 +24,9 @@ tempy.file({name: 'unicorn.png'}); tempy.directory(); //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' + +tempy.directory({prefix: 'name'}); +//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41' ``` ## API @@ -50,10 +53,24 @@ Type: `string` Filename. Mutually exclusive with the `extension` option. -### tempy.directory() +### tempy.directory([options]) Get a temporary directory path. The directory is created for you. +#### options + +Type: `Object` + +##### prefix + +_You usually won't need this option. Specify it only when actually needed._ + +Type: `string` + +Directory prefix. + +Useful for testing by making it easier to identify cache directories that are created. + ### tempy.write(fileContent, options?) Write data to a random temp file. diff --git a/test.js b/test.js index e472883..6ad2715 100644 --- a/test.js +++ b/test.js @@ -33,7 +33,10 @@ test('.file()', t => { }); test('.directory()', t => { + const prefix = 'name_'; + t.true(tempy.directory().includes(tempDir)); + t.true(path.basename(tempy.directory({prefix})).startsWith(prefix)); }); test('.write(string)', async t => {