All files / src/lib/compiler buildProject.ts

23.68% Statements 9/38
0% Branches 0/23
0% Functions 0/5
22.22% Lines 8/36

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 1241x 1x 1x           1x                             1x     1x                                                                                                                                                                                 1x             1x  
import fs from "fs-extra";
import { binjgbRoot } from "consts";
import copy from "lib/helpers/fsCopy";
import type {
  EngineFieldSchema,
  SceneTypeSchema,
} from "store/features/engine/engineState";
import { ProjectResources } from "shared/lib/resources/types";
import { buildRunner } from "./buildRunner";
 
type BuildOptions = {
  buildType: "rom" | "web" | "pocket";
  projectRoot: string;
  tmpPath: string;
  engineFields: EngineFieldSchema[];
  sceneTypes: SceneTypeSchema[];
  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",
    engineFields = [],
    sceneTypes = [],
    outputRoot = "/tmp/testing",
    debugEnabled = false,
    make = true,
    progress = (_msg: string) => {},
    warnings = (_msg: string) => {},
  }: BuildOptions
) => {
  cancelling = false;
 
  const { result, kill } = buildRunner({
    project,
    buildType,
    projectRoot,
    engineFields,
    sceneTypes,
    tmpPath,
    outputRoot,
    debugEnabled,
    make,
    progress,
    warnings,
  });
 
  cancelFunction = kill;
  const compiledData = await result;
 
  Iif (cancelling) {
    throw new Error("BUILD_CANCELLED");
  }
 
  if (buildType === "web") {
    const colorOnly = project.settings.colorMode === "color";
    const gameFile = colorOnly ? "game.gbc" : "game.gb";
    await copy(binjgbRoot, `${outputRoot}/build/web`);
    await copy(
      `${outputRoot}/build/rom/${gameFile}`,
      `${outputRoot}/build/web/rom/${gameFile}`
    );
    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/${gameFile}"`);
 
    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/game.pocket`,
      `${outputRoot}/build/pocket/game.pocket`
    );
  }
  return compiledData;
};
 
export const cancelCompileStepsInProgress = () => {
  cancelling = true;
  Iif (cancelFunction) {
    cancelFunction();
  }
};
 
export default buildProject;