2015-09-11 16:42:11 +02: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 15:57:04 +02:00
|
|
|
var chokidar = require('chokidar');
|
2015-09-11 16:42:11 +02:00
|
|
|
|
2017-05-08 15:57:04 +02:00
|
|
|
var componentIndex = path.join('src', 'component-index.js');
|
2017-05-19 12:44:04 +02:00
|
|
|
var componentIndexTmp = componentIndex+".tmp";
|
2015-12-01 11:26:08 +01:00
|
|
|
var componentsDir = path.join('src', 'components');
|
2017-05-08 17:29:53 +02:00
|
|
|
var componentGlob = '**/*.js';
|
2017-05-15 11:16:47 +02:00
|
|
|
var prevFiles = [];
|
2015-09-15 14:34:36 +02:00
|
|
|
|
2017-05-08 15:57:04 +02:00
|
|
|
function reskindex() {
|
2017-05-15 11:16:47 +02:00
|
|
|
var files = glob.sync(componentGlob, {cwd: componentsDir}).sort();
|
|
|
|
if (!filesHaveChanged(files, prevFiles)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
prevFiles = files;
|
|
|
|
|
2017-05-08 15:57:04 +02:00
|
|
|
var header = args.h || args.header;
|
2015-09-11 16:42:11 +02:00
|
|
|
|
2017-05-19 12:44:04 +02:00
|
|
|
var strm = fs.createWriteStream(componentIndexTmp);
|
2015-09-11 16:42:11 +02:00
|
|
|
|
2017-05-08 15:57:04 +02:00
|
|
|
if (header) {
|
|
|
|
strm.write(fs.readFileSync(header));
|
|
|
|
strm.write('\n');
|
|
|
|
}
|
2015-09-11 16:42:11 +02:00
|
|
|
|
2017-05-08 15:57:04 +02: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");
|
2019-12-13 03:31:52 +01:00
|
|
|
strm.write("let components = {};\n");
|
2017-05-08 15:57:04 +02:00
|
|
|
|
|
|
|
for (var i = 0; i < files.length; ++i) {
|
|
|
|
var file = files[i].replace('.js', '');
|
2015-09-11 16:42:11 +02:00
|
|
|
|
2017-05-08 15:57:04 +02:00
|
|
|
var moduleName = (file.replace(/\//g, '.'));
|
|
|
|
var importName = moduleName.replace(/\./g, "$");
|
2015-11-30 18:33:04 +01:00
|
|
|
|
2017-05-08 15:57:04 +02:00
|
|
|
strm.write("import " + importName + " from './components/" + file + "';\n");
|
2017-05-19 12:44:04 +02:00
|
|
|
strm.write(importName + " && (components['"+moduleName+"'] = " + importName + ");");
|
2017-05-08 15:57:04 +02:00
|
|
|
strm.write('\n');
|
|
|
|
strm.uncork();
|
|
|
|
}
|
|
|
|
|
2017-05-19 12:44:04 +02:00
|
|
|
strm.write("export {components};\n");
|
2017-05-08 15:57:04 +02:00
|
|
|
strm.end();
|
2017-05-19 12:44:04 +02:00
|
|
|
fs.rename(componentIndexTmp, componentIndex, function(err) {
|
|
|
|
if(err) {
|
|
|
|
console.error("Error moving new index into place: " + err);
|
|
|
|
} else {
|
|
|
|
console.log('Reskindex: completed');
|
|
|
|
}
|
|
|
|
});
|
2017-05-08 15:57:04 +02:00
|
|
|
}
|
2015-09-11 16:42:11 +02:00
|
|
|
|
2017-05-15 11:16:47 +02:00
|
|
|
// Expects both arrays of file names to be sorted
|
|
|
|
function filesHaveChanged(files, prevFiles) {
|
|
|
|
if (files.length !== prevFiles.length) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Check for name changes
|
|
|
|
for (var i = 0; i < files.length; i++) {
|
|
|
|
if (prevFiles[i] !== files[i]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-08 15:57:04 +02:00
|
|
|
// -w indicates watch mode where any FS events will trigger reskindex
|
|
|
|
if (!args.w) {
|
|
|
|
reskindex();
|
|
|
|
return;
|
2015-09-11 16:42:11 +02:00
|
|
|
}
|
2015-09-15 14:34:36 +02:00
|
|
|
|
2017-05-08 15:57:04 +02:00
|
|
|
var watchDebouncer = null;
|
2017-05-08 17:29:53 +02:00
|
|
|
chokidar.watch(path.join(componentsDir, componentGlob)).on('all', (event, path) => {
|
2017-05-08 15:57:04 +02:00
|
|
|
if (path === componentIndex) return;
|
|
|
|
if (watchDebouncer) clearTimeout(watchDebouncer);
|
|
|
|
watchDebouncer = setTimeout(reskindex, 1000);
|
|
|
|
});
|