From 38edadac90a9c238b46d0fe5a36efe73ce4dffe2 Mon Sep 17 00:00:00 2001 From: Nabil Bourenane Date: Tue, 8 Feb 2022 14:24:45 +0100 Subject: [PATCH] Add PLATFORM_LOCAL_MANIFEST_JSON env (#1586) * kaboom * let\'s try it * readFile -> readFileSync * debug * allow local manifest initialization with env variable Co-authored-by: Nabil Bourenane Co-authored-by: Alexandre Chabrolin <9203826+chabroA@users.noreply.github.com> --- src/env.ts | 5 ++++ src/platform/PlatformAppProvider/helpers.ts | 29 +++++++++++++++++++++ src/platform/PlatformAppProvider/index.tsx | 5 ++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/env.ts b/src/env.ts index f2f2975934..b699d82027 100644 --- a/src/env.ts +++ b/src/env.ts @@ -540,6 +540,11 @@ const envDefinitions = { parser: stringParser, desc: "url used to fetch platform app manifests (staging)", }, + PLATFORM_LOCAL_MANIFEST_JSON: { + def: "", + parser: stringParser, + desc: 'json manifest for a local (test) platform app manifests. How to use: PLATFORM_LOCAL_MANIFEST_JSON="$(cat /path/to/file.json)"', + }, PLATFORM_API_URL: { def: "", parser: stringParser, diff --git a/src/platform/PlatformAppProvider/helpers.ts b/src/platform/PlatformAppProvider/helpers.ts index faa78eeac9..2c5d4404b2 100644 --- a/src/platform/PlatformAppProvider/helpers.ts +++ b/src/platform/PlatformAppProvider/helpers.ts @@ -6,6 +6,7 @@ export type FilterParams = { private?: boolean; version?: string; }; +import { getEnv } from "../../env"; function matchVersion(filterParams: FilterParams, manifest: AppManifest) { return ( @@ -52,3 +53,31 @@ export function mergeManifestLists( const newIds = new Set(list2.map((elem) => elem.id)); return [...list1.filter((elem) => !newIds.has(elem.id)), ...list2]; } + +export const initializeLocalManifest = (): Map => { + const localManifestJsonRaw: string | null | undefined = getEnv( + "PLATFORM_LOCAL_MANIFEST_JSON" + ); + + if (!localManifestJsonRaw) { + return new Map(); + } + + try { + const manifest: AppManifest | [AppManifest] = + JSON.parse(localManifestJsonRaw); + + const manifestArray = Array.isArray(manifest) ? manifest : [manifest]; + + const map = new Map(manifestArray.map((m) => [m.id, m])); + + return map; + } catch (error) { + /** + * Probable error during `JSON.parse` call, log error and return empty map + * as if env variable was not defined + */ + console.error(error); + return new Map(); + } +}; diff --git a/src/platform/PlatformAppProvider/index.tsx b/src/platform/PlatformAppProvider/index.tsx index 041c130b04..ab663845b4 100644 --- a/src/platform/PlatformAppProvider/index.tsx +++ b/src/platform/PlatformAppProvider/index.tsx @@ -9,11 +9,12 @@ import React, { import type { PlatformAppContextType, Props, State } from "./types"; import api from "./api"; import type { AppManifest } from "../types"; +import { initializeLocalManifest } from "./helpers"; // @ts-expect-error empty object creates an error const PlatformAppContext = createContext({}); const initialState: State = { - localManifests: new Map(), + localManifests: initializeLocalManifest(), remoteManifests: new Map(), isLoading: false, lastUpdateTime: undefined, @@ -28,7 +29,7 @@ export function PlatformAppProvider({ autoUpdateDelay, platformAppsServerURL, children, -}: Props) { +}: Props): JSX.Element { const [state, setState] = useState(initialState); const addLocalManifest = useCallback((manifest: AppManifest) => {