Ensure component index has been written before renaming

This ensures we correctly wait for `component-index.js.tmp` to be written to
disk before trying to rename into place. This fixes an issue where stray temp
files were being strewn about the repos after renaming failures.
This commit is contained in:
J. Ryan Stinnett 2021-01-19 14:45:03 +00:00
parent 0e137bd0f6
commit 4e5f25206a

View file

@ -2,6 +2,7 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const glob = require('glob'); const glob = require('glob');
const util = require('util');
const args = require('minimist')(process.argv); const args = require('minimist')(process.argv);
const chokidar = require('chokidar'); const chokidar = require('chokidar');
@ -12,7 +13,7 @@ const componentJsGlob = '**/*.js';
const componentTsGlob = '**/*.tsx'; const componentTsGlob = '**/*.tsx';
let prevFiles = []; let prevFiles = [];
function reskindex() { async function reskindex() {
const jsFiles = glob.sync(componentJsGlob, {cwd: componentsDir}).sort(); const jsFiles = glob.sync(componentJsGlob, {cwd: componentsDir}).sort();
const tsFiles = glob.sync(componentTsGlob, {cwd: componentsDir}).sort(); const tsFiles = glob.sync(componentTsGlob, {cwd: componentsDir}).sort();
const files = [...tsFiles, ...jsFiles]; const files = [...tsFiles, ...jsFiles];
@ -51,7 +52,8 @@ 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);