https://github.com/boneskull/can-write
Test if a file or directory is writable. Because sometimes you just gotta know.
https://github.com/boneskull/can-write
Last synced: about 1 year ago
JSON representation
Test if a file or directory is writable. Because sometimes you just gotta know.
- Host: GitHub
- URL: https://github.com/boneskull/can-write
- Owner: boneskull
- License: mit
- Archived: true
- Created: 2015-09-22T18:41:49.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-11-23T20:44:42.000Z (over 9 years ago)
- Last Synced: 2025-03-03T02:04:56.437Z (over 1 year ago)
- Language: JavaScript
- Homepage: http://boneskull.com/can-write/
- Size: 120 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# can-write [](https://travis-ci.org/boneskull/can-write)
> Test if a file or directory is writable. Because sometimes you just gotta know.
## Usage
```js
var canWrite = require('can-write');
return canWrite('/path/to/my/file')
.then(function(result) {
if (result) {
// writable
} else {
// not writable
}
}, function(err) {
// some sort of error
});
// alternatively:
canWrite('/path/to/my/file', function(err, result) {
if (err) {
// handle error
} else {
if (result) {
// writable
} else {
// not writable
}
}
});
```
### Readability
Writability does not always imply readability. Think a "Drop Box", wherein guest users can put files in, but cannot read the contents.
To check readability, pass a truthy second parameter:
```js
var canWrite = require('can-write');
return canWrite('/path/to/my/file', true)
.then(function(result) {
if (result) {
// readable & writable
} else {
// either not readable or not writable.
// if you need to know, call canWrite() again without the flag.
}
}, function(err) {
// some sort of error
});
```
## Fair Warning
It's an *anti-pattern* to check writability.
You generally want to just **do what you're going to do**, then recover from an error.
For example, say you need to create a file:
```js
fs.writeFile('/some/filepath', 'some data', function(err) {
if (err.code === 'EACCESS') {
// the file is not writable.
}
});
```
In other words: don't use this module unless you really, *really*, ***really*** need to check whether something is writable.
## Install
```shell
$ npm install can-write
```
## License
© 2015 [Christopher Hiller](https://boneskull.com). Licensed MIT.