All files / source/CaffeineMc SourceRoots.js

82.5% Statements 33/40
57.89% Branches 11/19
86.67% Functions 13/15
82.05% Lines 32/39

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  7x 7x 7x         7x 7x     7x       7x         16x     7x 7x 9x       7x   8x 8x 8x                       8x 8x 8x   7x   11x                     9x     8x   9x   9x 9x                           7x                                                 7x 13x   7x 42x       7x 24x            
"use strict";
let Caf = require("caffeine-script-runtime");
Caf.defMod(module, () => {
  return Caf.importInvoke(
    ["BaseClass", "log", "Promise", "fs", "path", "process", "Error"],
    [global, require("./StandardImport")],
    (BaseClass, log, Promise, fs, path, process, Error) => {
      let caffeineInitFileName, caffeineProjectSourceRootFinder, SourceRoots;
      caffeineInitFileName = "caffeine-mc.config.caf";
      caffeineProjectSourceRootFinder = new (require("./SourceRootFinder"))({
        indicatorFiles: ["package.json", ".git", caffeineInitFileName],
      });
      return (SourceRoots = Caf.defClass(
        class SourceRoots extends BaseClass {},
        function (SourceRoots, classSuper, instanceSuper) {
          let _Metacompiler, newMetacompiler, evalCapturingModuleExports;
          this.classGetter("caffeineInits", {
            sourceRootIndicatorFiles: function () {
              return caffeineProjectSourceRootFinder.indicatorFiles;
            },
            caffeineInitFileName: function () {
              return caffeineInitFileName;
            },
          });
          _Metacompiler = null;
          newMetacompiler = function () {
            return new (_Metacompiler != null
              ? _Metacompiler
              : (_Metacompiler = require("./Metacompiler")))();
          };
          evalCapturingModuleExports = function (source) {
            let e, exports;
            global.__caffeineMcModule = {};
            try {
              eval(
                `(function(module){${Caf.toString(
                  source
                )}})(__caffeineMcModule);`
              );
            } catch (error) {
              e = error;
              log.error({
                "ERROR evalCapturingModuleExports": { source, error: e },
              });
              throw e;
            }
            ({ exports } = global.__caffeineMcModule || {});
            global.__caffeineMcModule = null;
            return exports;
          };
          this.getCaffeineInit = (sourceRoot = process.cwd()) => {
            let res, sourceFile;
            return (res = this.caffeineInits[sourceRoot]) != null
              ? Promise.resolve(res)
              : fs
                  .exists(
                    (sourceFile = path.join(
                      sourceRoot,
                      this.caffeineInitFileName
                    ))
                  )
                  .then((exists) => {
                    let contentsPromise;
                    contentsPromise = exists
                      ? fs
                          .readFile(sourceFile)
                          .then((contents) => (contents = contents.toString()))
                      : Promise.resolve(false);
                    return contentsPromise.then((contents) => {
                      let metacompiler, result;
                      metacompiler = newMetacompiler();
                      return (this.caffeineInits[sourceRoot] = {
                        compiler: metacompiler,
                        config: (result =
                          contents &&
                          metacompiler.compile(contents, {
                            sourceFile,
                            sourceRoot,
                          }))
                          ? evalCapturingModuleExports(result.compiled.js)
                          : {},
                      });
                    });
                  });
          };
          this.getCaffeineInitSync = (sourceRoot) => {
            let res, sourceFile, contents, metacompiler, result;
            if (!sourceRoot) {
              throw new Error("no sourceRoot");
            }
            return (res = this.caffeineInits[sourceRoot]) != null
              ? res
              : fs.existsSync(
                  (sourceFile = path.join(
                    sourceRoot,
                    this.caffeineInitFileName
                  ))
                )
              ? ((contents = fs.readFileSync(sourceFile).toString()),
                (metacompiler = newMetacompiler()),
                (result = metacompiler.compile(contents, {
                  sourceFile,
                  sourceRoot,
                })),
                (this.caffeineInits[sourceRoot] = {
                  compiler: metacompiler.compiler,
                  config: evalCapturingModuleExports(result.compiled.js),
                }))
              : false;
          };
          this.findSourceRoot = function (directory) {
            return caffeineProjectSourceRootFinder.findSourceRoot(directory);
          };
          this.findSourceRootSync = function (directory) {
            return caffeineProjectSourceRootFinder.findSourceRootSync(
              directory
            );
          };
          this._caffeineInits = {};
          this._resetSourceRoots = () => (this._caffeineInits = {});
        }
      ));
    }
  );
});