element-web/scripts/reskindex.js

92 lines
2.9 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
2021-01-19 14:36:21 +00:00
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const util = require('util');
2021-01-19 14:36:21 +00:00
const args = require('minimist')(process.argv);
const chokidar = require('chokidar');
2021-01-19 14:36:21 +00:00
const componentIndex = path.join('src', 'component-index.js');
const componentIndexTmp = componentIndex+".tmp";
const componentsDir = path.join('src', 'components');
const componentJsGlob = '**/*.js';
const componentTsGlob = '**/*.tsx';
let prevFiles = [];
2015-09-15 12:34:36 +00:00
async function reskindex() {
2021-01-19 14:36:21 +00:00
const jsFiles = glob.sync(componentJsGlob, {cwd: componentsDir}).sort();
const tsFiles = glob.sync(componentTsGlob, {cwd: componentsDir}).sort();
const files = [...tsFiles, ...jsFiles];
if (!filesHaveChanged(files, prevFiles)) {
return;
}
prevFiles = files;
2021-01-19 14:36:21 +00:00
const header = args.h || args.header;
2021-01-19 14:36:21 +00:00
const strm = fs.createWriteStream(componentIndexTmp);
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");
Implementation of new potential skinning mechanism With a switch to Only One Webpack™ we need a way to help developers generate the component index without a concurrent watch task. The best way to do this is to have developers import their components, but how do they do that when we support skins? The answer in this commit is to change skinning. Skinning now expects to receive your list of overrides instead of the react-sdk+branded components. For Riot this means we send over *only* the Vector components and not Vector+react-sdk. Components can then be annotated with the `replaceComponent` decorator to have them be skinnable. The decorator must take a string with the dot path of the component because we can't reliably calculate it ourselves, sadly. The decorator does a call to `getComponent` which is where the important part of the branded components not including the react-sdk is important: if the branded app includes the react-sdk then the decorator gets executed before the skin has finished loading, leading to all kinds of fun errors. This is also why the skinner lazily loads the react-sdk components to avoid importing them too early, breaking the app. The decorator will end up receiving null for a component because of the getComponent loop mentioned: the require() call is still in progress when the decorator is called, therefore we can't error out. All usages of getComponent() within the app are safe to not need such an error (the return won't be null, and developers shouldn't use getComponent() after this commit anyways). The AuthPage, being a prominent component, has been converted to demonstrate this working. Changes to riot-web are required to have this work. The reskindex script has also been altered to reflect these skinning changes - it no longer should set the react-sdk as a parent. The eventual end goal is to get rid of `getComponent()` entirely as it'll be easily replaced by imports.
2019-12-13 02:31:52 +00:00
strm.write("let components = {};\n");
2021-01-19 14:36:21 +00:00
for (let i = 0; i < files.length; ++i) {
const file = files[i].replace('.js', '').replace('.tsx', '');
2021-01-19 14:36:21 +00:00
const moduleName = (file.replace(/\//g, '.'));
const importName = moduleName.replace(/\./g, "$");
2015-11-30 17:33:04 +00:00
strm.write("import " + importName + " from './components/" + file + "';\n");
strm.write(importName + " && (components['"+moduleName+"'] = " + importName + ");");
strm.write('\n');
strm.uncork();
}
strm.write("export {components};\n");
// Ensure the file has been fully written to disk before proceeding
await util.promisify(strm.end);
fs.rename(componentIndexTmp, componentIndex, function(err) {
2021-01-19 14:42:10 +00:00
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
function filesHaveChanged(files, prevFiles) {
if (files.length !== prevFiles.length) {
return true;
}
// Check for name changes
2021-01-19 17:58:17 +00:00
for (let i = 0; i < files.length; i++) {
if (prevFiles[i] !== files[i]) {
return true;
}
}
return false;
}
// -w indicates watch mode where any FS events will trigger reskindex
if (!args.w) {
reskindex();
return;
}
2015-09-15 12:34:36 +00:00
2021-01-19 14:36:21 +00:00
let watchDebouncer = null;
chokidar.watch(path.join(componentsDir, componentJsGlob)).on('all', (event, path) => {
if (path === componentIndex) return;
if (watchDebouncer) clearTimeout(watchDebouncer);
watchDebouncer = setTimeout(reskindex, 1000);
});