Merge pull request #5556 from matrix-org/jryans/fix-tmp-reskindex

Ensure component index has been written before renaming
This commit is contained in:
J. Ryan Stinnett 2021-01-19 15:19:25 +00:00 committed by GitHub
commit 4cfd6e53aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,29 +1,30 @@
#!/usr/bin/env node #!/usr/bin/env node
var fs = require('fs'); const fs = require('fs');
var path = require('path'); const path = require('path');
var glob = require('glob'); const glob = require('glob');
var args = require('minimist')(process.argv); const util = require('util');
var chokidar = require('chokidar'); const args = require('minimist')(process.argv);
const chokidar = require('chokidar');
var componentIndex = path.join('src', 'component-index.js'); const componentIndex = path.join('src', 'component-index.js');
var componentIndexTmp = componentIndex+".tmp"; const componentIndexTmp = componentIndex+".tmp";
var componentsDir = path.join('src', 'components'); const componentsDir = path.join('src', 'components');
var componentJsGlob = '**/*.js'; const componentJsGlob = '**/*.js';
var componentTsGlob = '**/*.tsx'; const componentTsGlob = '**/*.tsx';
var prevFiles = []; let prevFiles = [];
function reskindex() { async function reskindex() {
var jsFiles = glob.sync(componentJsGlob, {cwd: componentsDir}).sort(); const jsFiles = glob.sync(componentJsGlob, {cwd: componentsDir}).sort();
var tsFiles = glob.sync(componentTsGlob, {cwd: componentsDir}).sort(); const tsFiles = glob.sync(componentTsGlob, {cwd: componentsDir}).sort();
var files = [...tsFiles, ...jsFiles]; const files = [...tsFiles, ...jsFiles];
if (!filesHaveChanged(files, prevFiles)) { if (!filesHaveChanged(files, prevFiles)) {
return; return;
} }
prevFiles = files; prevFiles = files;
var header = args.h || args.header; const header = args.h || args.header;
var strm = fs.createWriteStream(componentIndexTmp); const strm = fs.createWriteStream(componentIndexTmp);
if (header) { if (header) {
strm.write(fs.readFileSync(header)); strm.write(fs.readFileSync(header));
@ -38,11 +39,11 @@ function reskindex() {
strm.write(" */\n\n"); strm.write(" */\n\n");
strm.write("let components = {};\n"); strm.write("let components = {};\n");
for (var i = 0; i < files.length; ++i) { for (let i = 0; i < files.length; ++i) {
var file = files[i].replace('.js', '').replace('.tsx', ''); const file = files[i].replace('.js', '').replace('.tsx', '');
var moduleName = (file.replace(/\//g, '.')); const moduleName = (file.replace(/\//g, '.'));
var importName = moduleName.replace(/\./g, "$"); const importName = moduleName.replace(/\./g, "$");
strm.write("import " + importName + " from './components/" + file + "';\n"); strm.write("import " + importName + " from './components/" + file + "';\n");
strm.write(importName + " && (components['"+moduleName+"'] = " + importName + ");"); strm.write(importName + " && (components['"+moduleName+"'] = " + importName + ");");
@ -51,9 +52,10 @@ function reskindex() {
} }
strm.write("export {components};\n"); strm.write("export {components};\n");
strm.end(); // Ensure the file has been fully written to disk before proceeding
await util.promisify(strm.end);
fs.rename(componentIndexTmp, componentIndex, function(err) { fs.rename(componentIndexTmp, componentIndex, function(err) {
if(err) { if (err) {
console.error("Error moving new index into place: " + err); console.error("Error moving new index into place: " + err);
} else { } else {
console.log('Reskindex: completed'); console.log('Reskindex: completed');
@ -67,7 +69,7 @@ function filesHaveChanged(files, prevFiles) {
return true; return true;
} }
// Check for name changes // Check for name changes
for (var i = 0; i < files.length; i++) { for (const i = 0; i < files.length; i++) {
if (prevFiles[i] !== files[i]) { if (prevFiles[i] !== files[i]) {
return true; return true;
} }
@ -81,7 +83,7 @@ if (!args.w) {
return; return;
} }
var watchDebouncer = null; let watchDebouncer = null;
chokidar.watch(path.join(componentsDir, componentJsGlob)).on('all', (event, path) => { chokidar.watch(path.join(componentsDir, componentJsGlob)).on('all', (event, path) => {
if (path === componentIndex) return; if (path === componentIndex) return;
if (watchDebouncer) clearTimeout(watchDebouncer); if (watchDebouncer) clearTimeout(watchDebouncer);