@sanity/pkg-utils
Version:
Simple utilities for modern npm packages.
50 lines (49 loc) • 1.99 kB
JavaScript
import path from "node:path";
import { scan, startWith, distinctUntilChanged } from "rxjs/operators";
import { globby } from "globby";
import * as chokidar from "chokidar";
import { Observable } from "rxjs";
function globFiles(patterns) {
return globby(patterns.map((pattern) => pattern.split(path.sep).join(path.posix.sep)));
}
function watchFiles(patterns) {
return new Observable((observer) => {
const watcher = chokidar.watch(patterns, {
ignoreInitial: !0
});
function handleFileEvent(type, file) {
type === "error" || file instanceof Error ? observer.error(file) : observer.next({ type, file });
}
return watcher.on("all", handleFileEvent), () => {
watcher.off("all", handleFileEvent), watcher.close();
};
});
}
async function watchConfigFiles(options) {
const { cwd, logger } = options, initialFiles = await globFiles([
path.resolve(cwd, "package.json"),
path.resolve(cwd, "package.config.cjs"),
path.resolve(cwd, "package.config.js"),
path.resolve(cwd, "package.config.ts"),
path.resolve(cwd, "package.config.mjs"),
path.resolve(cwd, "package.config.mts")
]);
return watchFiles([
path.resolve(cwd, "package.json"),
path.resolve(cwd, "package.config.cjs"),
path.resolve(cwd, "package.config.js"),
path.resolve(cwd, "package.config.ts"),
path.resolve(cwd, "package.config.mjs"),
path.resolve(cwd, "package.config.mts")
]).pipe(
scan((files, fileEvent) => fileEvent.type === "add" ? files.concat(fileEvent.file) : fileEvent.type === "unlink" ? files.filter((f) => f !== fileEvent.file) : fileEvent.type === "change" ? (logger.log(
"--------------------------------------------------------------------------------"
), logger.info(path.relative(cwd, fileEvent.file), "changed"), logger.log(""), files.slice(0)) : files, initialFiles),
startWith(initialFiles),
distinctUntilChanged()
);
}
export {
watchConfigFiles
};
//# sourceMappingURL=watchConfigFiles.js.map