Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use only chokidar for watching and add more logging in copy-res.ts #26591

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 44 additions & 36 deletions scripts/copy-res.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +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,
opts?: {
directwatch?: 1;
},
][] = [
const COPY_LIST: [sourceGlob: string, outputPath: string][] = [
["res/apple-app-site-association", "webapp"],
["res/manifest.json", "webapp"],
["res/sw.js", "webapp"],
Expand All @@ -35,8 +29,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), {});
Expand All @@ -60,6 +54,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);

Expand All @@ -70,39 +80,30 @@ 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);
}
}

Expand Down Expand Up @@ -182,7 +183,14 @@ function watchLanguage(lang: string, dest: string, langFileMap: Record<string, s
};

[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);
});
}

Expand Down
Loading