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