temporarily
Create temporary directories and files.
Install
npm install --save temporarily
Why
There are a few other temporary file creation utilities. Here's why I made my own.
- cleanup by default (on process exit), no opt-out (though you may clean up early if necessary)
- sync by default, meant for testing
- easy nested dir / file scaffolding with content
Usage
const temp = ;// orconst dir file filepath = ;// or;
API
filepath
Generate a filepath. No file is created.
filepath( [options:object] )
- options.dir
:string
– defaultos.tmpdir()
- options.ext
:string
- options.name
:string
– defaulttemporarily-{wwwwdddd}
;// '/var/folders/30/T/temporarily-tkEK6023' ;// '/var/folders/30/T/temporarily-tkEK6023.json' ;// '/home/myuser/temporarily-tkEK6023' ;// '/var/folders/30/T/file-tk60'
file
Create a temporary file.
file( [options:object] )
- options.data
:string|Buffer
– default''
- options.encoding
:string
– default'utf8'
- options.mode
:string
– default0o666
All options from filepath can be applied as well.
;// { data: '',// filepath: '/var/folders/30/T/temporarily-RdgC6481',// mode: 438,// cleanup: [Function: cleanup] } ;// { data: '',// filepath: '/var/folders/30/T/temporarily-RdgC6481',// mode: 511,// cleanup: [Function: cleanup] } ; // write file contents// { data: 'Hello World!',// filepath: '/var/folders/30/T/temporarily-RdgC6481',// mode: 438,// cleanup: [Function: cleanup] }
dir
Create a temporary directory.
dir( [options:object], [children:Array<object>] )
- options.mode
:string
– default0o777
All options from filepath can be applied as well.
;// { filepath: '/var/folders/30/T/temporarily-tkEK6023',// mode: 511,// cleanup: [Function: cleanup] } ;// { filepath: '/home/myuser/temporarily-tkEK6023',// mode: 511,// cleanup: [Function: cleanup] } ;// { filepath: '/var/folders/30/T/temporarily-tkEK6023',// mode: 438,// cleanup: [Function: cleanup] } // { filepath: '/var/folders/30/T/tempo',// mode: 511,// cleanup: [Function: cleanup],// children:// [ { filepath: '/var/folders/30/T/tempo/temporarily-MwpX5662',// mode: 511,// cleanup: [Function: cleanup],// children:// [ { data: '',// filepath: '/var/folders/30/T/tempo/temporarily-MwpX5662/nestedFile',// mode: 438,// cleanup: [Function: cleanup] } ] },// { data: 'Hello World!',// filepath: '/var/folders/30/T/tempo/temporarily-yxYz6104',// mode: 438,// cleanup: [Function: cleanup] } ] }