sensai
Version:
Because even AI needs a master
58 lines (57 loc) • 2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, /**
* Initialize file-system watcher.
*
* @notes
* - reload modules on `update`
* - remove module from cache and router on `remove`
* - add module to cache and router on `add`
* - TODO node modules are not tracked (npm install will not reload cache)
* - TODO a failing module (bad dependency or anything else) should be removed from cache
* - TODO ignored folders (i.e node modules and .simpi) that get renamed are not added to cache/router
*/ "default", {
enumerable: true,
get: function() {
return _default;
}
});
const _chokidar = /*#__PURE__*/ _interop_require_default(require("chokidar"));
const _path = require("path");
const _path1 = require("../../utils/path");
const _invalidate = /*#__PURE__*/ _interop_require_default(require("../../utils/invalidate"));
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
const _default = async (router, { apiDir })=>{
const resetCache = (0, _invalidate.default)();
// track dependencies only when needed
const watcher = _chokidar.default.watch(apiDir, {
ignored: /(^|[\/\\])(node_modules|\.sensai)/,
ignoreInitial: true
});
watcher.on("change", (filePath)=>{
resetCache((0, _path.join)(process.cwd(), filePath));
});
watcher.on("add", (filePath)=>{
if ((0, _path1.isRelative)(apiDir, filePath)) {
router.add((0, _path.join)("/", filePath));
}
});
watcher.on("unlink", (filePath)=>{
resetCache((0, _path.join)(process.cwd(), filePath));
if ((0, _path1.isRelative)(apiDir, filePath)) {
router.remove((0, _path.join)("/", filePath));
}
});
// watcher.on('unlinkDir', (folderPath: string) => {
// console.log('unlinkDir')
// if (isRelative(dir, folderPath)) {
// router.prune(folderPath)
// }
// })
};