Code coverage report for lib\livecss.js

Statements: 28.74% (25 / 87)      Branches: 35% (7 / 20)      Functions: 10.53% (2 / 19)      Lines: 28.74% (25 / 87)      Ignored: none     

All files » lib\ » livecss.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141  1   1   1   1 1           1 3 3 3 3 3 1   2   3     1                                           1 1   1                                       1                                                                     1                         1       1   1 1     1                    
'use strict';
var LiveUpdateDisabled, LiveUpdateMixin, clientUpdater, debug, e, fs, incUrlSeq, socketio;
 
fs = require("fs");
 
debug = require("debug")("piler:livecss");
 
try {
  socketio = require('socket.io');
} catch (_error) {
  e = _error;
  socketio = null;
}
 
incUrlSeq = function(url) {
  var cleanUrl, match, seq, seqRegexp;
  seqRegexp = /(--([0-9]+))\..*$/;
  match = url.match(seqRegexp);
  seq = parseInt((match != null ? match[2] : void 0) || 0, 10);
  if (match) {
    cleanUrl = url.replace(match[1], "");
  } else {
    cleanUrl = url;
  }
  return cleanUrl = cleanUrl.substr(0, cleanUrl.lastIndexOf('.')) + ("--" + (seq + 1)) + cleanUrl.substr(cleanUrl.lastIndexOf('.'));
};
 
clientUpdater = function() {
  var pile;
  debug("CSS updater is active. Waiting for connection...");
  pile = io.connect('/pile');
  pile.on("connect", function() {
    return debug("CSS updater has connected");
  });
  pile.on("disconnect", function() {
    return debug("CSS updater has disconnected! Refresh to reconnect");
  });
  return pile.on("update", function(fileId) {
    var elem;
    elem = document.getElementById("pile-" + fileId);
    if (elem) {
      debug("updating", fileId, elem);
      return elem.href = PILE.incUrlSeq(elem.href);
    } else {
      return debug("id", fileId, "not found");
    }
  });
};
 
LiveUpdateMixin = (function() {
  function LiveUpdateMixin() {}
 
  LiveUpdateMixin.prototype.installSocketIo = function(userio) {
    var io;
    this.addUrl("/socket.io/socket.io.js");
    this.addOb({
      PILE: {
        incUrlSeq: incUrlSeq
      }
    });
    this.addExec(clientUpdater);
    if (!userio) {
      io = socketio.listen(this.app);
    } else {
      io = userio;
    }
    io.configure(function() {
      return io.set('log level', 0);
    });
    return this.io = io.of("/pile");
  };
 
  LiveUpdateMixin.prototype.liveUpdate = function(cssmanager, userio) {
    var listener;
    if (this.production) {
      this.logger.info("Not activating live update in production");
      return;
    }
    if (!this.app) {
      throw new Error('JSManager must be bind to a http server (Express app) before it can live update CSS');
    }
    this.installSocketIo(userio);
    listener = this.server ? this.server : this.app;
    return listener.on("listening", (function(_this) {
      return function() {
        var codeOb, k, pile, _ref, _results;
        _this.logger.info("Activating CSS updater");
        _ref = cssmanager.piles;
        _results = [];
        for (k in _ref) {
          pile = _ref[k];
          _results.push((function() {
            var _i, _len, _ref1, _results1;
            _ref1 = pile.code;
            _results1 = [];
            for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
              codeOb = _ref1[_i];
              _results1.push(this._watch(pile, codeOb));
            }
            return _results1;
          }).call(_this));
        }
        return _results;
      };
    })(this));
  };
 
  LiveUpdateMixin.prototype._watch = function(pile, codeOb) {
    if (codeOb.type !== "file") {
      return;
    }
    this.logger.info("watching " + codeOb.filePath + " for changes");
    return fs.watch(codeOb.filePath, (function(_this) {
      return function() {
        _this.logger.info("updated", codeOb.filePath);
        return _this.io.emit("update", codeOb.getId());
      };
    })(this));
  };
 
  return LiveUpdateMixin;
 
})();
 
LiveUpdateMixin.incUrlSeq = incUrlSeq;
 
Eif (socketio != null) {
  module.exports = LiveUpdateMixin;
} else {
  module.exports = LiveUpdateDisabled = (function() {
    function LiveUpdateDisabled() {}
 
    LiveUpdateDisabled.prototype.liveUpdate = function() {
      return this.logger.error("No socket.io installed. Live update won't work.");
    };
 
    return LiveUpdateDisabled;
 
  })();
}