#!/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');

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 = [];

function reskindex() {
    var jsFiles = glob.sync(componentJsGlob, {cwd: componentsDir}).sort();
    var tsFiles = glob.sync(componentTsGlob, {cwd: componentsDir}).sort();
    var files = [...tsFiles, ...jsFiles];
    if (!filesHaveChanged(files, prevFiles)) {
        return;
    }
    prevFiles = files;

    var header = args.h || args.header;

    var 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");
    strm.write("let components = {};\n");

    for (var i = 0; i < files.length; ++i) {
        var file = files[i].replace('.js', '').replace('.tsx', '');

        var moduleName = (file.replace(/\//g, '.'));
        var importName = moduleName.replace(/\./g, "$");

        strm.write("import " + importName + " from './components/" + file + "';\n");
        strm.write(importName + " && (components['"+moduleName+"'] = " + importName + ");");
        strm.write('\n');
        strm.uncork();
    }

    strm.write("export {components};\n");
    strm.end();
    fs.rename(componentIndexTmp, componentIndex, function(err) {
        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
    for (var 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;
}

var watchDebouncer = null;
chokidar.watch(path.join(componentsDir, componentJsGlob)).on('all', (event, path) => {
    if (path === componentIndex) return;
    if (watchDebouncer) clearTimeout(watchDebouncer);
    watchDebouncer = setTimeout(reskindex, 1000);
});

@@ -13,19 +13,21 @@
 nv node%0A
-var
+const
  fs = re
@@ -39,19 +39,21 @@
 ('fs');%0A
-var
+const
  path = 
@@ -69,19 +69,21 @@
 path');%0A
-var
+const
  glob = 
@@ -99,19 +99,21 @@
 glob');%0A
-var
+const
  args = 
@@ -147,19 +147,21 @@
 .argv);%0A
-var
+const
  chokida
@@ -186,19 +186,21 @@
 dar');%0A%0A
-var
+const
  compone
@@ -245,27 +245,29 @@
 index.js');%0A
-var
+const
  componentIn
@@ -298,19 +298,21 @@
 %22.tmp%22;%0A
-var
+const
  compone
@@ -352,19 +352,21 @@
 ents');%0A
-var
+const
  compone
@@ -387,19 +387,21 @@
 /*.js';%0A
-var
+const
  compone
@@ -423,19 +423,19 @@
 *.tsx';%0A
-var
+let
  prevFil
@@ -471,19 +471,21 @@
 ) %7B%0A    
-var
+const
  jsFiles
@@ -548,19 +548,21 @@
 ();%0A    
-var
+const
  tsFiles
@@ -621,27 +621,29 @@
 sort();%0A    
-var
+const
  files = %5B..
@@ -762,19 +762,21 @@
 s;%0A%0A    
-var
+const
  header 
@@ -805,19 +805,21 @@
 r;%0A%0A    
-var
+const
  strm = 
@@ -1315,35 +1315,35 @@
 %5Cn%22);%0A%0A    for (
-var
+let
  i = 0; i %3C file
@@ -1367,19 +1367,21 @@
         
-var
+const
  file = 
@@ -1438,19 +1438,21 @@
         
-var
+const
  moduleN
@@ -1493,19 +1493,21 @@
         
-var
+const
  importN
@@ -2286,19 +2286,21 @@
    for (
-var
+const
  i = 0; 
@@ -2548,11 +2548,11 @@
 %0A%7D%0A%0A
-var
+let
  wat

Use const / let instead of var