diff --git a/package.json b/package.json index 2001f0d4ad..b019c63da5 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "blueimp-canvas-to-blob": "^3.5.0", "browser-encrypt-attachment": "^0.3.0", "browser-request": "^0.3.3", + "chokidar": "^1.6.1", "classnames": "^2.1.2", "commonmark": "^0.27.0", "draft-js": "^0.8.1", diff --git a/scripts/reskindex.js b/scripts/reskindex.js index f9cbc2a711..e82104f35c 100755 --- a/scripts/reskindex.js +++ b/scripts/reskindex.js @@ -1,53 +1,67 @@ #!/usr/bin/env node - var fs = require('fs'); var path = require('path'); var glob = require('glob'); - var args = require('optimist').argv; - -var header = args.h || args.header; - -var componentsDir = path.join('src', 'components'); +var chokidar = require('chokidar'); var componentIndex = path.join('src', 'component-index.js'); +var componentsDir = path.join('src', 'components'); -var packageJson = JSON.parse(fs.readFileSync('./package.json')); +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(componentIndex); -if (header) { - strm.write(fs.readFileSync(header)); - strm.write('\n'); + if (header) { + strm.write(fs.readFileSync(header)); + strm.write('\n'); + } + + strm.write("/*\n"); + strm.write(" * THIS FILE IS AUTO-GENERATED\n"); + strm.write(" * You can edit it you like, but your changes will be overwritten,\n"); + strm.write(" * so you'd just be trying to swim upstream like a salmon.\n"); + strm.write(" * You are not a salmon.\n"); + strm.write(" */\n\n"); + + if (packageJson['matrix-react-parent']) { + strm.write( + "module.exports.components = require('"+ + packageJson['matrix-react-parent']+ + "/lib/component-index').components;\n\n" + ); + } else { + strm.write("module.exports.components = {};\n"); + } + + var files = glob.sync('**/*.js', {cwd: componentsDir}).sort(); + for (var i = 0; i < files.length; ++i) { + var file = files[i].replace('.js', ''); + + var moduleName = (file.replace(/\//g, '.')); + var importName = moduleName.replace(/\./g, "$"); + + strm.write("import " + importName + " from './components/" + file + "';\n"); + strm.write(importName + " && (module.exports.components['"+moduleName+"'] = " + importName + ");"); + strm.write('\n'); + strm.uncork(); + } + + strm.end(); + console.log('Reskindex: completed'); } -strm.write("/*\n"); -strm.write(" * THIS FILE IS AUTO-GENERATED\n"); -strm.write(" * You can edit it you like, but your changes will be overwritten,\n"); -strm.write(" * so you'd just be trying to swim upstream like a salmon.\n"); -strm.write(" * You are not a salmon.\n"); -strm.write(" *\n"); -strm.write(" * To update it, run:\n"); -strm.write(" * ./reskindex.js -h header\n"); -strm.write(" */\n\n"); - -if (packageJson['matrix-react-parent']) { - strm.write("module.exports.components = require('"+packageJson['matrix-react-parent']+"/lib/component-index').components;\n\n"); -} else { - strm.write("module.exports.components = {};\n"); +// -w indicates watch mode where any FS events will trigger reskindex +if (!args.w) { + reskindex(); + return; } -var files = glob.sync('**/*.js', {cwd: componentsDir}).sort(); -for (var i = 0; i < files.length; ++i) { - var file = files[i].replace('.js', ''); - - var moduleName = (file.replace(/\//g, '.')); - var importName = moduleName.replace(/\./g, "$"); - - strm.write("import " + importName + " from './components/" + file + "';\n"); - strm.write(importName + " && (module.exports.components['"+moduleName+"'] = " + importName + ");"); - strm.write('\n'); - strm.uncork(); -} - -strm.end(); +var watchDebouncer = null; +chokidar.watch('./src').on('all', (event, path) => { + if (path === componentIndex) return; + if (watchDebouncer) clearTimeout(watchDebouncer); + watchDebouncer = setTimeout(reskindex, 1000); +});