Provides native promises for all file system methods involving file descriptors (FD), basically manages fs.open
for you.
file descriptor (FD, less frequently fildes)
en.wikipedia.org/wiki/File_descriptor
npm i --save fildes
fildes
always returns a new native Promise!
var fildes = require('fildes');
fildes.write('./path/to/file.txt', 'The quick green fix')
.then(function(){
console.log('done!');
})
.catch(function(error){
// error
});
- I needed an API that returns Promises
- provides nice defaults i.e. suitable flags for
open
,read
andwrite
, see fildes/issues/1 - creates a directories if flag is
w
,w+
,a
ora+
- uses no magic
- promises useful methods, for
copy
,mkdir
,rmdir
, etc. - some very popular node modules use
fs.exists()
which is deprecated…
fs.exists()
should not be used to check if a file exists before callingfs.open()
. Doing so introduces a race condition since other processes may change the file's state between the two calls. Instead, user code should callfs.open()
directly and handle the error raised if the file is non-existent.
fs.exists Stability: 0 - Deprecated (Node.js v5.0.0 File System API)
Promise.all(['file.txt', 'file2.txt'].map(function(path){
return fildes.read(path, {
'length': 262,
'position': 0
});
}))
.then(function(result){
// chunk of file 1
console.log(result[0]);
// chunk of file 2
console.log(result[1]);
});
var files = ['a.txt', 'b.json', 'c.txt'];
Promise.all(files.map(function(file){
return fildes.fstat(file)
.then(function(stat){
return stat.size;
});
}))
.then(function(sizes){
console.log('got filesizes', sizes);
});
var files = ['buffer.txt', 'nothere.txt', 'dir'];
Promise.all(files.map(function(file){
return fildes.fstat(file)
.then(function(stat){
return stat.isFile();
})
.catch(function(){
return false;
});
}))
.then(function(result){
console.log(result);
})
fildes.open(path)
.then(function(fd){
// write first time
return fildes.write(fd, 'Hi there!')
.then(function(){
var word = new Buffer('again');
// write second time on the same fd
return fildes.write(fd, word, {
'offset': 0,
'length': 5,
'position': 3
});
})
.then(function(){
return fildes.stats(fd);
})
.then(function(stats){
console.log(stats);
// manually close fd
return fildes.close(fd);
});
})
.catch(function(error){
console.error(err.stack);
});
- open
- close
- write
- writeFile
- read
- readFile
- appendFile
- stats
- truncate
- utime
- chmod
- unlink
- readdir
- mkdir
- rm
- cp
Opens a file descriptor (FD). If flags
is 'w', 'w+', 'a' or 'a+' open will
check for 'ENOENT: no such file or directory' error and try to mkdir.
fildes.open
is used internally for write, read and fstat.
Manually opening and closing is optional as all methods take a path
and internally open and close.
path
Stringoptions
Objectflag
orflags
String defaults to 'w+'mode
String defaults to '0666'
fildes.open('./no/file/here.txt', {
'flag': 'r'
})
.then(function(fd){
// file descriptor (FD)
})
.catch(function(error){
// returns { [Error: ENOENT: no such file or directory..
});
See also fs.open (Node.js File System API)
Closes a file descriptor (FD). Methods generally take care about closing if a path was given. If a file descriptor (FD) was passed fildes will not close by itself.
fildes.open('./file.txt')
.then(function(fd){
// do something
// manually close fd
return fildes.close(fd);
})
.then(function(){
console.log('done!');
});
Promise to open a file descriptor, write data to it and close it. Uses open internally which checks for 'ENOENT' error then tries to mkdir.
If data is type of Object
it will be converted to JSON.
path
String | file descriptor (FD)data
String | Object | Bufferoptions
Objectflag
orflags
String defaults to 'w' unless position > 0 in that case it is 'r+', see also openmode
String, see open- If data is of type String or Object,
fs.write (Node.js File System API)
position
encoding
(optional)
- If data is a Buffer,
fs.write (Node.js File System API)
offset
length
position
(optional)
fildes.write('./new/dir/file.txt', 'some data\n')
.then(function(){
console.log('dir created and file written!');
});
fildes.write('./path/to/file.json', {
'some': 'data'
});
var buffer = new Buffer('Hello World!');
fildes.write('./path/to/file.txt', buffer, {
'offset': 0,
'length': buffer.length
});
Promise uses fs.writeFile
.
path
String | file descriptor (FD, introduced in Node.js 5.x)data
String | Object | Bufferoptions
Object (optional)encoding
,mode
,flag
fildes.writeFile('./path/to/file.json', { 'data': 1 });
See also fs.writeFile (Node.js File System API)
Promise to read a file to a buffer.
path
String | file descriptor (FD)buffer
Buffer (optional)options
Objectflag
|flags
String defaults to 'r', see also openoffset
Number defaults to 0 (optional)length
Numberposition
Number (optional)encoding
String (optional)
fildes.read('./path/to/file.txt', {
'length': 8,
'encoding': 'utf8'
})
.then(function(content){
console.log(content);
})
var buffer = new Buffer(8);
fildes.read('./path/to/file.txt', buffer, {
'offset': 0,
'length': 8,
'position': 0
})
.then(function(){
console.log(buffer.toString());
})
Promise uses fs.readFile
.
path
String | file descriptor (FD, introduced in Node.js 5.x)options
Object (optional)encoding
,flag
fildes.readFile('./path/to/file.json')
.then(function(buffer){
console.log('got', buffer.toString());
});
See also fs.readFile (Node.js File System API)
Promise uses fs.appendFile
.
path
String | file descriptor (FD, introduced in Node.js 5.x)data
Buffer | Stringoptions
Object (optional)encoding
,flag
,mode
fildes.appendFile('./path/to/file.txt', '2015-11-07 GET /robots.txt ')
.then(function(){
console.log('added some data');
});
See also fs.appendFile (Node.js File System API)
Promise file stats. alias for fildes.fstat
.
path
String | file descriptor (FD)options
Objectflag
|flags
String defaults to 'r', see also open
fildes.stats('./path/to/file.txt')
.then(function(stats){
console.log(stats);
})
See also fs.fstat (Node.js File System API)
Promise truncate, alias for fildes.ftruncate
.
path
String | file descriptor (FD)options
Objectflag
|flags
String defaults to 'r+', see also openlength
|len
Number, defaults to 0
fildes.truncate('./path/to/file.txt', {
'length': 8
})
See also fs.ftruncate (Node.js File System API)
Promise utime, alias for fildes.futime
.
path
String | file descriptor (FD)options
Objectflag
|flags
String defaults to 'r+', see also openaccess
|atime
UNIX timestamp or Date, defaults to new Datemodification
|mtime
UNIX timestamp or Date, defaults to new Date
fildes.utimes('./path/to/file.txt', {
'access': Date.now() - (60 * 60 * 1000),
'modification': new Date('2015-10-26')
})
See also fs.futime (Node.js File System API)
Promise chmod, alias for fildes.fchmod
.
path
String | file descriptor (FD)options
Objectflag
|flags
String defaults to 'r+', see also openmode
String | Integer
fildes.chmod('./path/to/file.txt', {
'mode': 0700 // nobody else
})
Promise uses fs.unlink (Node.js File System API).
fildes.unlink('./path/to/file.txt')
.then(function(){
console.log('file removed!');
})
.catch(function(error){
// unlink thorws an error if file not found
});
Promise uses fs.rename (Node.js File System API).
fildes.rename('./path/to/old.txt', './path/moved/to/new.txt')
.then(function(){
console.log('file moved!');
})
.catch(function(error){
// unlink thorws an error if file not found
});
Promise uses fs.readdir (Node.js File System API).
path
String
fildes.readdir('./path/to/dir')
.then(function(files){
console.log(files);
})
.catch(function(error){
// readdir thorws an error no such file or directory
});
Promise uses mkdirp (NPM Documentation).
path
Stringoptions
Object (optional)mode
fildes.mkdir('./path/to/dir')
.then(function(){
console.log('directory created!');
});
Promise fildes.rm
alias fildes.rmdir
uses rimraf (NPM Documentation).
fildes.rm('./path/to/dir')
.then(function(){
console.log('directory removed!');
});
Promise fildes.cp
alias fildes.copy
uses cpy (NPM Documentation).
fildes.cp(['./data/*.txt'], './destination')
.then(function(){
console.log('directory copied!');
});
npm install
npm test
# debug
DEBUG=fildes npm test
# debug all
DEBUG=fildes* npm test
- Promises for all async fs methods that use a file descriptor (FD): fs.fchown, fs.fsync
- Test graceful-fs for ulimit, but include multiple child process (isaacs/node-graceful-fs#48)
- https://github.com/sindresorhus/trash ?
- fs.link, fs.symlink