file-checker
v1.0.1
Published
Simple module to check for file status
Readme
File checker module
Simple module to check for file status. This is an initial version, more features to come!
Installation
In your project folder run:
npm install --save file-checker
Note: The module code uses few ES6 features, so at least 4.4.5 version of Node is required.
Usage
First, you need to include the file checker.
const checker = require('file-checker').create();
Second, specify the files (or directories) that need to be checked.
checker.addRule('/path/to/file');By default, the checker only checks whether the given file exists.
However, you can pass additional options to fine-tune the assertion rules:
checker.addRule('/path/to/file', { exists: true }); // equivalent to above
Note: It is strongly recommended to use absolute file paths, but the checker also works with relative ones.
List of supported options (all options are optional):
exists- whether the file/directory existsisFile- whether the path is fileisDirectory- whether the path is directorysize- filesize in bytesaccessed- time when file data last accessed (numeric value)modified- time when file data last modifiedchanged- time when file status was last changedcreated- time of file creation
For example, let's check if a file exists and fits specific size:
checker.addRule('/path/to/file', {
exists: true,
isFile: true,
size: (size) => {
// custom assertion logic
return size < 1024*1024; // 1 MB
}
});Third, process the rules and review results.
checker.run() // returns promise
.then((results) => {
console.log('results', results);
})
.catch((error) => {
console.log('unexpected error occured', error);
});