Skip to content

Commit

Permalink
Update multiple files at once in watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cowwoc committed Dec 5, 2023
1 parent 239144e commit 05ef22c
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions build/Project.mts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import eslintConfig from "../.eslintrc.mjs";
import {Logger} from "winston";
import {mode} from "./mode.mjs";
import parseArgs from "minimist";
import _ from "lodash";


class Project
Expand Down Expand Up @@ -293,7 +294,7 @@ class Project
console.timeEnd("bundleResources");
}

public async test()
private async test()
{
console.time("test");
const binPath = path.posix.resolve("./node_modules/.bin");
Expand Down Expand Up @@ -359,12 +360,29 @@ class Project
private async watchFiles(paths: string | ReadonlyArray<string>,
callback: (sources: string[]) => Promise<void>): Promise<void>
{
const changes: Set<string> = new Set();
const project = this;

async function processUpdate()
{
const filesToProcess = new Set(changes);
const posixPaths = [];
for (const changed of filesToProcess)
{
changes.delete(changed);
// Use POSIX paths across all platforms
const posixPath = changed.split(path.posix.sep).join(path.posix.sep);
posixPaths.push(posixPath);
}
project.log.info(`Updating: [${Array.from(filesToProcess).join(", ")}]`);
await callback(posixPaths);
}

const queueUpdate = _.debounce(processUpdate, 500);
const onUpdate = async (changed: string, stats: fs.Stats) =>
{
this.log.info(`Updating: ${changed}`);
// Use POSIX paths across all platforms
const posixPath = changed.split(path.posix.sep).join(path.posix.sep);
await callback([posixPath]);
changes.add(changed);
await queueUpdate();
};
chokidar.watch(paths).on("add", onUpdate).
on("addDir", onUpdate).
Expand Down

0 comments on commit 05ef22c

Please sign in to comment.