All files / source/CaffeineMc Run.js

30.3% Statements 10/33
0% Branches 0/15
25% Functions 3/12
30.3% Lines 10/33

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  7x 7x 7x                                                     7x     7x           7x                                               7x       7x                                 7x           7x                                  
"use strict";
let Caf = require("caffeine-script-runtime");
Caf.defMod(module, () => {
  return Caf.importInvoke(
    [
      "BaseClass",
      "fs",
      "merge",
      "Error",
      "formattedInspect",
      "Object",
      "process",
      "compactFlattenAll",
      "Neptune",
      "path",
    ],
    [global, require("./StandardImport")],
    (
      BaseClass,
      fs,
      merge,
      Error,
      formattedInspect,
      Object,
      process,
      compactFlattenAll,
      Neptune,
      path
    ) => {
      let fileExists, Run;
      fileExists = function (filename) {
        return fs.existsSync(filename) && filename;
      };
      return (Run = Caf.defClass(class Run extends BaseClass {}, function (
        Run,
        classSuper,
        instanceSuper
      ) {
        let rewriteArgv;
        this._resolveSourceFile = function (options) {
          let sourceFile, color, found, e;
          ({ sourceFile, color } = options);
          return merge(options, {
            sourceFile: fs.existsSync(sourceFile)
              ? sourceFile
              : (found = Caf.find(require.extensions, (v, k) =>
                  fileExists(`${Caf.toString(sourceFile)}${Caf.toString(k)}`)
                ))
              ? found
              : ((e = new Error(
                  `No matching file found: ${Caf.toString(
                    formattedInspect({
                      sourceFile,
                      extensions: Object.keys(require.extensions).join(" "),
                    })
                  )}`
                )),
                (e.stack = color ? e.message.red : e.message),
                (() => {
                  throw e;
                })()),
          });
        };
        rewriteArgv = function (sourceFile, args) {
          process.argvRaw = process.argv;
          return (process.argv = compactFlattenAll(sourceFile, args));
        };
        this.runFile = (sourceFile, options) => {
          let globalCompilerOptions;
          ({ globalCompilerOptions } = Neptune.CaffeineMc);
          return (() => {
            try {
              Neptune.CaffeineMc.globalCompilerOptions = options;
              this.setupNodeForRun(
                this._resolveSourceFile(
                  (options = merge(options, { sourceFile }))
                )
              );
              return require(require.main.filename);
            } finally {
              Neptune.CaffeineMc.globalCompilerOptions = globalCompilerOptions;
            }
          })();
        };
        this.runJs = (js, options = {}) => {
          let main;
          this.setupNodeForRun(options);
          ({ main } = require);
          return main._compile(js, main.filename);
        };
        this.setupNodeForRun = function (options) {
          let sourceFile, main;
          ({ sourceFile } = options);
          ({ main } = require);
          main.filename = sourceFile = sourceFile
            ? fs.realpathSync(sourceFile)
            : "<anonymous>";
          rewriteArgv(sourceFile, options.args);
          main.moduleCache && (main.moduleCache = {});
          return (main.paths = require("module")._nodeModulePaths(
            fs.realpathSync(path.dirname(sourceFile || "./anonymous"))
          ));
        };
      }));
    }
  );
});