All files / src/lib/compiler buildWorker.ts

0% Statements 0/40
0% Branches 0/16
0% Functions 0/6
0% Lines 0/40

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 158 159 160 161 162 163 164 165                                                                                                                                                                                                                                                                                                                                         
import { parentPort, workerData, isMainThread, threadId } from "worker_threads";
import loadAllScriptEventHandlers from "lib/project/loadScriptEventHandlers";
import type {
  EngineFieldSchema,
  SceneTypeSchema,
} from "store/features/engine/engineState";
import compileData from "./compileData";
import { ProjectResources } from "shared/lib/resources/types";
import { L10NLookup, setL10NData } from "shared/lib/lang/l10n";
import ejectBuild from "./ejectBuild";
import { validateEjectedBuild } from "./validate/validateEjectedBuild";
import makeBuild, { cancelBuildCommandsInProgress } from "./makeBuild";
 
export type BuildType = "rom" | "web" | "pocket";
 
export type BuildWorkerData = {
  project: ProjectResources;
  buildType: BuildType;
  projectRoot: string;
  tmpPath: string;
  engineFields: EngineFieldSchema[];
  sceneTypes: SceneTypeSchema[];
  outputRoot: string;
  make: boolean;
  debugEnabled?: boolean;
  l10nData: L10NLookup;
};
 
export type BuildTaskResponse =
  | {
      action: "progress";
      threadId: number;
      payload: {
        message: string;
      };
    }
  | {
      action: "warning";
      threadId: number;
      payload: {
        message: string;
      };
    }
  | {
      action: "complete";
      threadId: number;
      payload: Awaited<ReturnType<typeof compileData>>;
    };
 
let terminating = false;
 
const buildProject = async ({
  project,
  projectRoot,
  engineFields,
  sceneTypes,
  tmpPath,
  outputRoot,
  buildType,
  make,
  debugEnabled,
  l10nData,
}: BuildWorkerData) => {
  // Initialise l10n
  setL10NData(l10nData);
 
  // Load script event handlers + plugins
  const scriptEventHandlers = await loadAllScriptEventHandlers(projectRoot);
 
  const compiledData = await compileData(project, {
    projectRoot,
    engineFields,
    scriptEventHandlers,
    sceneTypes,
    tmpPath,
    debugEnabled,
    progress,
    warnings,
  });
 
  await ejectBuild({
    projectType: "gb",
    projectRoot,
    tmpPath,
    projectData: project,
    engineFields,
    sceneTypes,
    outputRoot,
    compiledData,
    progress,
    warnings,
  });
 
  await validateEjectedBuild({
    buildRoot: outputRoot,
    progress,
    warnings,
  });
 
  Iif (make) {
    await makeBuild({
      buildRoot: outputRoot,
      tmpPath,
      buildType,
      data: project,
      debug: project.settings.generateDebugFilesEnabled,
      progress,
      warnings,
    });
  }
 
  return compiledData;
};
 
const progress = (message: string) => {
  send({
    action: "progress",
    threadId,
    payload: {
      message,
    },
  });
};
 
const warnings = (message: string) => {
  send({
    action: "warning",
    threadId,
    payload: {
      message,
    },
  });
};
 
const send = (msg: BuildTaskResponse) => {
  parentPort?.postMessage?.(msg);
};
 
const run = async () => {
  try {
    const res = await buildProject(workerData);
    send({ action: "complete", threadId, payload: res });
    process.exit(0);
  } catch (e) {
    Iif (terminating) {
      return;
    }
    warnings(String(e));
    console.error("buildTask process terminated with error:", e);
    process.exit(1);
  }
};
 
parentPort?.on("message", async (message: { action: string }) => {
  Iif (message.action === "terminate") {
    terminating = true;
    await cancelBuildCommandsInProgress();
    process.exit(1);
  }
});
 
Iif (!isMainThread) {
  run();
}