#! /usr/bin/env node
"use strict";
const fs = require("fs");
const exec = require("child_process").exec;
const fileName = "src/js/mainSiteURLs.json";

function fileFinder(dir, pattern) {
  return new Promise((resolve, reject) => {
    var command = `find ${dir} ${pattern}`;
    exec(command, (err, stdout) => {
      return (err) ? reject(err) : resolve(stdout);
    });
  });
}

// Find css and JS files
const findJs = fileFinder(`src/js`, `-iname "*.js"`);
const findCss = fileFinder(`src/css`, `-iname "*.css" -or -iname "*.svg" -or -iname "*.png"`);

function processResults(results) {
  const paths = results
    .reduce((current, next) => next.concat(current), [])
    .split("\n")
    .filter(item => item)
    .map(item => item.replace(/^src\//, "./"))
    .sort();
  return JSON.stringify(paths, null, 2);
}

function writeFile(template) {
  return new Promise((resolve, reject) => {
    fs.writeFile(fileName, template, (err) => {
      return (err) ? reject(err) : resolve();
    });
  });
}

// Process and generate file
Promise.all([findJs, findCss])
  .then(processResults)
  .then(writeFile)
  .catch(err => console.error(err));

@@ -116,22 +116,19 @@
 Name = %22
-src/js
+www
 /mainSit
@@ -427,14 +427,11 @@
 er(%60
-src/js
+www
 %60, %60
@@ -443,16 +443,17 @@
 e %22*.js%22
+ 
 %60);%0Acons
@@ -480,15 +480,11 @@
 er(%60
-src/css
+www
 %60, %60
@@ -735,11 +735,11 @@
 e(/%5E
-src
+www
 %5C//,

fix generate-offline-files