Use only chokidar for watching and add more logging

pull/26591/head
Johannes Marbach 2023-11-15 14:19:44 +01:00
parent e1b5c72e14
commit 08bc6d816a
1 changed files with 36 additions and 32 deletions

View File

@ -22,9 +22,6 @@ const INCLUDE_LANGS = [...new Set([...fs.readdirSync(I18N_BASE_PATH), ...fs.read
const COPY_LIST: [ const COPY_LIST: [
sourceGlob: string, sourceGlob: string,
outputPath: string, outputPath: string,
opts?: {
directwatch?: 1;
},
][] = [ ][] = [
["res/apple-app-site-association", "webapp"], ["res/apple-app-site-association", "webapp"],
["res/manifest.json", "webapp"], ["res/manifest.json", "webapp"],
@ -35,8 +32,8 @@ const COPY_LIST: [
["res/vector-icons/**", "webapp/vector-icons"], ["res/vector-icons/**", "webapp/vector-icons"],
["res/decoder-ring/**", "webapp/decoder-ring"], ["res/decoder-ring/**", "webapp/decoder-ring"],
["node_modules/matrix-react-sdk/res/media/**", "webapp/media"], ["node_modules/matrix-react-sdk/res/media/**", "webapp/media"],
["node_modules/@matrix-org/olm/olm_legacy.js", "webapp", { directwatch: 1 }], ["node_modules/@matrix-org/olm/olm_legacy.js", "webapp"],
["./config.json", "webapp", { directwatch: 1 }], ["./config.json", "webapp"],
["contribute.json", "webapp"], ["contribute.json", "webapp"],
]; ];
const argv = parseArgs(process.argv.slice(2), {}); const argv = parseArgs(process.argv.slice(2), {});
@ -60,6 +57,22 @@ if (!fs.existsSync("webapp/i18n/")) {
fs.mkdirSync("webapp/i18n/"); fs.mkdirSync("webapp/i18n/");
} }
function createCpx(source: string, dest: string): Cpx {
const cpx = new Cpx(source, dest);
if (verbose) {
cpx.on("copy", (event) => {
console.log(`Copied: ${event.srcPath} --> ${event.dstPath}`);
});
}
return cpx;
};
const logWatch = (path: string) => {
if (verbose) {
console.log(`Watching: ${path}`);
}
};
function next(i: number, err?: Error): void { function next(i: number, err?: Error): void {
errCheck(err); errCheck(err);
@ -70,39 +83,26 @@ function next(i: number, err?: Error): void {
const ent = COPY_LIST[i]; const ent = COPY_LIST[i];
const source = ent[0]; const source = ent[0];
const dest = ent[1]; const dest = ent[1];
const opts = ent[2] || {};
const cpx = new Cpx(source, dest);
if (verbose) {
cpx.on("copy", (event) => {
console.log(`Copied: ${event.srcPath} --> ${event.dstPath}`);
});
cpx.on("remove", (event) => {
console.log(`Removed: ${event.path}`);
});
}
const cb = (err?: Error): void => { const cb = (err?: Error): void => {
next(i + 1, err); next(i + 1, err);
}; };
if (watch) { if (watch) {
if (opts.directwatch) { // cpx -w creates a watcher for the parent of any files specified,
// cpx -w creates a watcher for the parent of any files specified, // which in the case of e.g. config.json is '.', which inevitably takes
// which in the case of config.json is '.', which inevitably takes // ages to crawl. To prevent this, we only use cpx for copying and resort
// ages to crawl. So we create our own watcher on the files // to chokidar for watching.
// instead. const copy = (path: string): void => {
const copy = (): void => { createCpx(path, dest).copy(errCheck);
cpx.copy(errCheck); };
}; chokidar.watch(source)
chokidar.watch(source).on("add", copy).on("change", copy).on("ready", cb).on("error", errCheck); .on("ready", () => { logWatch(source); cb(); })
} else { .on("add", copy)
cpx.on("watch-ready", cb); .on("change", copy)
cpx.on("watch-error", cb); .on("error", errCheck);
cpx.watch();
}
} else { } else {
cpx.copy(cb); createCpx(source, dest).copy(cb);
} }
} }
@ -182,7 +182,11 @@ function watchLanguage(lang: string, dest: string, langFileMap: Record<string, s
}; };
[reactSdkFile, riotWebFile].forEach(function (f) { [reactSdkFile, riotWebFile].forEach(function (f) {
chokidar.watch(f).on("add", makeLang).on("change", makeLang).on("error", errCheck); chokidar.watch(f)
.on("ready", () => { logWatch(f); })
.on("add", makeLang)
.on("change", makeLang)
.on("error", errCheck);
}); });
} }