All files / source/CaffeineMc CompileCache.js

89.16% Statements 74/83
83.33% Branches 35/42
81.82% Functions 18/22
88% Lines 66/75

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198  7x   469x 7x   7x   7x   7x   7x   7x   7x   7x   7x   7x   7x                             7x 7x           7x   7x   2x     20x       7x 18x     7x 36x     7x 18x     7x 18x     7x 18x               7x   18x 18x             18x     18x 18x 18x 18x               7x   5x 5x 5x         5x           5x               7x   8x 8x 5x 5x 5x     4x 4x         4x         7x   5x 2x 2x 2x     2x 1x                     1x     1x   3x       7x 2x 2x                       7x          
// Generated by CoffeeScript 1.12.7
(function() {
  var BaseClass, CompileCache, Promise, array, consistentJsonStringify, crypto, currentSecond, defineModule, findModuleSync, findSourceRootSync, formattedInspect, fs, glob, isString, log, merge, os, path, randomBase62Character, ref, upperCamelCase,
    extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
    hasProp = {}.hasOwnProperty;
 
  ref = require('art-standard-lib'), array = ref.array, merge = ref.merge, formattedInspect = ref.formattedInspect, log = ref.log, defineModule = ref.defineModule, isString = ref.isString, upperCamelCase = ref.upperCamelCase, randomBase62Character = ref.randomBase62Character, consistentJsonStringify = ref.consistentJsonStringify, Promise = ref.Promise, currentSecond = ref.currentSecond;
 
  BaseClass = require('art-class-system').BaseClass;
 
  findSourceRootSync = require('./SourceRoots').findSourceRootSync;
 
  findModuleSync = require('./ModuleResolver').findModuleSync;
 
  require('colors');
 
  fs = require('fs-extra');
 
  glob = require('glob-promise');
 
  crypto = require('crypto');
 
  os = require('os');
 
  path = require('path');
 
 
  /*
    cachedFileKey: (object)
      compiler:   (required, object) compiler
      source:     (required, string) source-code
      sourceFile: (required, string) source file path & name
      compilerOptions: (object) all options which affect the generated output
  
    cachedFileKeyWithCompilerResults: (object)
      all the cachedFileKey fields
      compiled: (required, object) the compiler's results
   */
 
  defineModule(module, CompileCache = (function(superClass) {
    extend(CompileCache, superClass);
 
    function CompileCache() {
      return CompileCache.__super__.constructor.apply(this, arguments);
    }
 
    CompileCache.compileCacheFileNameRoot = "CaffineMcCompileCache";
 
    CompileCache.classGetter({
      compileCachePathRoot: function() {
        return os.tmpdir();
      },
      compileCacheFilePathRoot: function() {
        return this._compileCacheFilePathRoot || (this._compileCacheFilePathRoot = path.join(this.compileCachePathRoot, this.compileCacheFileNameRoot));
      }
    });
 
    CompileCache.compilerSupportsCaching = function(compiler) {
      return isString(compiler.version) && this.getCompilerName(compiler);
    };
 
    CompileCache.getCompilerName = function(compiler) {
      return (typeof compiler.getName === "function" ? compiler.getName() : void 0) || compiler.name;
    };
 
    CompileCache.getCompilerSignature = function(compiler) {
      return (this.getCompilerName(compiler)) + "-" + compiler.version;
    };
 
    CompileCache.makeSha256FilenameFriendly = function(sha256String) {
      return sha256String.replace(/[\/+=]/g, "-");
    };
 
    CompileCache.hashSource = function(source) {
      return this.makeSha256FilenameFriendly(crypto.createHmac('sha256', "no need for a real secret").update(source).digest('base64').split("=")[0]);
    };
 
 
    /*
    IN: cachedFileKey (see above)
     */
 
    CompileCache.getFileName = function(cachedFileKey) {
      var compiler, compilerOptions, relativeSourceFile, source, sourceFile, sourceRoot;
      compiler = cachedFileKey.compiler, source = cachedFileKey.source, sourceFile = cachedFileKey.sourceFile, compilerOptions = cachedFileKey.compilerOptions;
      Iif (!(compiler && sourceFile && (source != null))) {
        throw new Error("expecting compiler, source and sourceFile: " + formattedInspect({
          compiler: compiler,
          source: source,
          sourceFile: sourceFile
        }));
      }
      Iif (!this.compilerSupportsCaching(compiler)) {
        return null;
      }
      sourceRoot = findSourceRootSync(sourceFile);
      relativeSourceFile = path.relative(sourceRoot, sourceFile);
      source = "# sourceFile: " + relativeSourceFile + "\n# compilerOptions: " + (consistentJsonStringify(compilerOptions != null ? compilerOptions : null)) + "\n" + source;
      return [this.compileCacheFilePathRoot, path.basename(sourceRoot).replace(/\./g, "-"), path.basename(sourceFile).replace(/\./g, "-"), this.getCompilerSignature(compiler), this.hashSource(source)].join("_") + ".json";
    };
 
 
    /*
    IN: cachedFileKey (see above), but also with {source, compiled and props}
     */
 
    CompileCache.cache = function(cachedFileKey) {
      var compiled, fileName, props, source;
      Eif (fileName = this.getFileName(cachedFileKey)) {
        source = cachedFileKey.source, compiled = cachedFileKey.compiled, props = cachedFileKey.props;
        Iif (cachedFileKey.verbose) {
          log({
            caching: cachedFileKey.sourceFile
          });
        }
        fs.writeFileSync(fileName, JSON.stringify(merge({
          source: source,
          compiled: compiled,
          props: props
        })));
      }
      return cachedFileKey;
    };
 
 
    /*
    IN: cachedFileKey (see above)
     */
 
    CompileCache.fetch = function(cachedFileKey) {
      var cacheContents, fileName, start;
      start = currentSecond();
      if ((fileName = this.getFileName(cachedFileKey)) && fs.existsSync(fileName)) {
        if ((cacheContents = (function() {
          try {
            return JSON.parse(fs.readFileSync(fileName));
          } catch (error) {}
        })()) && cacheContents.source === cachedFileKey.source && this.verifyDependencies(cachedFileKey, cacheContents.props)) {
          cacheContents.fromCache = true;
          Iif (cachedFileKey.verbose) {
            log({
              cached: (((currentSecond() - start) * 1000 | 0) + "ms ") + cachedFileKey.sourceFile
            });
          }
          return cacheContents;
        }
      }
    };
 
    CompileCache.verifyDependencies = function(cachedFileKey, props) {
      var cachedRequireString, moduleDependencies, requireString, sourceString;
      if (moduleDependencies = props != null ? props.moduleDependencies : void 0) {
        for (sourceString in moduleDependencies) {
          cachedRequireString = moduleDependencies[sourceString];
          requireString = findModuleSync(sourceString, {
            sourceFile: cachedFileKey.sourceFile
          }).requireString;
          if (requireString !== cachedRequireString) {
            log({
              CompileCache: {
                message: "moduleDependencies changed",
                sourceString: sourceString,
                sourceFile: cachedFileKey.sourceFile,
                require: {
                  old: cachedRequireString,
                  "new": requireString
                }
              }
            });
            return false;
          }
        }
        return true;
      } else {
        return true;
      }
    };
 
    CompileCache.reset = function(verbose) {
      return glob(this.compileCacheFilePathRoot + "*").then(function(list) {
        return Promise.all(array(list, function(item) {
          return Promise.resolve(item).then(function(item) {
            return fs.unlink(item);
          }).tap(function() {
            if (verbose) {
              return log("cache-reset: ".gray + item.green + " (deleted)".gray);
            }
          });
        }));
      });
    };
 
    return CompileCache;
 
  })(BaseClass));
 
}).call(this);