From 0ba16a81de3e7e32a06124b31fc4db311a677f61 Mon Sep 17 00:00:00 2001 From: Janne Savolainen Date: Mon, 14 Feb 2022 16:04:58 +0200 Subject: [PATCH] Introduce way for execute file Signed-off-by: Janne Savolainen --- src/common/fs/exec-file.injectable.ts | 23 +++++++++++++++++++++++ src/main/getDiForUnitTesting.ts | 6 ++++++ 2 files changed, 29 insertions(+) create mode 100644 src/common/fs/exec-file.injectable.ts diff --git a/src/common/fs/exec-file.injectable.ts b/src/common/fs/exec-file.injectable.ts new file mode 100644 index 0000000000..8e8f04cffb --- /dev/null +++ b/src/common/fs/exec-file.injectable.ts @@ -0,0 +1,23 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import { execFile } from "child_process"; + +export type ExecFile = (filePath: string, args: string[]) => Promise; + +const execFileInjectable = getInjectable({ + id: "exec-file", + + instantiate: (): ExecFile => async (filePath, args) => + new Promise((resolve) => { + execFile(filePath, args, (error, stdout) => { + resolve(stdout); + }); + }), + + causesSideEffects: true, +}); + +export default execFileInjectable; diff --git a/src/main/getDiForUnitTesting.ts b/src/main/getDiForUnitTesting.ts index 2cc78ec528..f9c781bef3 100644 --- a/src/main/getDiForUnitTesting.ts +++ b/src/main/getDiForUnitTesting.ts @@ -84,6 +84,7 @@ import startKubeConfigSyncInjectable from "./start-main-application/runnables/ku import appVersionInjectable from "../common/get-configuration-file-model/app-version/app-version.injectable"; import getRandomIdInjectable from "../common/utils/get-random-id.injectable"; import periodicalCheckForUpdatesInjectable from "./application-update/periodical-check-for-updates/periodical-check-for-updates.injectable"; +import execFileInjectable from "../common/fs/exec-file.injectable"; export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {}) { const { @@ -162,6 +163,11 @@ export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {}) throw new Error("Tried to read file from file system without specifying explicit override."); }); + di.override(execFileInjectable, () => () => { + throw new Error("Tried to exec file from file system without specifying explicit override."); + }); + + di.override(loggerInjectable, () => ({ warn: noop, debug: noop,