From a22049a67969cf1658be64193b0e6beb06dd025f Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Tue, 19 Jan 2021 14:36:21 +0000 Subject: [PATCH 1/3] Use const / let instead of var --- scripts/reskindex.js | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/scripts/reskindex.js b/scripts/reskindex.js index 9fb0e1a7c0..a57fd9cbe3 100755 --- a/scripts/reskindex.js +++ b/scripts/reskindex.js @@ -1,29 +1,29 @@ #!/usr/bin/env node -var fs = require('fs'); -var path = require('path'); -var glob = require('glob'); -var args = require('minimist')(process.argv); -var chokidar = require('chokidar'); +const fs = require('fs'); +const path = require('path'); +const glob = require('glob'); +const args = require('minimist')(process.argv); +const chokidar = require('chokidar'); -var componentIndex = path.join('src', 'component-index.js'); -var componentIndexTmp = componentIndex+".tmp"; -var componentsDir = path.join('src', 'components'); -var componentJsGlob = '**/*.js'; -var componentTsGlob = '**/*.tsx'; -var prevFiles = []; +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 = []; function reskindex() { - var jsFiles = glob.sync(componentJsGlob, {cwd: componentsDir}).sort(); - var tsFiles = glob.sync(componentTsGlob, {cwd: componentsDir}).sort(); - var files = [...tsFiles, ...jsFiles]; + 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; - var header = args.h || args.header; + const header = args.h || args.header; - var strm = fs.createWriteStream(componentIndexTmp); + const strm = fs.createWriteStream(componentIndexTmp); if (header) { strm.write(fs.readFileSync(header)); @@ -38,11 +38,11 @@ function reskindex() { strm.write(" */\n\n"); strm.write("let components = {};\n"); - for (var i = 0; i < files.length; ++i) { - var file = files[i].replace('.js', '').replace('.tsx', ''); + for (let i = 0; i < files.length; ++i) { + const file = files[i].replace('.js', '').replace('.tsx', ''); - var moduleName = (file.replace(/\//g, '.')); - var importName = moduleName.replace(/\./g, "$"); + const moduleName = (file.replace(/\//g, '.')); + const importName = moduleName.replace(/\./g, "$"); strm.write("import " + importName + " from './components/" + file + "';\n"); strm.write(importName + " && (components['"+moduleName+"'] = " + importName + ");"); @@ -67,7 +67,7 @@ function filesHaveChanged(files, prevFiles) { return true; } // 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]) { return true; } @@ -81,7 +81,7 @@ if (!args.w) { return; } -var watchDebouncer = null; +let watchDebouncer = null; chokidar.watch(path.join(componentsDir, componentJsGlob)).on('all', (event, path) => { if (path === componentIndex) return; if (watchDebouncer) clearTimeout(watchDebouncer); From 0e137bd0f60393d1bf3d41495beb4cf930d242c6 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Tue, 19 Jan 2021 14:42:10 +0000 Subject: [PATCH 2/3] Fix code style --- scripts/reskindex.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/reskindex.js b/scripts/reskindex.js index a57fd9cbe3..e3ebefd78d 100755 --- a/scripts/reskindex.js +++ b/scripts/reskindex.js @@ -53,7 +53,7 @@ function reskindex() { strm.write("export {components};\n"); strm.end(); fs.rename(componentIndexTmp, componentIndex, function(err) { - if(err) { + if (err) { console.error("Error moving new index into place: " + err); } else { console.log('Reskindex: completed'); From 4e5f25206a416177fdc8e3ff0a7bb4dfa7ef8e11 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Tue, 19 Jan 2021 14:45:03 +0000 Subject: [PATCH 3/3] 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. --- scripts/reskindex.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/reskindex.js b/scripts/reskindex.js index e3ebefd78d..1770073aad 100755 --- a/scripts/reskindex.js +++ b/scripts/reskindex.js @@ -2,6 +2,7 @@ const fs = require('fs'); const path = require('path'); const glob = require('glob'); +const util = require('util'); const args = require('minimist')(process.argv); const chokidar = require('chokidar'); @@ -12,7 +13,7 @@ const componentJsGlob = '**/*.js'; const componentTsGlob = '**/*.tsx'; let prevFiles = []; -function reskindex() { +async function reskindex() { const jsFiles = glob.sync(componentJsGlob, {cwd: componentsDir}).sort(); const tsFiles = glob.sync(componentTsGlob, {cwd: componentsDir}).sort(); const files = [...tsFiles, ...jsFiles]; @@ -51,7 +52,8 @@ function reskindex() { } 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) { if (err) { console.error("Error moving new index into place: " + err);