Skip to content

Commit 075f568

Browse files
committed
Fix crash; fs event received /w langauge file empty
It was common, at least for me, to experience a crash produced when running the `matrix-react-sdk/scripts/gen-i18n.js` script because when writing en_EN.json (via the script or otherwise) choikdar would receive an event and cause the file to be read, but the file would be empty when clearly it wasn't. This would happen even when doing an atomic write in gen-i18n. The fix adds a debounce as a workaround.
1 parent e1e8d97 commit 075f568

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

‎scripts/copy-res.js‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,19 @@ function next(i, err) {
134134
const reactSdkFile = 'node_modules/matrix-react-sdk/src/i18n/strings/' + source + '.json';
135135
const riotWebFile = 'src/i18n/strings/' + source + '.json';
136136

137-
const translations = {};
138-
const makeLang = () => { genLangFile(source, dest) };
137+
// XXX: Use a debounce because for some reason if we read the language
138+
// file immediately after the FS event is received, the file contents
139+
// appears empty. Possibly https://github.com/nodejs/node/issues/6112
140+
let makeLangDebouncer;
141+
const makeLang = () => {
142+
if (makeLangDebouncer) {
143+
clearTimeout(makeLangDebouncer);
144+
}
145+
makeLangDebouncer = setTimeout(() => {
146+
genLangFile(source, dest);
147+
}, 500);
148+
};
149+
139150
[reactSdkFile, riotWebFile].forEach(function(f) {
140151
chokidar.watch(f)
141152
.on('add', makeLang)
@@ -170,13 +181,13 @@ function genLangFile(lang, dest) {
170181
JSON.parse(fs.readFileSync(f).toString())
171182
);
172183
} catch (e) {
173-
console.error("Failed: "+f, e);
184+
console.error("Failed: " + f, e);
174185
throw e;
175186
}
176187
}
177188
});
178189

179-
translations = weblateToCounterpart(translations)
190+
translations = weblateToCounterpart(translations);
180191

181192
fs.writeFileSync(dest + lang + '.json', JSON.stringify(translations, null, 4));
182193
if (verbose) {

0 commit comments

Comments
 (0)