#! /usr/bin/env node

/* A script that generates all paths from the public directory files.
 * Used by the service worker to cache all files beforehand.
 */

/* eslint eqeqeq: 0 */

const fs = require('fs');
const readdir = require('recursive-readdir');

readdir('public').then(files => {
  const CACHE = {};
  CACHE.html = ['/'];

  const re = /^public\/([^/]*)\/.*$/;

  files.forEach(file => {
    const directory = file.match(re) && file.match(re)[1];
    if (directory) {
      if (!CACHE[directory]) {
        CACHE[directory] = [];
      }
      CACHE[directory].push(file);
    }
  });

  const cache = JSON.stringify(CACHE, null, 2);
  fs.writeFileSync('client/scripts/public/standalones/swCache.json', cache);
})
.catch(err => console.error(`error: ${err}`));

@@ -248,16 +248,84 @@
 dir');%0A%0A
+const filePath = 'client/scripts/public/standalones/swCache.json';%0A%0A
 readdir(
@@ -393,16 +393,199 @@
 %5B'/'%5D;%0A%0A
+  // sorted so that arbitrary file path string rearrangements in their respective arrays%0A  // do not happen and thus prevents unnecessary commits%0A  const sortedFiles = files.sort();%0A%0A
   const 
@@ -617,17 +617,23 @@
 *$/;%0A%0A  
-f
+sortedF
 iles.for
@@ -841,24 +841,496 @@
    %7D%0A  %7D);%0A%0A
+  // If the file exists,%0A  if (fs.existsSync(filePath)) %7B%0A    const oldCache = JSON.parse(fs.readFileSync(filePath));%0A%0A    // check if it has been updated. If so, write over old version.%0A    const updated = !Array.prototype.concat(...Object.values(CACHE))%0A      .every(file =%3E Array.prototype.concat(...Object.values(oldCache)).includes(file));%0A%0A    if (updated) %7B%0A      console.log('buildCacheFile.js: Service Worker file paths used for caching have been updated!');%0A    
   const cach
@@ -1365,16 +1365,20 @@
 ll, 2);%0A
+    
   fs.wri
@@ -1392,56 +1392,366 @@
 ync(
-'client/scripts/public/standalones/swCache.json'
+filePath, cache);%0A    %7D%0A    else console.log('buildCacheFile.js: SW cache files have not changed. No operation.');%0A  %7D // Else write the file as it is completely new.%0A  else %7B%0A    console.log('buildCacheFile.js: Service Worker file paths used for caching have been generated!');%0A    const cache = JSON.stringify(CACHE, null, 2);%0A    fs.writeFileSync(filePath
 , ca
@@ -1756,16 +1756,20 @@
 cache);%0A
+  %7D%0A
 %7D)%0A.catc

update script to regenerate file only on changes