astexplorer.app
Version:
https://astexplorer.net with ES Modules support and Hot Reloading
48 lines (47 loc) • 1.48 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.createFileWatcher = void 0;
var chokidar_1 = __importDefault(require("chokidar"));
var createFileWatcher = function (onChange) {
var filePath = '';
var watcher = null;
var hasFilePath = function () { return filePath !== ''; };
var isNewPath = function (nextFilePath) { return nextFilePath !== filePath; };
var isRunning = function () { return watcher !== null; };
var start = function () {
if (!isRunning() && hasFilePath()) {
watcher = chokidar_1["default"]
.watch(filePath, { persistent: true })
.on('change', function () { return onChange(filePath); });
}
return Promise.resolve(filePath);
};
var stop = function () {
if (isRunning()) {
watcher.close();
watcher = null;
}
};
var move = function () {
stop();
start();
};
var setFilePath = function (nextFilePath) {
if (isNewPath(nextFilePath)) {
filePath = nextFilePath;
if (isRunning()) {
move();
}
}
return Promise.resolve(filePath);
};
return {
setFilePath: setFilePath,
start: start,
stop: stop
};
};
exports.createFileWatcher = createFileWatcher;