From 1410ebe4cadccd1ad711ddeea973384a5b07c63b Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Sun, 30 Jan 2022 17:23:09 -0800 Subject: [PATCH] chore: move execaSudo --- src/kcov/kcov.ts | 4 ++-- src/utils/env/sudo.ts | 9 --------- src/utils/exec/sudo.ts | 10 ++++++++++ src/utils/setup/setupAptPack.ts | 16 ++++++++-------- 4 files changed, 20 insertions(+), 19 deletions(-) create mode 100644 src/utils/exec/sudo.ts diff --git a/src/kcov/kcov.ts b/src/kcov/kcov.ts index 560bc109..af359ebe 100644 --- a/src/kcov/kcov.ts +++ b/src/kcov/kcov.ts @@ -2,7 +2,7 @@ import execa from "execa" // import { join } from "path" // import { untildify_user as untildify } from "./utils/path/untildify" // import { setupCmake } from "../cmake/cmake" -import { execaSudo } from "../utils/env/sudo" +import { execSudo } from "../utils/exec/sudo" import { addBinExtension } from "../utils/extension/extension" import { extractTarByExe } from "../utils/setup/extract" import { setupAptPack } from "../utils/setup/setupAptPack" @@ -38,7 +38,7 @@ function getKcovPackageInfo(version: string): PackageInfo { await setupAptPack("libcurl4-openssl-dev") await execa("cmake", ["-S", "./", "-B", "./build"], { cwd: out }) await execa("cmake", ["--build", "./build", "--config", "Release"], { cwd: out }) - await execaSudo("cmake", ["--install", "./build"], out) + await execSudo("cmake", ["--install", "./build"], out) return out }, } diff --git a/src/utils/env/sudo.ts b/src/utils/env/sudo.ts index c50f638c..64593997 100644 --- a/src/utils/env/sudo.ts +++ b/src/utils/env/sudo.ts @@ -1,4 +1,3 @@ -import execa from "execa" import which from "which" let _issudo: boolean | undefined = undefined @@ -18,11 +17,3 @@ export function mightSudo(command: string) { } return command } - -export function execaSudo(file: string, args: string[], cwd?: string) { - if (isRoot()) { - return execa.command(`sudo ${[file, ...args].map((arg) => `'${arg}'`).join(" ")}`, { shell: true, cwd }) - } else { - return execa(file, args) - } -} diff --git a/src/utils/exec/sudo.ts b/src/utils/exec/sudo.ts new file mode 100644 index 00000000..f4f421c8 --- /dev/null +++ b/src/utils/exec/sudo.ts @@ -0,0 +1,10 @@ +import execa from "execa" +import { isRoot } from "../env/sudo" + +export function execSudo(file: string, args: string[], cwd?: string) { + if (isRoot()) { + return execa.command(`sudo ${[file, ...args].map((arg) => `'${arg}'`).join(" ")}`, { shell: true, cwd }) + } else { + return execa(file, args) + } +} diff --git a/src/utils/setup/setupAptPack.ts b/src/utils/setup/setupAptPack.ts index 3f85adcd..9121b2d9 100644 --- a/src/utils/setup/setupAptPack.ts +++ b/src/utils/setup/setupAptPack.ts @@ -1,6 +1,6 @@ /* eslint-disable require-atomic-updates */ import { InstallationInfo } from "./setupBin" -import { execaSudo } from "../env/sudo" +import { execSudo } from "../exec/sudo" let didUpdate: boolean = false let didInit: boolean = false @@ -16,7 +16,7 @@ export async function setupAptPack( process.env.DEBIAN_FRONTEND = "noninteractive" if (!didUpdate) { - await execaSudo(apt, ["update", "-y"]) + await execSudo(apt, ["update", "-y"]) didUpdate = true } @@ -25,7 +25,7 @@ export async function setupAptPack( // set time - zone // TZ = Canada / Pacific // ln - snf / usr / share / zoneinfo / $TZ / etc / localtime && echo $TZ > /etc/timezone - await execaSudo(apt, [ + await execSudo(apt, [ "install", "--fix-broken", "-y", @@ -40,19 +40,19 @@ export async function setupAptPack( if (Array.isArray(repositories)) { for (const repo of repositories) { // eslint-disable-next-line no-await-in-loop - await execaSudo("add-apt-repository", ["--update", "-y", repo]) + await execSudo("add-apt-repository", ["--update", "-y", repo]) } - await execaSudo(apt, ["update", "-y"]) + await execSudo(apt, ["update", "-y"]) } if (version !== undefined && version !== "") { try { - await execaSudo(apt, ["install", "--fix-broken", "-y", `${name}=${version}`]) + await execSudo(apt, ["install", "--fix-broken", "-y", `${name}=${version}`]) } catch { - await execaSudo(apt, ["install", "--fix-broken", "-y", `${name}-${version}`]) + await execSudo(apt, ["install", "--fix-broken", "-y", `${name}-${version}`]) } } else { - await execaSudo(apt, ["install", "--fix-broken", "-y", name]) + await execSudo(apt, ["install", "--fix-broken", "-y", name]) } return { binDir: "/usr/bin/" }