diff --git a/packages/core/src/common/utils/platform-specific-version.ts b/packages/core/src/common/utils/platform-specific-version.ts new file mode 100644 index 0000000000..adbc9971cc --- /dev/null +++ b/packages/core/src/common/utils/platform-specific-version.ts @@ -0,0 +1,20 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +export interface PlatformSpecific { + instantiate: () => T; + readonly platform: NodeJS.Platform | ""; +} + +export const getPlatformSpecificFor = (targetPlatform: NodeJS.Platform, id: string) => (specificImplementations: PlatformSpecific[]): T => { + const impl = specificImplementations.find(impl => impl.platform === targetPlatform) + ?? specificImplementations.find(impl => impl.platform === ""); + + if (!impl) { + throw new Error(`No platform specific implementation of "${id}" found`); + } + + return impl.instantiate(); +}