1.0.1 • Published 8 years ago

file-checker v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
8 years ago

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 exists
  • isFile - whether the path is file
  • isDirectory - whether the path is directory
  • size - filesize in bytes
  • accessed - time when file data last accessed (numeric value)
  • modified - time when file data last modified
  • changed - time when file status was last changed
  • created - 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);
});