2015-09-11 22:42:11 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var glob = require('glob');
|
|
|
|
var args = require('optimist').argv;
|
2017-05-08 21:57:04 +08:00
|
|
|
var chokidar = require('chokidar');
|
2015-09-11 22:42:11 +08:00
|
|
|
|
2017-05-08 21:57:04 +08:00
|
|
|
var componentIndex = path.join('src', 'component-index.js');
|
2015-12-01 18:26:08 +08:00
|
|
|
var componentsDir = path.join('src', 'components');
|
2017-05-08 23:29:53 +08:00
|
|
|
var componentGlob = '**/*.js';
|
2015-09-15 20:34:36 +08:00
|
|
|
|
2017-05-08 21:57:04 +08:00
|
|
|
function reskindex() {
|
|
|
|
var header = args.h || args.header;
|
|
|
|
var packageJson = JSON.parse(fs.readFileSync('./package.json'));
|
2015-09-11 22:42:11 +08:00
|
|
|
|
2017-05-08 21:57:04 +08:00
|
|
|
var strm = fs.createWriteStream(componentIndex);
|
2015-09-11 22:42:11 +08:00
|
|
|
|
2017-05-08 21:57:04 +08:00
|
|
|
if (header) {
|
|
|
|
strm.write(fs.readFileSync(header));
|
|
|
|
strm.write('\n');
|
|
|
|
}
|
2015-09-11 22:42:11 +08:00
|
|
|
|
2017-05-08 21:57:04 +08:00
|
|
|
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");
|
2015-09-11 22:42:11 +08:00
|
|
|
|
2017-05-08 21:57:04 +08:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2017-05-08 23:29:53 +08:00
|
|
|
var files = glob.sync(componentGlob, {cwd: componentsDir}).sort();
|
2017-05-08 21:57:04 +08:00
|
|
|
for (var i = 0; i < files.length; ++i) {
|
|
|
|
var file = files[i].replace('.js', '');
|
2015-09-11 22:42:11 +08:00
|
|
|
|
2017-05-08 21:57:04 +08:00
|
|
|
var moduleName = (file.replace(/\//g, '.'));
|
|
|
|
var importName = moduleName.replace(/\./g, "$");
|
2015-12-01 01:33:04 +08:00
|
|
|
|
2017-05-08 21:57:04 +08:00
|
|
|
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');
|
|
|
|
}
|
2015-09-11 22:42:11 +08:00
|
|
|
|
2017-05-08 21:57:04 +08:00
|
|
|
// -w indicates watch mode where any FS events will trigger reskindex
|
|
|
|
if (!args.w) {
|
|
|
|
reskindex();
|
|
|
|
return;
|
2015-09-11 22:42:11 +08:00
|
|
|
}
|
2015-09-15 20:34:36 +08:00
|
|
|
|
2017-05-08 21:57:04 +08:00
|
|
|
var watchDebouncer = null;
|
2017-05-08 23:29:53 +08:00
|
|
|
chokidar.watch(path.join(componentsDir, componentGlob)).on('all', (event, path) => {
|
2017-05-08 21:57:04 +08:00
|
|
|
if (path === componentIndex) return;
|
|
|
|
if (watchDebouncer) clearTimeout(watchDebouncer);
|
|
|
|
watchDebouncer = setTimeout(reskindex, 1000);
|
|
|
|
});
|