0

I'm having a problem: Variable output doesn't change the way it should. When logging it inside the while loop it's being edited in it shows up fine. But when logging it again outside the loop the change never happened.

Here is my code:

(function() {
  var foot, fs, head, inputFile, output, outputFile;

  fs = require('fs');

  head = "var setStyles = function(){";

  foot = "}";

  inputFile = "css/index.css";

  outputFile = "compiled/index.js";

  output = head;

  fs.readFile(inputFile, 'utf8', function(err, data) {
    var css, identifier, match, prop, regex, results, selector, styleMatch, styleRegex, styles, tag, val;
    if (err) {
      console.log(err);
    }
    css = data;
    regex = /(\.|#)?([a-zA-Z-_0-9]+)\n*{([\s\S]+?)}/;
    results = [];
    while (match = css.match(regex)) {
      identifier = match[1];
      tag = match[2];
      styles = match[3];
      if (identifier === ".") {
        selector = "document.getElementsByClassName(" + tag + ");";
      } else if (identifier === "#") {
        selector = "document.getElementById(" + tag + ")";
      } else {
        selector = "document.getElementsByTagName(" + tag + ")";
      }
      output = "";
      styleRegex = /([a-zA-Z-_]+?):\s*(["'#-_a-zA-Z0-9]+?)\s*;/;
      while (styleMatch = styles.match(styleRegex)) {
        prop = styleMatch[1];
        val = styleMatch[2];
        styles = styles.replace(styleMatch[0], "");
      }
      results.push(css = css.replace(match[0], ""));
    }
    return results;
  });

  console.log(output);

  output += "\n" + foot;

  fs.writeFile(outputFile, output, function(err) {
    if (err) {
      return console.log(err);
    }
  });

}).call(this);

Could anyone please tell me what I'm doing wrong here?

Thanks in advance, Jordy

JordyvD
  • 1,565
  • 1
  • 18
  • 45

0 Answers0