From 4df51a2b0b6137617d37d5c62a3ea833518299c3 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Fri, 19 May 2017 11:44:04 +0100 Subject: [PATCH] Reskindex to a temp file, to confuse the babel watcher less (#908) I'm seeing a lot of instances where the babel watcher picks up a half-written src/component-index, and generates an empty lib file - which it then doesn't update when src/component-index is updated. Empirically, this seems to make it better. --- scripts/reskindex.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/scripts/reskindex.js b/scripts/reskindex.js index 9516614fa5..833151a298 100755 --- a/scripts/reskindex.js +++ b/scripts/reskindex.js @@ -6,6 +6,7 @@ var args = require('optimist').argv; var chokidar = require('chokidar'); var componentIndex = path.join('src', 'component-index.js'); +var componentIndexTmp = componentIndex+".tmp"; var componentsDir = path.join('src', 'components'); var componentGlob = '**/*.js'; var prevFiles = []; @@ -20,7 +21,7 @@ function reskindex() { var header = args.h || args.header; var packageJson = JSON.parse(fs.readFileSync('./package.json')); - var strm = fs.createWriteStream(componentIndex); + var strm = fs.createWriteStream(componentIndexTmp); if (header) { strm.write(fs.readFileSync(header)); @@ -35,13 +36,16 @@ function reskindex() { strm.write(" */\n\n"); if (packageJson['matrix-react-parent']) { + const parentIndex = packageJson['matrix-react-parent'] + + '/lib/component-index'; strm.write( - "module.exports.components = require('"+ - packageJson['matrix-react-parent']+ - "/lib/component-index').components;\n\n" - ); +`let components = require('${parentIndex}').components; +if (!components) { + throw new Error("'${parentIndex}' didn't export components"); +} +`); } else { - strm.write("module.exports.components = {};\n"); + strm.write("let components = {};\n"); } for (var i = 0; i < files.length; ++i) { @@ -51,13 +55,20 @@ function reskindex() { var importName = moduleName.replace(/\./g, "$"); strm.write("import " + importName + " from './components/" + file + "';\n"); - strm.write(importName + " && (module.exports.components['"+moduleName+"'] = " + importName + ");"); + strm.write(importName + " && (components['"+moduleName+"'] = " + importName + ");"); strm.write('\n'); strm.uncork(); } + strm.write("export {components};\n"); strm.end(); - console.log('Reskindex: completed'); + fs.rename(componentIndexTmp, componentIndex, function(err) { + if(err) { + console.error("Error moving new index into place: " + err); + } else { + console.log('Reskindex: completed'); + } + }); } // Expects both arrays of file names to be sorted