1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/features/shell-sync/main/compute-shell-environment.injectable.ts
Sebastian Malton cb9d978e5a Add new Result type to complement AsyncResult
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-01-20 10:13:06 -05:00

52 lines
1.7 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { AsyncResult } from "../../../common/utils/result";
import { getInjectable } from "@ogre-tools/injectable";
import isWindowsInjectable from "../../../common/vars/is-windows.injectable";
import computeUnixShellEnvironmentInjectable from "./compute-unix-shell-environment.injectable";
export type EnvironmentVariables = Partial<Record<string, string>>;
export type ComputeShellEnvironment = (shell: string) => Promise<AsyncResult<EnvironmentVariables | undefined, string>>;
const computeShellEnvironmentInjectable = getInjectable({
id: "compute-shell-environment",
instantiate: (di): ComputeShellEnvironment => {
const isWindows = di.inject(isWindowsInjectable);
const computeUnixShellEnvironment = di.inject(computeUnixShellEnvironmentInjectable);
if (isWindows) {
return async () => ({
callWasSuccessful: true,
response: undefined,
});
}
return async (shell) => {
const controller = new AbortController();
const timeoutHandle = setTimeout(() => controller.abort(), 30_000);
const result = await computeUnixShellEnvironment(shell, { signal: controller.signal });
clearTimeout(timeoutHandle);
if (result.callWasSuccessful) {
return result;
}
if (controller.signal.aborted) {
return {
callWasSuccessful: false,
error: `Resolving shell environment is taking very long. Please review your shell configuration: ${result.error}`,
};
}
return result;
};
},
});
export default computeShellEnvironmentInjectable;