All files / src/lib/compiler buildWorker.ts

75.6% Statements 31/41
36.36% Branches 8/22
16.66% Functions 1/6
75% Lines 30/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 166 167 168 169 170 171 172 173 174 175 176 1771x 1x 1x   1x 1x 1x 1x   1x                                                                             1x                         4x 4x 4x 4x                 3x                   3x         3x       3x           3x   1x             3x 2x 2x                       1x                 2x           1x                   1x                   1x       1x           1x             1x      
import { parentPort, workerData, isMainThread, threadId } from "worker_threads";
import loadAllScriptEventHandlers from "lib/project/loadScriptEventHandlers";
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";
import { EngineSchema } from "lib/project/loadEngineSchema";
import { createBuildManifest, resolveBuildSources } from "./buildManifest";
import type { BuildArtifacts, BuildWorkerResult } from "./buildResult";
 
export type BuildType = "rom" | "web" | "pocket";
 
export type BuildWorkerData = {
  project: ProjectResources;
  buildType: BuildType;
  projectRoot: string;
  tmpPath: string;
  engineSchema: EngineSchema;
  outputRoot: string;
  romFilename: 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: BuildWorkerResult;
    };
 
export const buildProject = async ({
  project,
  projectRoot,
  engineSchema,
  tmpPath,
  outputRoot,
  romFilename,
  buildType,
  make,
  debugEnabled,
  l10nData,
}: BuildWorkerData): Promise<BuildWorkerResult> => {
  let artifacts: BuildArtifacts;
  try {
    setL10NData(l10nData);
    const scriptEventHandlers = await loadAllScriptEventHandlers(projectRoot);
    const compiledData = await compileData(project, {
      projectRoot,
      engineSchema,
      scriptEventHandlers,
      tmpPath,
      debugEnabled,
      progress,
      warnings,
    });
    const { pluginAttribution } = await ejectBuild({
      projectRoot,
      tmpPath,
      projectData: project,
      engineSchema,
      outputRoot,
      compiledData,
      progress,
      warnings,
    });
    await validateEjectedBuild({
      buildRoot: outputRoot,
      progress,
      warnings,
    });
    const buildSources = await resolveBuildSources(
      outputRoot,
      pluginAttribution,
    );
    const manifest = createBuildManifest({
      buildRoot: outputRoot,
      romFilename,
      cartType: project.settings.cartType,
      sources: buildSources,
    });
    artifacts = { compiledData, manifest };
  } catch (error) {
    return {
      status: "failed",
      stage: "prepare",
      error: error instanceof Error ? error.toString() : String(error),
    };
  }
 
  if (make) {
    try {
      await makeBuild({
        buildRoot: outputRoot,
        romFilename,
        tmpPath,
        buildType,
        data: project,
        debug: project.settings.generateDebugFilesEnabled,
        progress,
        warnings,
        manifest: artifacts.manifest,
      });
    } catch (error) {
      return {
        status: "failed",
        stage: "make",
        error: error instanceof Error ? error.toString() : String(error),
        ...artifacts,
      };
    }
  }
 
  return {
    status: "success",
    ...artifacts,
  };
};
 
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 () => {
  const result = await buildProject(workerData);
  send({ action: "complete", threadId, payload: result });
  process.exit(0);
};
 
parentPort?.on("message", async (message: { action: string }) => {
  if (message.action === "terminate") {
    await cancelBuildCommandsInProgress();
    process.exit(1);
  }
});
 
Iif (!isMainThread) {
  run();
}