a2r
Version:
A2R Framework
34 lines (33 loc) • 1.29 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const chokidar_1 = __importDefault(require("chokidar"));
const fs_1 = require("@a2r/fs");
/**
* Watch project API folder recursively for files changes
*
* @param {WatcherOptions} watcherOptions watcher options
*/
const watchFolder = async (watcherOptions) => new Promise((resolve, reject) => {
const { targetPath, onReady, onError, options } = watcherOptions;
const normalizedTargetPath = path_1.default.normalize(targetPath);
(0, fs_1.exists)(normalizedTargetPath).then((pathExists) => {
if (pathExists) {
const watcher = chokidar_1.default.watch(normalizedTargetPath, options);
watcher.on('error', onError);
watcher.on('ready', () => {
if (onReady) {
onReady(watcher, normalizedTargetPath);
}
resolve(watcher);
});
}
else {
reject(new Error(`Provided target path doesn't exist: ${normalizedTargetPath}`));
}
});
});
exports.default = watchFolder;