"Fossies" - the Fresh Open Source Software Archive

Member "lens-5.3.3/src/migrations/hotbar-store/5.0.0-beta.10.ts" (14 Dec 2021, 6143 Bytes) of package /linux/misc/lens-5.3.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "5.0.0-beta.10.ts": 5.2.7_vs_5.3.0.

    1 /**
    2  * Copyright (c) 2021 OpenLens Authors
    3  *
    4  * Permission is hereby granted, free of charge, to any person obtaining a copy of
    5  * this software and associated documentation files (the "Software"), to deal in
    6  * the Software without restriction, including without limitation the rights to
    7  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
    8  * the Software, and to permit persons to whom the Software is furnished to do so,
    9  * subject to the following conditions:
   10  *
   11  * The above copyright notice and this permission notice shall be included in all
   12  * copies or substantial portions of the Software.
   13  *
   14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
   16  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
   17  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   18  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   20  */
   21 
   22 import fse from "fs-extra";
   23 import { isNull } from "lodash";
   24 import path from "path";
   25 import * as uuid from "uuid";
   26 import { AppPaths } from "../../common/app-paths";
   27 import type { ClusterStoreModel } from "../../common/cluster-store";
   28 import { defaultHotbarCells, getEmptyHotbar, Hotbar, HotbarItem } from "../../common/hotbar-types";
   29 import { catalogEntity } from "../../main/catalog-sources/general";
   30 import { MigrationDeclaration, migrationLog } from "../helpers";
   31 import { generateNewIdFor } from "../utils";
   32 
   33 interface Pre500WorkspaceStoreModel {
   34   workspaces: {
   35     id: string;
   36     name: string;
   37   }[];
   38 }
   39 
   40 interface PartialHotbar {
   41   id: string;
   42   name: string;
   43   items: (null | HotbarItem)[];
   44 }
   45 
   46 export default {
   47   version: "5.0.0-beta.10",
   48   run(store) {
   49     const rawHotbars = store.get("hotbars");
   50     const hotbars: Hotbar[] = Array.isArray(rawHotbars) ? rawHotbars.filter(h => h && typeof h === "object") : [];
   51     const userDataPath = AppPaths.get("userData");
   52 
   53     // Hotbars might be empty, if some of the previous migrations weren't run
   54     if (hotbars.length === 0) {
   55       const hotbar = getEmptyHotbar("default");
   56       const { metadata: { uid, name, source }} = catalogEntity;
   57 
   58       hotbar.items[0] = { entity: { uid, name, source }};
   59 
   60       hotbars.push(hotbar);
   61     }
   62 
   63     try {
   64       const workspaceStoreData: Pre500WorkspaceStoreModel = fse.readJsonSync(path.join(userDataPath, "lens-workspace-store.json"));
   65       const { clusters }: ClusterStoreModel = fse.readJSONSync(path.join(userDataPath, "lens-cluster-store.json"));
   66       const workspaceHotbars = new Map<string, PartialHotbar>(); // mapping from WorkspaceId to HotBar
   67 
   68       for (const { id, name } of workspaceStoreData.workspaces) {
   69         migrationLog(`Creating new hotbar for ${name}`);
   70         workspaceHotbars.set(id, {
   71           id: uuid.v4(), // don't use the old IDs as they aren't necessarily UUIDs
   72           items: [],
   73           name: `Workspace: ${name}`,
   74         });
   75       }
   76 
   77       {
   78         // grab the default named hotbar or the first.
   79         const defaultHotbarIndex = Math.max(0, hotbars.findIndex(hotbar => hotbar.name === "default"));
   80         const [{ name, id, items }] = hotbars.splice(defaultHotbarIndex, 1);
   81 
   82         workspaceHotbars.set("default", {
   83           name,
   84           id,
   85           items: items.filter(Boolean),
   86         });
   87       }
   88 
   89       for (const cluster of clusters) {
   90         const uid = generateNewIdFor(cluster);
   91 
   92         for (const workspaceId of cluster.workspaces ?? [cluster.workspace].filter(Boolean)) {
   93           const workspaceHotbar = workspaceHotbars.get(workspaceId);
   94 
   95           if (!workspaceHotbar) {
   96             migrationLog(`Cluster ${uid} has unknown workspace ID, skipping`);
   97             continue;
   98           }
   99 
  100           migrationLog(`Adding cluster ${uid} to ${workspaceHotbar.name}`);
  101 
  102           if (workspaceHotbar?.items.length < defaultHotbarCells) {
  103             workspaceHotbar.items.push({
  104               entity: {
  105                 uid: generateNewIdFor(cluster),
  106                 name: cluster.preferences.clusterName || cluster.contextName,
  107               },
  108             });
  109           }
  110         }
  111       }
  112 
  113       for (const hotbar of workspaceHotbars.values()) {
  114         if (hotbar.items.length === 0) {
  115           migrationLog(`Skipping ${hotbar.name} due to it being empty`);
  116           continue;
  117         }
  118 
  119         while (hotbar.items.length < defaultHotbarCells) {
  120           hotbar.items.push(null);
  121         }
  122 
  123         hotbars.push(hotbar as Hotbar);
  124       }
  125 
  126       /**
  127        * Finally, make sure that the catalog entity hotbar item is in place.
  128        * Just in case something else removed it.
  129        *
  130        * if every hotbar hasĀ elements that all not the `catalog-entity` item
  131        */
  132       if (hotbars.every(hotbar => hotbar.items.every(item => item?.entity?.uid !== "catalog-entity"))) {
  133         // note, we will add a new whole hotbar here called "default" if that was previously removed
  134         const defaultHotbar = hotbars.find(hotbar => hotbar.name === "default");
  135         const { metadata: { uid, name, source }} = catalogEntity;
  136 
  137         if (defaultHotbar) {
  138           const freeIndex = defaultHotbar.items.findIndex(isNull);
  139 
  140           if (freeIndex === -1) {
  141             // making a new hotbar is less destructive if the first hotbar
  142             // called "default" is full than overriding a hotbar item
  143             const hotbar = getEmptyHotbar("initial");
  144 
  145             hotbar.items[0] = { entity: { uid, name, source }};
  146             hotbars.unshift(hotbar);
  147           } else {
  148             defaultHotbar.items[freeIndex] = { entity: { uid, name, source }};
  149           }
  150         } else {
  151           const hotbar = getEmptyHotbar("default");
  152 
  153           hotbar.items[0] = { entity: { uid, name, source }};
  154           hotbars.unshift(hotbar);
  155         }
  156       }
  157 
  158     } catch (error) {
  159       // ignore files being missing
  160       if (error.code !== "ENOENT") {
  161         throw error;
  162       }
  163     }
  164 
  165     store.set("hotbars", hotbars);
  166   },
  167 } as MigrationDeclaration;