All files / src/lib/project/migration/versions 420to500.ts

96.49% Statements 55/57
84.78% Branches 39/46
100% Functions 14/14
96.29% Lines 52/54

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 177 178 179 180 181 182 183 184 185 18614x               14x   14x 14x 14x           14x       14x   18x                                   14x     4x       5x       14x       9x     9x 9x 9x 9x   69x 65x   4x     4x 4x       5x               9x     14x     8x       6x   2x                         14x       9x   9x 3x         9x 3x   9x   9x   9x     8x       8x 25x 25x           23x     8x         9x 3x   9x 32x   23x 23x 23x               9x                 14x                  
import {
  createScriptEventsMigrator,
  migrateEvents,
  pipeMigrationFns,
  ProjectResourcesMigration,
  ProjectResourcesMigrationFn,
  ScriptEventMigrationFn,
} from "lib/project/migration/helpers";
import { genSymbol } from "shared/lib/helpers/symbols";
import { Variable } from "shared/lib/resources/types";
import { extractVariableIdsFromScriptEvent } from "shared/lib/variables/extractVariableReferences";
import scriptEventDefs420Snapshot from "lib/project/migration/snapshots/scriptEventDefs420.json";
import {
  scriptEventDefsFromSnapshot,
  ScriptEventDefsSnapshot,
} from "lib/project/migration/snapshots/scriptEventDefs";
import type { ScriptEventDefsFieldTypeLookup } from "shared/lib/scripts/scriptDefHelpers";
 
const scriptEventDefs420 = scriptEventDefsFromSnapshot(
  scriptEventDefs420Snapshot as ScriptEventDefsSnapshot,
);
 
const scriptEventDefsForMigration = (
  currentScriptEventDefs: ScriptEventDefsFieldTypeLookup = {},
): ScriptEventDefsFieldTypeLookup => ({
  ...currentScriptEventDefs,
  ...scriptEventDefs420,
});
 
type LegacyVariable = {
  id: string;
  name: string;
  symbol: string;
  flags?: Record<string, string>;
};
 
type LegacyScriptDataTable = {
  label?: string;
  variables: string[];
  rows: unknown[];
};
 
const isLegacyScriptDataTable = (
  value: unknown,
): value is LegacyScriptDataTable =>
  typeof value === "object" &&
  value !== null &&
  "variables" in value &&
  Array.isArray(value.variables) &&
  value.variables.every((variable) => typeof variable === "string") &&
  "rows" in value &&
  Array.isArray(value.rows);
 
export const migrateFrom420r10To500r1DataTables: ScriptEventMigrationFn = (
  scriptEvent,
  context,
) => {
  Iif (!scriptEvent.args) {
    return scriptEvent;
  }
  const scriptEventDefs = scriptEventDefsForMigration(context?.scriptEventDefs);
  const fieldsLookup = scriptEventDefs[scriptEvent.command]?.fieldsLookup;
  let changed = false;
  const args = Object.fromEntries(
    Object.entries(scriptEvent.args).map(([key, value]) => {
      if (fieldsLookup?.[key]?.type !== "dataTable") {
        return [key, value];
      }
      Iif (!isLegacyScriptDataTable(value)) {
        return [key, value];
      }
      changed = true;
      return [
        key,
        {
          ...value,
          variables: value.variables.map((variable) => ({
            type: "variable" as const,
            value: variable,
          })),
        },
      ];
    }),
  );
  return changed ? { ...scriptEvent, args } : scriptEvent;
};
 
export const migrateFrom420r10To500r1DataPeek: ScriptEventMigrationFn = (
  scriptEvent,
) => {
  if (
    scriptEvent.command !== "EVENT_PEEK_DATA" ||
    typeof scriptEvent.args?.variableSource !== "string"
  ) {
    return scriptEvent;
  }
  return {
    ...scriptEvent,
    args: {
      ...scriptEvent.args,
      variableSource: {
        type: "variable",
        value: scriptEvent.args.variableSource,
      },
    },
  };
};
 
// Create global variable entry for all variable references
export const migrateFrom420r10To500r1Variables: ProjectResourcesMigrationFn = (
  resources,
  context,
) => {
  const globalVariables = resources.variables
    .variables as unknown as LegacyVariable[];
  const migratedGlobalVariables: Variable[] = globalVariables.map(
    (variable) => ({
      ...variable,
      type: "number",
    }),
  );
  const existingVariableIds = new Set(
    globalVariables.map((variable) => variable.id),
  );
  const usedLegacyVariableIds = new Set<string>();
 
  const scriptEventDefs = scriptEventDefsForMigration(context.scriptEventDefs);
 
  const migratedResources = migrateEvents(
    resources,
    (scriptEvent) => {
      const variableIds = extractVariableIdsFromScriptEvent(
        scriptEvent,
        scriptEventDefs,
      );
      for (const variableId of variableIds) {
        const variableNumber = Number(variableId);
        if (
          /^\d+$/.test(variableId) &&
          variableNumber >= 0 &&
          variableNumber < 512 &&
          !existingVariableIds.has(variableId)
        ) {
          usedLegacyVariableIds.add(variableId);
        }
      }
      return scriptEvent;
    },
    context,
  );
 
  const existingSymbols = new Set(
    globalVariables.map((variable) => variable.symbol),
  );
  const newGlobalVariables: Variable[] = Array.from(usedLegacyVariableIds)
    .sort((a, b) => Number(a) - Number(b))
    .map((variableId) => {
      const symbol = genSymbol(`var_${variableId}`, existingSymbols);
      existingSymbols.add(symbol);
      return {
        id: variableId,
        name: "",
        symbol,
        type: "number",
      };
    });
 
  return {
    ...migratedResources,
    variables: {
      ...resources.variables,
      variables: [...migratedGlobalVariables, ...newGlobalVariables],
    },
  };
};
 
export const migrate420r10To500r1: ProjectResourcesMigration = {
  from: { version: "4.2.0", release: "10" },
  to: { version: "5.0.0", release: "1" },
  migrationFn: pipeMigrationFns([
    createScriptEventsMigrator(migrateFrom420r10To500r1DataTables),
    createScriptEventsMigrator(migrateFrom420r10To500r1DataPeek),
    migrateFrom420r10To500r1Variables,
  ]),
};