All files / source/CaffeineMc FileCompiler.js

66.67% Statements 30/45
31.25% Branches 5/16
75% Functions 9/12
66.67% Lines 30/45

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  7x     7x   7x   7x   7x   7x   7x     7x                                   7x   9x 2x   9x 9x   9x           9x 9x     9x     9x 9x 9x 9x 9x       9x 9x   9x 9x 9x         9x         9x               7x          
// Generated by CoffeeScript 1.12.7
(function() {
  var CaffeineMc, FileCompiler, array, caffeineInitFileName, defineModule, each, find, findSourceRoot, findSourceRootSync, fs, getCaffeineInit, getCaffeineInitSync, log, merge, path, present, ref, ref1, w;
 
  ref = require('art-standard-lib'), defineModule = ref.defineModule, array = ref.array, log = ref.log, merge = ref.merge, present = ref.present, find = ref.find, each = ref.each, w = ref.w;
 
  fs = require('fs-extra');
 
  path = require('path');
 
  CaffeineMc = require('./namespace');
 
  ref1 = require('./SourceRoots'), getCaffeineInit = ref1.getCaffeineInit, caffeineInitFileName = ref1.caffeineInitFileName, findSourceRoot = ref1.findSourceRoot, getCaffeineInitSync = ref1.getCaffeineInitSync, findSourceRootSync = ref1.findSourceRootSync;
 
  defineModule(module, FileCompiler = (function() {
    function FileCompiler() {}
 
    FileCompiler.compileFileSync = function(sourceFile, options) {
      var caffeineInit, source, sourceRoot;
      if (options == null) {
        options = {};
      }
      if (options.outputDirectory) {
        throw new Error("outputDirectory unsupported");
      }
      source = options.source, sourceRoot = options.sourceRoot;
      sourceRoot = sourceRoot ? path.resolve(sourceRoot) : findSourceRootSync(sourceFile);
      caffeineInit = getCaffeineInitSync(sourceRoot);
      source || (source = (fs.readFileSync(sourceFile)).toString());
      return CaffeineMc.compile(source, merge(options, {
        sourceFile: sourceFile,
        sourceRoot: sourceRoot
      }), caffeineInit);
    };
 
    FileCompiler.compileFile = function(sourceFile, options) {
      var outputDirectory, source;
      if (options == null) {
        options = {};
      }
      outputDirectory = options.outputDirectory, source = options.source;
      return findSourceRoot(sourceFile).then(function(sourceRoot) {
        var result;
        result = {
          readCount: 0,
          writeCount: 0,
          outputFiles: [],
          output: null
        };
        return fs.exists(sourceFile).then(function(exists) {
          Iif (!exists) {
            throw new Error("sourceFile not found: " + sourceFile);
          }
          return getCaffeineInit(sourceRoot);
        }).then(function(arg) {
          var compiler, config, p;
          compiler = arg.compiler, config = arg.config;
          p = source ? Promise.resolve(source) : fs.readFile(sourceFile);
          return p.then(function(source) {
            source = source.toString();
            result.output = compiler.compile(source, merge(config, options, {
              sourceFile: sourceFile,
              sourceRoot: sourceRoot
            }));
            result.readCount++;
            return Promise.all(array(result.output.compiled, function(text, extension) {
              var basename, outputFilename;
              basename = path.basename(sourceFile, path.extname(sourceFile));
              result.outputFiles.push(outputFilename);
              Iif (outputDirectory) {
                result.writeCount++;
                outputFilename = path.join(outputDirectory, basename + "." + extension);
                return fs.writeFile(outputFilename, text);
              } else {
                return Promise.resolve(text);
              }
            }));
          });
        }).then(function() {
          return result;
        })["catch"](function(e) {
          log.error("error compiling: " + sourceFile);
          throw e;
        });
      });
    };
 
    return FileCompiler;
 
  })());
 
}).call(this);