From 075f568f61c4b73180f12ed6caace3eb4c746dc1 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Fri, 2 Mar 2018 15:30:06 +0000 Subject: [PATCH] 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. --- scripts/copy-res.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/scripts/copy-res.js b/scripts/copy-res.js index 305ea05e..84b1d1b2 100755 --- a/scripts/copy-res.js +++ b/scripts/copy-res.js @@ -134,8 +134,19 @@ function next(i, err) { const reactSdkFile = 'node_modules/matrix-react-sdk/src/i18n/strings/' + source + '.json'; const riotWebFile = 'src/i18n/strings/' + source + '.json'; - const translations = {}; - const makeLang = () => { genLangFile(source, dest) }; + // XXX: Use a debounce because for some reason if we read the language + // file immediately after the FS event is received, the file contents + // appears empty. Possibly https://github.com/nodejs/node/issues/6112 + let makeLangDebouncer; + const makeLang = () => { + if (makeLangDebouncer) { + clearTimeout(makeLangDebouncer); + } + makeLangDebouncer = setTimeout(() => { + genLangFile(source, dest); + }, 500); + }; + [reactSdkFile, riotWebFile].forEach(function(f) { chokidar.watch(f) .on('add', makeLang) @@ -170,13 +181,13 @@ function genLangFile(lang, dest) { JSON.parse(fs.readFileSync(f).toString()) ); } catch (e) { - console.error("Failed: "+f, e); + console.error("Failed: " + f, e); throw e; } } }); - translations = weblateToCounterpart(translations) + translations = weblateToCounterpart(translations); fs.writeFileSync(dest + lang + '.json', JSON.stringify(translations, null, 4)); if (verbose) {