All files / src/lib/compiler buildProject.ts

92% Statements 23/25
52.17% Branches 12/23
50% Functions 2/4
91.66% Lines 22/24

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 1022x 2x   2x   2x                                     2x                               5x                           5x 5x 5x 5x     5x 3x     2x 2x 1x                 1x               1x               1x     2x 1x 1x       2x  
import fs from "fs-extra";
import copy from "lib/helpers/fsCopy";
import { ProjectResources } from "shared/lib/resources/types";
import { buildRunner } from "./buildRunner";
import { EngineSchema } from "lib/project/loadEngineSchema";
import { exportWebBuild } from "./webBuild";
import type { BuildResult } from "./buildResult";
 
type BuildOptions = {
  buildType: "rom" | "web" | "pocket";
  projectRoot: string;
  tmpPath: string;
  engineSchema: EngineSchema;
  romFilename: string;
  outputRoot: string;
  make?: boolean;
  debugEnabled?: boolean;
  useCustomWebTemplate?: boolean;
  progress: (msg: string) => void;
  warnings: (msg: string) => void;
};
 
let cancelFunction: (() => void) | undefined;
 
const buildProject = async (
  project: ProjectResources,
  {
    buildType = "rom",
    projectRoot = "/tmp",
    tmpPath = "/tmp",
    engineSchema,
    outputRoot = "/tmp/testing",
    romFilename,
    debugEnabled = false,
    useCustomWebTemplate = true,
    make = true,
    progress = (_msg: string) => {},
    warnings = (_msg: string) => {},
  }: BuildOptions,
): Promise<BuildResult> => {
  const { result, kill } = buildRunner({
    project,
    buildType,
    projectRoot,
    engineSchema,
    tmpPath,
    outputRoot,
    romFilename,
    debugEnabled,
    make,
    progress,
    warnings,
  });
 
  cancelFunction = kill;
  const buildResult = await result;
  Eif (cancelFunction === kill) {
    cancelFunction = undefined;
  }
 
  if (buildResult.status !== "success") {
    return buildResult;
  }
 
  try {
    if (buildType === "web") {
      await exportWebBuild({
        project,
        projectRoot,
        destination: `${outputRoot}/build/web`,
        romFilename,
        romPath: `${outputRoot}/build/rom/${romFilename}`,
        webTemplate: useCustomWebTemplate ? project.settings.webTemplate : "",
        warnings,
      });
    } else Iif (buildType === "pocket") {
      await fs.mkdir(`${outputRoot}/build/pocket`);
      await copy(
        `${outputRoot}/build/rom/${romFilename}`,
        `${outputRoot}/build/pocket/${romFilename}`,
      );
    }
  } catch (error) {
    return {
      status: "failed",
      stage: "export",
      error: error instanceof Error ? error.toString() : String(error),
      compiledData: buildResult.compiledData,
      manifest: buildResult.manifest,
    };
  }
  return buildResult;
};
 
export const cancelCompileStepsInProgress = () => {
  Eif (cancelFunction) {
    cancelFunction();
  }
};
 
export default buildProject;