Skip to content

Commit ed6b524

Browse files
committed
Fix documentation and add more type checks
1 parent 5e7773e commit ed6b524

File tree

1 file changed

+43
-93
lines changed

1 file changed

+43
-93
lines changed

lib/tmp.ts

Lines changed: 43 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ interface FileOptions extends DirOptions {
2626
detachDescriptor?: boolean;
2727
}
2828

29+
interface DirSyncObject {
30+
name: string;
31+
removeCallback: CleanupCallback;
32+
}
33+
34+
interface FileSyncObject extends DirSyncObject {
35+
fd: number;
36+
37+
}
38+
2939
type CleanupCallback = (next?: Function) => void;
3040

3141
type FileCallback = (err?: Error, name?: string, fd?: number, cb?: CleanupCallback) => void;
@@ -79,8 +89,8 @@ var
7989
* Random name generator based on crypto.
8090
* Adapted from http://blog.tompawlak.org/how-to-generate-random-values-nodejs-javascript
8191
*
82-
* @param {number} howMany
83-
* @returns {string} the generated random name
92+
* @param howMany how many random characters should be generated
93+
* @returns the generated random name
8494
* @private
8595
*/
8696
function _randomChars(howMany: number) {
@@ -105,8 +115,8 @@ function _randomChars(howMany: number) {
105115
/**
106116
* Generates a new temporary name.
107117
*
108-
* @param {Object} opts
109-
* @returns {string} the new random name according to opts
118+
* @param opts the name options
119+
* @returns the new random name according to opts
110120
* @private
111121
*/
112122
function _generateTmpName(opts: Options) {
@@ -133,8 +143,8 @@ function _generateTmpName(opts: Options) {
133143
/**
134144
* Gets a temporary file name.
135145
*
136-
* @param {(Options|tmpNameCallback)} options options or callback
137-
* @param {?tmpNameCallback} callback the callback function
146+
* @param callbackOrOptions options or the callback
147+
* @param callback the callback function if options is provided
138148
*/
139149
function tmpName(callbackOrOptions: Options | TmpNameCallback, callback?: TmpNameCallback) {
140150
const emptyOptions = {} as Options;
@@ -178,9 +188,9 @@ function tmpName(callbackOrOptions: Options | TmpNameCallback, callback?: TmpNam
178188
/**
179189
* Synchronous version of tmpName.
180190
*
181-
* @param {Object} options
182-
* @returns {string} the generated random name
183-
* @throws {Error} if the options are invalid or could not generate a filename
191+
* @param options the options
192+
* @returns the generated random name
193+
* @throws if the options are invalid or could not generate a filename
184194
*/
185195
function tmpNameSync(options?: Options) {
186196
const opts: Options = typeof options == 'undefined' ? {} : options;
@@ -208,8 +218,8 @@ function tmpNameSync(options?: Options) {
208218
/**
209219
* Creates and opens a temporary file.
210220
*
211-
* @param {(Options|fileCallback)} options the config options or the callback function
212-
* @param {?fileCallback} callback
221+
* @param callbackOrOptions options or the callback
222+
* @param callback the callback function if options is provided
213223
*/
214224
function file(callbackOrOptions: FileOptions | FileCallback, callback?: FileCallback) {
215225
const emptyOptions = {} as FileOptions;
@@ -268,11 +278,11 @@ function file(callbackOrOptions: FileOptions | FileCallback, callback?: FileCall
268278
/**
269279
* Synchronous version of file.
270280
*
271-
* @param {Options} options
272-
* @returns {FileSyncObject} object consists of name, fd and removeCallback
273-
* @throws {Error} if cannot create a file
281+
* @param options the options
282+
* @returns object consists of name, fd and removeCallback
283+
* @throws if cannot create a file
274284
*/
275-
function fileSync(options: FileOptions) {
285+
function fileSync(options: FileOptions): FileSyncObject {
276286
const opts: FileOptions = typeof options == 'undefined' ? {} : options;
277287

278288
opts.postfix = opts.postfix || '.tmp';
@@ -290,7 +300,7 @@ function fileSync(options: FileOptions) {
290300
/**
291301
* Removes files and folders in a directory recursively.
292302
*
293-
* @param {string} root
303+
* @param root the starting directory
294304
* @private
295305
*/
296306
function _rmdirRecursiveSync(root: string) {
@@ -330,8 +340,8 @@ function _rmdirRecursiveSync(root: string) {
330340
/**
331341
* Creates a temporary directory.
332342
*
333-
* @param {(Options|dirCallback)} options the options or the callback function
334-
* @param {?dirCallback} callback
343+
* @param callbackOrOptions options or the callback
344+
* @param callback the callback function if options is provided
335345
*/
336346
function dir(callbackOrOptions: DirOptions | DirCallback, callback?: DirCallback) {
337347
const emptyOptions = {} as DirOptions;
@@ -364,11 +374,11 @@ function dir(callbackOrOptions: DirOptions | DirCallback, callback?: DirCallback
364374
/**
365375
* Synchronous version of dir.
366376
*
367-
* @param {Options} options
368-
* @returns {DirSyncObject} object consists of name and removeCallback
377+
* @param options
378+
* @returns object consists of name and removeCallback
369379
* @throws {Error} if it cannot create a directory
370380
*/
371-
function dirSync(options: DirOptions) {
381+
function dirSync(options: DirOptions): DirSyncObject {
372382
const opts: DirOptions = (typeof options == 'undefined') ? {} : options;
373383

374384
const name = tmpNameSync(opts);
@@ -383,10 +393,10 @@ function dirSync(options: DirOptions) {
383393
/**
384394
* Prepares the callback for removal of the temporary file.
385395
*
386-
* @param {string} name the path of the file
387-
* @param {number} fd file descriptor
388-
* @param {Object} opts
389-
* @returns {fileCallback}
396+
* @param name the path of the file
397+
* @param fd file descriptor
398+
* @param opts the passed options
399+
* @returns the callback function
390400
* @private
391401
*/
392402
function _prepareTmpFileRemoveCallback(name: string, fd: number, opts: FileOptions): CleanupCallback {
@@ -426,14 +436,14 @@ function _prepareTmpFileRemoveCallback(name: string, fd: number, opts: FileOptio
426436
/**
427437
* Prepares the callback for removal of the temporary directory.
428438
*
429-
* @param {string} name
430-
* @param {Object} opts
431-
* @returns {Function} the callback
439+
* @param path the path of the dir
440+
* @param opts the provided options
441+
* @returns the callback function
432442
* @private
433443
*/
434-
function _prepareTmpDirRemoveCallback(name: string, opts: DirOptions) {
444+
function _prepareTmpDirRemoveCallback(path: string, opts: DirOptions): CleanupCallback {
435445
const removeFunction = opts.unsafeCleanup ? _rmdirRecursiveSync : fs.rmdirSync.bind(fs);
436-
const removeCallback = _prepareRemoveCallback(removeFunction, name);
446+
const removeCallback = _prepareRemoveCallback(removeFunction, path);
437447

438448
if (!opts.keep) {
439449
_removeObjects.unshift(removeCallback);
@@ -445,9 +455,9 @@ function _prepareTmpDirRemoveCallback(name: string, opts: DirOptions) {
445455
/**
446456
* Creates a guarded function wrapping the removeFunction call.
447457
*
448-
* @param {Function} removeFunction
449-
* @param {Object} arg
450-
* @returns {Function}
458+
* @param removeFunction the function to guard
459+
* @param arg the arguments to pass
460+
* @returns the guarded function
451461
* @private
452462
*/
453463
function _prepareRemoveCallback<T>(removeFunction: (_: T) => void, arg: T): CleanupCallback {
@@ -530,66 +540,6 @@ process.addListener('exit', function _exit(code) {
530540
_garbageCollector();
531541
});
532542

533-
/**
534-
* Configuration options.
535-
*
536-
* @typedef {Object} Options
537-
* @property {?number} tries the number of tries before give up the name generation
538-
* @property {?string} template the "mkstemp" like filename template
539-
* @property {?string} name fix name
540-
* @property {?string} dir the tmp directory to use
541-
* @property {?string} prefix prefix for the generated name
542-
* @property {?string} postfix postfix for the generated name
543-
*/
544-
545-
/**
546-
* @typedef {Object} FileSyncObject
547-
* @property {string} name the name of the file
548-
* @property {string} fd the file descriptor
549-
* @property {fileCallback} removeCallback the callback function to remove the file
550-
*/
551-
552-
/**
553-
* @typedef {Object} DirSyncObject
554-
* @property {string} name the name of the directory
555-
* @property {fileCallback} removeCallback the callback function to remove the directory
556-
*/
557-
558-
/**
559-
* @callback tmpNameCallback
560-
* @param {?Error} err the error object if anything goes wrong
561-
* @param {string} name the temporary file name
562-
*/
563-
564-
/**
565-
* @callback fileCallback
566-
* @param {?Error} err the error object if anything goes wrong
567-
* @param {string} name the temporary file name
568-
* @param {number} fd the file descriptor
569-
* @param {cleanupCallback} fn the cleanup callback function
570-
*/
571-
572-
/**
573-
* @callback dirCallback
574-
* @param {?Error} err the error object if anything goes wrong
575-
* @param {string} name the temporary file name
576-
* @param {cleanupCallback} fn the cleanup callback function
577-
*/
578-
579-
/**
580-
* Removes the temporary created file or directory.
581-
*
582-
* @callback cleanupCallback
583-
* @param {simpleCallback} [next] function to call after entry was removed
584-
*/
585-
586-
/**
587-
* Callback function for function composition.
588-
* @see {@link https://github.com/raszi/node-tmp/issues/57|raszi/node-tmp#57}
589-
*
590-
* @callback simpleCallback
591-
*/
592-
593543
// exporting all the needed methods
594544
module.exports.tmpdir = TMP_DIR;
595545

0 commit comments

Comments
 (0)