diff --git a/temp-fs/temp-fs-tests.ts b/temp-fs/temp-fs-tests.ts new file mode 100644 index 000000000..9d01e31f3 --- /dev/null +++ b/temp-fs/temp-fs-tests.ts @@ -0,0 +1,31 @@ +// Copied from https://github.com/jakwings/node-temp-fs/blob/60a4d2586a81a7057bd4a395ec8c00b4100f84fe/README.md +// and slightly modified. + +/// + +// Create a tempfile in the system-provided tempdir. +tempfs.open(function (err:any, file:tempfs.file) { + if (err) { throw err; } + + console.log(file.path, file.fd); + // async + file.unlink(function () { + console.log('File delected'); + }); + // sync + // No problem even if unlink() is called twice. + file.unlink(); +}); + +// Create a tempdir in current directory. +tempfs.mkdir({ + dir: '.', + recursive: true, // It and its content will be remove recursively. + track: true // Track this directory. +}, function (err:any, dir:tempfs.dir) { + if (err) { throw err; } + + console.log(dir.path, dir.recursive); + throw new Error('Since it is tracked, tempfs will remove it for you.'); + dir.unlink(); +}); diff --git a/temp-fs/temp-fs.d.ts b/temp-fs/temp-fs.d.ts new file mode 100644 index 000000000..a833c773f --- /dev/null +++ b/temp-fs/temp-fs.d.ts @@ -0,0 +1,211 @@ +// Type definitions for temp-fs v0.9.8 +// Project: https://github.com/jakwings/node-temp-fs +// Definitions by: MEDIA CHECK s.r.o. +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/** + * A temporary file and directory creator. + */ +declare module tempfs { + + /** + * A tempdir. + */ + interface dir { + /** + * The absolute path to the tempdir. + */ + path: String; + + /** + * Whether {@link dir#unlink} will remove the tempdir recursively. + */ + recursive: Boolean; + + /** + * A special function for you to remove the directory. + * + * If the directory is not tracked, it may throw when an error occurs or + * the first argument of the callback function will be an Error object. + * + * @param callback makes it asynchronous. + */ + unlink(callback?:(error:Error)=>any): any; + } + + /** + * A tempfile. + */ + interface file { + /** + * The absolute path to the tempfile. + */ + path: String; + + /** + * An integer file descriptor. + */ + fd: Number; + + /** + * A special function for you to delete the file. + * + * If the file is not tracked, it may throw when an error occurs or the + * first argument of the callback function will be an Error object. + * + * @param callback makes it asynchronous. + */ + unlink(callback?:(error:Error)=>any): any; + } + + /** + * Options. + */ + interface options { + /** + * Where to put the generated tempfile or tempdir. + * + * Also see {@link options#name}. Default: tempfs.dir() + */ + dir?: String; + + /** + * The maximum number of chance to retry before throwing an error. + * + * It should be a finite number. Default: 5 + */ + limit?: Number; + + /** + * File mode (default: 0600) or directory mode (default: 0700) to use. + */ + mode?: Number; + + /** + * If set, join the two paths {@link options#dir} || + * tempfs.dir() and {@link options#name} together and use the + * result as the customized filename/pathname. + */ + name?: String; + + /** + * The prefix for the generated random name. + * + * Default: "tmp-" + */ + prefix?: String; + + /** + * Whether {@link dir#unlink} should remove a directory recursively. + * + * Default: false + */ + recursive?: Boolean; + + /** + * The suffix for the generated random name. + * + * Default: "" + */ + suffix?: String; + + /** + * A string containing some capital letters Xs for substitution with + * random characters. + * + * Then it is used as part of the filename/dirname. Just like what you + * do with the mktemp(3) function in the C library. + */ + template?: String; + + /** + * If set to true, let temp-fs manage the the current file/directory for + * you even if the global tracking is off. If set to false, don't let + * temp-fs manage it even if the global tracking is on. Otherwise, use + * the current global setting. + */ + track?: Boolean; + } + + /** + * Remove all tracked files and directories asynchronously. + */ + function clear(callback?:()=>any):any; + + /** + * Remove all tracked files and directories synchronously. + */ + function clearSync():any; + + /** + * Return the path of a system-provided tempdir as + * require('os').tmpdir() does. + * + * You should not make any assumption about whether the path contains a + * trailing path separator, or it is a real path. On most system it is not a + * fixed path, and it can be changed by the user environment. When in doubt, + * check it first. + */ + function dir():string; + + /** + * Try to create a new tempdir asynchronously. + * + * @param callback function receives two arguments error and + * dir. If error is + * null, dir has the properties of + * {@link dir}. + */ + function mkdir(options?:options, callback?:(err:any, dir:dir)=>any):any; + + /** + * The synchronous version of {@link mkdir}. + * + * @throws when an error happens. + */ + function mkdirSync(options?:options):dir; + + /** + * Return a customized/random filename/dirname. + */ + function name(options?:options):string; + + /** + * Try to open a unique tempfile asynchronously. + * + * @param callback function receives two arguments error and + * file. If error is + * null, file has the properties + * of {@link file}. + */ + function open(options?:options, callback?:(err:any, file:file)=>any):any; + + /** + * The synchronous version of {@link open}. + * + * @throws when an error happens. + */ + function openSync(options?:options):file; + + /** + * Use it to switch global files/directories tracking on or off. + * + * Turn it on if you don't want to manually delete everything. When it is + * turned off, all recorded files and directories will not be removed but + * still kept in case it is turned on again before the program exits. + * + * This switch does not affect manually tracked files through + * {@link options#track}. They will be removed automatically on exit. + * + * Note: When an uncaught exception occurs, all tracked temporary files + * and directories will be removed no matter it is on or off. + */ + function track(on?:Boolean):void; +} + +/** + * A temporary file and directory creator. + */ +declare module "temp-fs" { + export = tempfs; +}