file-state-monitor
Version:
Completely customizable file monitoring using states
72 lines (56 loc) • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _fs = _interopRequireDefault(require("fs"));
var _BaseFileState = _interopRequireDefault(require("./BaseFileState"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* A file state using size of the file to detect changes
*/
class SizeState extends _BaseFileState.default {
/**
* Constructs a new file size aware state
*
* @param {Object} fileInfo Object containing existing state data
*/
constructor(fileInfo) {
super(fileInfo);
if (Object.keys(fileInfo).length === 1) {
let stat = _fs.default.statSync(this.path);
this._size = stat.size;
}
}
/**
* Gets the size of the file
*
* @return {number} Size of the file in bytes
*/
get size() {
return this._size;
}
/**
* Checks if this file state is different than another by comparing their
* file size
*
* @param {BaseFileState} otherState Other file state to check against
* @return {boolean} True if the two file states are different, false if not
*/
isDifferentThan(otherState) {
if (this.path !== otherState.path) {
throw new Error(`Can only compare files with the same path, but tried to compare ${this._path} with ${otherState.path}`);
}
return this.size !== otherState.size;
}
/**
* @inheritdoc
*/
toJson() {
return {
path: this.path,
size: this.size
};
}
}
exports.default = SizeState;