From 08bc6d816a83fc11be275cb07e8ab7cc400b3ff1 Mon Sep 17 00:00:00 2001 From: Johannes Marbach Date: Wed, 15 Nov 2023 14:19:44 +0100 Subject: [PATCH 1/2] Use only chokidar for watching and add more logging --- scripts/copy-res.ts | 68 ++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/scripts/copy-res.ts b/scripts/copy-res.ts index 83a0fe693e..b31bfdb620 100755 --- a/scripts/copy-res.ts +++ b/scripts/copy-res.ts @@ -22,9 +22,6 @@ const INCLUDE_LANGS = [...new Set([...fs.readdirSync(I18N_BASE_PATH), ...fs.read const COPY_LIST: [ sourceGlob: string, outputPath: string, - opts?: { - directwatch?: 1; - }, ][] = [ ["res/apple-app-site-association", "webapp"], ["res/manifest.json", "webapp"], @@ -35,8 +32,8 @@ const COPY_LIST: [ ["res/vector-icons/**", "webapp/vector-icons"], ["res/decoder-ring/**", "webapp/decoder-ring"], ["node_modules/matrix-react-sdk/res/media/**", "webapp/media"], - ["node_modules/@matrix-org/olm/olm_legacy.js", "webapp", { directwatch: 1 }], - ["./config.json", "webapp", { directwatch: 1 }], + ["node_modules/@matrix-org/olm/olm_legacy.js", "webapp"], + ["./config.json", "webapp"], ["contribute.json", "webapp"], ]; const argv = parseArgs(process.argv.slice(2), {}); @@ -60,6 +57,22 @@ if (!fs.existsSync("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 { errCheck(err); @@ -70,39 +83,26 @@ function next(i: number, err?: Error): void { const ent = COPY_LIST[i]; const source = ent[0]; 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 => { next(i + 1, err); }; if (watch) { - if (opts.directwatch) { - // cpx -w creates a watcher for the parent of any files specified, - // which in the case of config.json is '.', which inevitably takes - // ages to crawl. So we create our own watcher on the files - // instead. - const copy = (): void => { - cpx.copy(errCheck); - }; - chokidar.watch(source).on("add", copy).on("change", copy).on("ready", cb).on("error", errCheck); - } else { - cpx.on("watch-ready", cb); - cpx.on("watch-error", cb); - cpx.watch(); - } + // 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 + // ages to crawl. To prevent this, we only use cpx for copying and resort + // to chokidar for watching. + const copy = (path: string): void => { + createCpx(path, dest).copy(errCheck); + }; + chokidar.watch(source) + .on("ready", () => { logWatch(source); cb(); }) + .on("add", copy) + .on("change", copy) + .on("error", errCheck); } else { - cpx.copy(cb); + createCpx(source, dest).copy(cb); } } @@ -182,7 +182,11 @@ function watchLanguage(lang: string, dest: string, langFileMap: Record { logWatch(f); }) + .on("add", makeLang) + .on("change", makeLang) + .on("error", errCheck); }); } From 8a3f8a499e404909f3aa683508a0c2c864d3283e Mon Sep 17 00:00:00 2001 From: Johannes Marbach Date: Wed, 15 Nov 2023 20:35:09 +0100 Subject: [PATCH 2/2] Appease the linter --- scripts/copy-res.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/scripts/copy-res.ts b/scripts/copy-res.ts index b31bfdb620..592e41639d 100755 --- a/scripts/copy-res.ts +++ b/scripts/copy-res.ts @@ -19,10 +19,7 @@ const INCLUDE_LANGS = [...new Set([...fs.readdirSync(I18N_BASE_PATH), ...fs.read // cpx includes globbed parts of the filename in the destination, but excludes // common parents. Hence, "res/{a,b}/**": the output will be "dest/a/..." and // "dest/b/...". -const COPY_LIST: [ - sourceGlob: string, - outputPath: string, -][] = [ +const COPY_LIST: [sourceGlob: string, outputPath: string][] = [ ["res/apple-app-site-association", "webapp"], ["res/manifest.json", "webapp"], ["res/sw.js", "webapp"], @@ -65,7 +62,7 @@ function createCpx(source: string, dest: string): Cpx { }); } return cpx; -}; +} const logWatch = (path: string) => { if (verbose) { @@ -96,8 +93,12 @@ function next(i: number, err?: Error): void { const copy = (path: string): void => { createCpx(path, dest).copy(errCheck); }; - chokidar.watch(source) - .on("ready", () => { logWatch(source); cb(); }) + chokidar + .watch(source) + .on("ready", () => { + logWatch(source); + cb(); + }) .on("add", copy) .on("change", copy) .on("error", errCheck); @@ -182,8 +183,11 @@ function watchLanguage(lang: string, dest: string, langFileMap: Record { logWatch(f); }) + chokidar + .watch(f) + .on("ready", () => { + logWatch(f); + }) .on("add", makeLang) .on("change", makeLang) .on("error", errCheck);