All files / src/lib/compiler buildProject.ts

24.32% Statements 9/37
0% Branches 0/21
0% Functions 0/5
22.86% Lines 8/35

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 1261x 1x 1x   1x                               1x     1x                                                                                                                                                                                           1x             1x  
import fs from "fs-extra";
import { binjgbRoot } from "consts";
import copy from "lib/helpers/fsCopy";
import { ProjectResources } from "shared/lib/resources/types";
import { buildRunner } from "./buildRunner";
import { EngineSchema } from "lib/project/loadEngineSchema";
 
type BuildOptions = {
  buildType: "rom" | "web" | "pocket";
  projectRoot: string;
  tmpPath: string;
  engineSchema: EngineSchema;
  romFilename: string;
  outputRoot: string;
  make?: boolean;
  debugEnabled?: boolean;
  progress: (msg: string) => void;
  warnings: (msg: string) => void;
};
 
let cancelling = false;
let cancelFunction: (() => void) | undefined;
 
const buildProject = async (
  project: ProjectResources,
  {
    buildType = "rom",
    projectRoot = "/tmp",
    tmpPath = "/tmp",
    engineSchema,
    outputRoot = "/tmp/testing",
    romFilename,
    debugEnabled = false,
    make = true,
    progress = (_msg: string) => {},
    warnings = (_msg: string) => {},
  }: BuildOptions,
) => {
  cancelling = false;
 
  const { result, kill } = buildRunner({
    project,
    buildType,
    projectRoot,
    engineSchema,
    tmpPath,
    outputRoot,
    romFilename,
    debugEnabled,
    make,
    progress,
    warnings,
  });
 
  cancelFunction = kill;
  const compiledData = await result;
 
  Iif (cancelling) {
    throw new Error("BUILD_CANCELLED");
  }
 
  if (buildType === "web") {
    const colorCorrection =
      project.settings.colorCorrection === "default" ? 2 : 0;
    await copy(binjgbRoot, `${outputRoot}/build/web`);
    await copy(
      `${outputRoot}/build/rom/${romFilename}`,
      `${outputRoot}/build/web/rom/${romFilename}`,
    );
    const sanitize = (s: string) => String(s || "").replace(/["<>]/g, "");
    const projectName = sanitize(project.metadata.name);
    const author = sanitize(project.metadata.author);
    const colorsHead =
      project.settings.colorMode !== "mono"
        ? `<style type="text/css"> body { background-color:#${project.settings.customColorsBlack}; }</style>`
        : "";
    const customHead = project.settings.customHead || "";
    const customControls = JSON.stringify({
      up: project.settings.customControlsUp,
      down: project.settings.customControlsDown,
      left: project.settings.customControlsLeft,
      right: project.settings.customControlsRight,
      a: project.settings.customControlsA,
      b: project.settings.customControlsB,
      start: project.settings.customControlsStart,
      select: project.settings.customControlsSelect,
    });
    const html = (
      await fs.readFile(`${outputRoot}/build/web/index.html`, "utf8")
    )
      .replace(/___PROJECT_NAME___/g, projectName)
      .replace(/___AUTHOR___/g, author)
      .replace(/___COLORS_HEAD___/g, colorsHead)
      .replace(/___PROJECT_HEAD___/g, customHead)
      .replace(/___CUSTOM_CONTROLS___/g, customControls);
 
    const scriptJs = (
      await fs.readFile(`${outputRoot}/build/web/js/script.js`, "utf8")
    )
      .replace(/ROM_FILENAME = "[^"]*"/g, `ROM_FILENAME = "rom/${romFilename}"`)
      .replace(
        /CGB_COLOR_CURVE = [0-9]+/g,
        `CGB_COLOR_CURVE = ${colorCorrection}`,
      );
 
    await fs.writeFile(`${outputRoot}/build/web/index.html`, html);
    await fs.writeFile(`${outputRoot}/build/web/js/script.js`, scriptJs);
  } else Iif (buildType === "pocket") {
    await fs.mkdir(`${outputRoot}/build/pocket`);
    await copy(
      `${outputRoot}/build/rom/${romFilename}`,
      `${outputRoot}/build/pocket/${romFilename}`,
    );
  }
  return compiledData;
};
 
export const cancelCompileStepsInProgress = () => {
  cancelling = true;
  Iif (cancelFunction) {
    cancelFunction();
  }
};
 
export default buildProject;