Give `reskindex.js` a watch mode (-w)

`scripts/reskindex.js -w` will run reskindex in watch mode whereby FS events will cause a reskindex to occur.

This depends on `chokidar`
pull/21833/head
Luke Barnard 2017-05-08 14:57:04 +01:00
parent 1addc7e2c5
commit 2eaaa97451
2 changed files with 54 additions and 39 deletions

View File

@ -46,6 +46,7 @@
"blueimp-canvas-to-blob": "^3.5.0", "blueimp-canvas-to-blob": "^3.5.0",
"browser-encrypt-attachment": "^0.3.0", "browser-encrypt-attachment": "^0.3.0",
"browser-request": "^0.3.3", "browser-request": "^0.3.3",
"chokidar": "^1.6.1",
"classnames": "^2.1.2", "classnames": "^2.1.2",
"commonmark": "^0.27.0", "commonmark": "^0.27.0",
"draft-js": "^0.8.1", "draft-js": "^0.8.1",

View File

@ -1,17 +1,15 @@
#!/usr/bin/env node #!/usr/bin/env node
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var glob = require('glob'); var glob = require('glob');
var args = require('optimist').argv; var args = require('optimist').argv;
var chokidar = require('chokidar');
var header = args.h || args.header;
var componentsDir = path.join('src', 'components');
var componentIndex = path.join('src', 'component-index.js'); var componentIndex = path.join('src', 'component-index.js');
var componentsDir = path.join('src', 'components');
function reskindex() {
var header = args.h || args.header;
var packageJson = JSON.parse(fs.readFileSync('./package.json')); var packageJson = JSON.parse(fs.readFileSync('./package.json'));
var strm = fs.createWriteStream(componentIndex); var strm = fs.createWriteStream(componentIndex);
@ -26,13 +24,14 @@ 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(" * 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(" * so you'd just be trying to swim upstream like a salmon.\n");
strm.write(" * You are not 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"); strm.write(" */\n\n");
if (packageJson['matrix-react-parent']) { if (packageJson['matrix-react-parent']) {
strm.write("module.exports.components = require('"+packageJson['matrix-react-parent']+"/lib/component-index').components;\n\n"); strm.write(
"module.exports.components = require('"+
packageJson['matrix-react-parent']+
"/lib/component-index').components;\n\n"
);
} else { } else {
strm.write("module.exports.components = {};\n"); strm.write("module.exports.components = {};\n");
} }
@ -51,3 +50,18 @@ for (var i = 0; i < files.length; ++i) {
} }
strm.end(); strm.end();
console.log('Reskindex: completed');
}
// -w indicates watch mode where any FS events will trigger reskindex
if (!args.w) {
reskindex();
return;
}
var watchDebouncer = null;
chokidar.watch('./src').on('all', (event, path) => {
if (path === componentIndex) return;
if (watchDebouncer) clearTimeout(watchDebouncer);
watchDebouncer = setTimeout(reskindex, 1000);
});