All files / source/CaffeineMc SourceRootFinder.js

93.18% Statements 41/44
92.5% Branches 37/40
85% Functions 17/20
93.02% Lines 40/43

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  7x 7x 7x                                                 7x       9x 9x       9x 9x       7x 7x         7x   41x         31x 15x   31x   31x       7x   70x                   7x 37x   7x   96x             96x   200x 200x 200x   96x         186x             7x   137x           137x   281x 281x 37x 37x   244x   137x                 7x 9x           24x                                  
"use strict";
let Caf = require("caffeine-script-runtime");
Caf.defMod(module, () => {
  return Caf.importInvoke(
    [
      "BaseClass",
      "path",
      "Promise",
      "fs",
      "present",
      "isArray",
      "Error",
      "formattedInspect",
      "isString",
    ],
    [global, require("./StandardImport")],
    (
      BaseClass,
      path,
      Promise,
      fs,
      present,
      isArray,
      Error,
      formattedInspect,
      isString
    ) => {
      let SourceRootFinder;
      return (SourceRootFinder = Caf.defClass(
        class SourceRootFinder extends BaseClass {
          constructor(options = {}) {
            let temp;
            super(...arguments);
            this.indicatorFiles =
              (temp = options.indicatorFiles) != null
                ? temp
                : ["package.json", ".git"];
            this._validatedIndicatorFiles();
            this.resetKnownSourceRoots();
          }
        },
        function (SourceRootFinder, classSuper, instanceSuper) {
          this.property("indicatorFiles", "knownSourceRoots");
          this.classGetter("sourceRootIndicatorFiles knownSourceRoots", {
            caffeineInitFileName: function () {
              return "caffeine-mc.config.caf";
            },
          });
          this.prototype.findSourceRoot = function (directory) {
            let known;
            return (known = this._knownSourceRoots[
              (directory = path.resolve(directory))
            ])
              ? Promise.resolve(known)
              : fs.stat(directory).then((stat) => {
                  if (!stat.isDirectory()) {
                    directory = path.dirname(directory);
                  }
                  return this._findRootR(directory).then(
                    (sourceRoot) =>
                      (this._knownSourceRoots[directory] = sourceRoot || false)
                  );
                });
          };
          this.prototype.findSourceRootSync = function (directory) {
            let known;
            return (known = this._knownSourceRoots[
              (directory = path.resolve(directory))
            ])
              ? known
              : (!fs.statSync(directory).isDirectory()
                  ? (directory = path.dirname(directory))
                  : undefined,
                (this._knownSourceRoots[directory] =
                  this._findRootSyncR(directory) || false));
          };
          this.prototype.resetKnownSourceRoots = function () {
            return (this._knownSourceRoots = {});
          };
          this.prototype._findRootR = function (directory) {
            let from, into, to, i, temp;
            return Promise.all(
              ((from = this._indicatorFiles),
              (into = []),
              from != null
                ? ((to = from.length),
                  (i = 0),
                  (() => {
                    while (i < to) {
                      let file;
                      file = from[i];
                      into.push(fs.exists(path.join(directory, file)));
                      temp = i++;
                    }
                    return temp;
                  })())
                : undefined,
              into)
            ).then((rootFileExistResults) =>
              Caf.find(rootFileExistResults, null, (bool) => bool)
                ? directory
                : directory !== "/" && present(directory)
                ? this._findRootR(path.dirname(directory))
                : undefined
            );
          };
          this.prototype._findRootSyncR = function (directory) {
            let from, into, to, i, temp;
            return ((from = this._indicatorFiles),
            (into = null),
            from != null
              ? ((to = from.length),
                (i = 0),
                (() => {
                  while (i < to) {
                    let file;
                    file = from[i];
                    if (fs.existsSync(path.join(directory, file))) {
                      into = file;
                      break;
                    }
                    temp = i++;
                  }
                  return temp;
                })())
              : undefined,
            into)
              ? directory
              : directory !== "/" && present(directory)
              ? this._findRootSyncR(path.dirname(directory))
              : undefined;
          };
          this.prototype._validatedIndicatorFiles = function () {
            return !(
              isArray(this.indicatorFiles) &&
              this.indicatorFiles.length > 0 &&
              !Caf.find(
                this.indicatorFiles,
                (i) => true,
                (i) => !isString(i) || !present(i)
              )
            )
              ? (() => {
                  throw new Error(
                    `indicatorFiles must be an array of strings:\n${Caf.toString(
                      formattedInspect({ indicatorFiles: this.indicatorFiles })
                    )}`
                  );
                })()
              : undefined;
          };
        }
      ));
    }
  );
});