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

100% Statements 26/26
100% Branches 3/3
100% Functions 11/11
100% Lines 21/21

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    10x                                   10x       8x     1x   7x                   10x       12x                 10x 10x 22x 10x   10x     1x 1x 2x         10x     1x 8x 16x         10x         12x          
import { ScriptEvent } from "shared/lib/entities/entitiesTypes";
import { CompressedProjectResources } from "shared/lib/resources/types";
import {
  mapScenesScript,
  mapActorsScript,
  mapTriggersScript,
  mapCustomScriptsScript,
} from "shared/lib/scripts/walk";
 
export type ScriptEventMigrationFn = (scriptEvent: ScriptEvent) => ScriptEvent;
export type ProjectResourcesMigrationFn = (
  resources: CompressedProjectResources
) => CompressedProjectResources;
 
export type ProjectResourcesMigration = {
  from: { version: string; release: string };
  to: { version: string; release: string };
  migrationFn: ProjectResourcesMigrationFn;
};
 
export const applyProjectResourcesMigration = (
  resources: CompressedProjectResources,
  migration: ProjectResourcesMigration
): CompressedProjectResources => {
  if (
    !isProjectVersion(migration.from.version, migration.from.release, resources)
  ) {
    return resources;
  }
  return {
    ...migration.migrationFn(resources),
    metadata: {
      ...resources.metadata,
      _version: migration.to.version,
      _release: migration.to.release,
    },
  };
};
 
export const migrateEvents = (
  resources: CompressedProjectResources,
  migrateFn: ScriptEventMigrationFn
): CompressedProjectResources => {
  return {
    ...resources,
    scenes: mapScenesScript(resources.scenes, migrateFn),
    actorPrefabs: mapActorsScript(resources.actorPrefabs, migrateFn),
    triggerPrefabs: mapTriggersScript(resources.triggerPrefabs, migrateFn),
    scripts: mapCustomScriptsScript(resources.scripts, migrateFn),
  };
};
 
export const createScriptEventsMigrator =
  (migrateFn: ScriptEventMigrationFn) =>
  (resources: CompressedProjectResources): CompressedProjectResources =>
    migrateEvents(resources, migrateFn);
 
export const pipeMigrationFns = (
  migrationFns: ProjectResourcesMigrationFn[]
): ProjectResourcesMigrationFn => {
  return (resources: CompressedProjectResources): CompressedProjectResources =>
    migrationFns.reduce(
      (currentResources, migrationFn) => migrationFn(currentResources),
      resources
    );
};
 
export const pipeScriptEventMigrationFns = (
  scriptEventMigrationFns: ScriptEventMigrationFn[]
): ScriptEventMigrationFn => {
  return (scriptEvent: ScriptEvent): ScriptEvent =>
    scriptEventMigrationFns.reduce(
      (currentScriptEvent, migrationFn) => migrationFn(currentScriptEvent),
      scriptEvent
    );
};
 
export const isProjectVersion = (
  version: string,
  release: string,
  resources: CompressedProjectResources
): boolean => {
  return (
    resources.metadata._version === version &&
    resources.metadata._release === release
  );
};