All files / src/lib/project/migration helpers.ts

100% Statements 38/38
100% Branches 4/4
100% Functions 17/17
100% Lines 33/33

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          17x                                                     17x         35x     1x   34x                   17x     53x 53x 8x 14x     53x 8x 14x     53x     17x         53x 53x 98x 53x                         17x 17x 130x       37x   17x     43x       13x 32x         17x     1x       8x   16x         17x         39x          
import {
  CompressedProjectResources,
  ScriptEvent,
} from "shared/lib/resources/types";
import type { ScriptEventDefs } from "shared/lib/scripts/scriptDefHelpers";
import {
  mapScenesScript,
  mapActorsScript,
  mapTriggersScript,
  mapCustomScriptsScript,
  walkActorScripts,
  walkTriggerScripts,
} from "shared/lib/scripts/walk";
 
export type ProjectResourcesMigrationContext = {
  scriptEventDefs: ScriptEventDefs;
};
export type ScriptEventMigrationFn = (
  scriptEvent: ScriptEvent,
  context?: ProjectResourcesMigrationContext,
) => ScriptEvent;
export type ProjectResourcesMigrationFn = (
  resources: CompressedProjectResources,
  context: ProjectResourcesMigrationContext,
) => CompressedProjectResources;
 
export type ProjectResourcesMigration = {
  from: { version: string; release: string };
  to: { version: string; release: string };
  migrationFn: ProjectResourcesMigrationFn;
};
 
export const applyProjectResourcesMigration = (
  resources: CompressedProjectResources,
  migration: ProjectResourcesMigration,
  context: ProjectResourcesMigrationContext,
): CompressedProjectResources => {
  if (
    !isProjectVersion(migration.from.version, migration.from.release, resources)
  ) {
    return resources;
  }
  return {
    ...migration.migrationFn(resources, context),
    metadata: {
      ...resources.metadata,
      _version: migration.to.version,
      _release: migration.to.release,
    },
  };
};
 
const buildPrefabEventsLookup = (
  resources: CompressedProjectResources,
): Record<string, ScriptEvent> => {
  const prefabEventsLookup: Record<string, ScriptEvent> = {};
  resources.actorPrefabs.forEach((actorPrefab) => {
    walkActorScripts(actorPrefab, undefined, (e) => {
      prefabEventsLookup[e.id] = e;
    });
  });
  resources.triggerPrefabs.forEach((triggerPrefab) => {
    walkTriggerScripts(triggerPrefab, undefined, (e) => {
      prefabEventsLookup[e.id] = e;
    });
  });
  return prefabEventsLookup;
};
 
export const migrateEvents = (
  resources: CompressedProjectResources,
  migrateFn: ScriptEventMigrationFn,
  context?: ProjectResourcesMigrationContext,
): CompressedProjectResources => {
  const prefabEventsLookup = buildPrefabEventsLookup(resources);
  const migrateEvent = (scriptEvent: ScriptEvent) =>
    migrateFn(scriptEvent, context);
  return {
    ...resources,
    scenes: mapScenesScript(
      resources.scenes,
      { includePrefabOverrides: true, prefabEventsLookup },
      migrateEvent,
    ),
    actorPrefabs: mapActorsScript(resources.actorPrefabs, migrateEvent),
    triggerPrefabs: mapTriggersScript(resources.triggerPrefabs, migrateEvent),
    scripts: mapCustomScriptsScript(resources.scripts, migrateEvent),
  };
};
 
export const createScriptEventsMigrator =
  (migrateFn: ScriptEventMigrationFn) =>
  (
    resources: CompressedProjectResources,
    context: ProjectResourcesMigrationContext,
  ): CompressedProjectResources =>
    migrateEvents(resources, migrateFn, context);
 
export const pipeMigrationFns = (
  migrationFns: ProjectResourcesMigrationFn[],
): ProjectResourcesMigrationFn => {
  return (
    resources: CompressedProjectResources,
    context: ProjectResourcesMigrationContext,
  ): CompressedProjectResources =>
    migrationFns.reduce(
      (currentResources, migrationFn) => migrationFn(currentResources, context),
      resources,
    );
};
 
export const pipeScriptEventMigrationFns = (
  scriptEventMigrationFns: ScriptEventMigrationFn[],
): ScriptEventMigrationFn => {
  return (
    scriptEvent: ScriptEvent,
    context?: ProjectResourcesMigrationContext,
  ): ScriptEvent =>
    scriptEventMigrationFns.reduce(
      (currentScriptEvent, migrationFn) =>
        migrationFn(currentScriptEvent, context),
      scriptEvent,
    );
};
 
export const isProjectVersion = (
  version: string,
  release: string,
  resources: CompressedProjectResources,
): boolean => {
  return (
    resources.metadata._version === version &&
    resources.metadata._release === release
  );
};