From 2eb43d048c12d4e74fee1803b2efc7b2163d6cac Mon Sep 17 00:00:00 2001 From: Vladimir Vagaytsev Date: Wed, 15 Nov 2023 18:00:35 +0100 Subject: [PATCH 1/4] chore: remove empty dangling comment --- core/src/mutagen.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/src/mutagen.ts b/core/src/mutagen.ts index 348add8873..6b193905a5 100644 --- a/core/src/mutagen.ts +++ b/core/src/mutagen.ts @@ -783,10 +783,6 @@ export interface SyncSession { excludedConflicts?: number } -/** - * - */ - /** * Returns mutagen data directory path based on the project dir. * From da2216df3c095539d1bb83e7e3deba238b7e9496 Mon Sep 17 00:00:00 2001 From: Vladimir Vagaytsev Date: Fri, 17 Nov 2023 11:21:58 +0100 Subject: [PATCH 2/4] chore: deduplicate interface --- core/src/plugins.ts | 6 +----- core/src/plugins/kubernetes/helm/helm-cli.ts | 5 +---- core/src/util/util.ts | 4 ++++ 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/core/src/plugins.ts b/core/src/plugins.ts index 93fb3c24d5..e90a438783 100644 --- a/core/src/plugins.ts +++ b/core/src/plugins.ts @@ -18,7 +18,7 @@ import { pluginSchema, pluginNodeModuleSchema } from "./plugin/plugin.js" import type { GenericProviderConfig } from "./config/provider.js" import { CircularDependenciesError, ConfigurationError, PluginError, RuntimeError } from "./exceptions.js" import { uniq, mapValues, fromPairs, flatten, keyBy, some, isString, sortBy } from "lodash-es" -import type { MaybeUndefined } from "./util/util.js" +import type { Dictionary, MaybeUndefined } from "./util/util.js" import { findByName, pushToKey, getNames, isNotNull } from "./util/util.js" import { dedent, deline, naturalList } from "./util/string.js" import { validateSchema } from "./config/validation.js" @@ -39,10 +39,6 @@ import type { import type { ObjectSchema } from "@hapi/joi" import { GardenSdkPlugin } from "./plugin/sdk.js" -interface Dictionary { - [index: string]: T -} - export async function loadAndResolvePlugins( log: Log, projectRoot: string, diff --git a/core/src/plugins/kubernetes/helm/helm-cli.ts b/core/src/plugins/kubernetes/helm/helm-cli.ts index d6a5e29439..ce5686b89c 100644 --- a/core/src/plugins/kubernetes/helm/helm-cli.ts +++ b/core/src/plugins/kubernetes/helm/helm-cli.ts @@ -10,13 +10,10 @@ import type { Log } from "../../../logger/log-entry.js" import type { KubernetesPluginContext } from "../config.js" import type { StringMap } from "../../../config/common.js" import type { PluginToolSpec } from "../../../plugin/tools.js" +import type { Dictionary } from "../../../util/util.js" import split2 from "split2" import { pickBy } from "lodash-es" -interface Dictionary { - [index: string]: T -} - export const HELM_VERSION = "3.12.0" export const helm3Spec: PluginToolSpec = { diff --git a/core/src/util/util.ts b/core/src/util/util.ts index a32b3b0cf8..898369aa98 100644 --- a/core/src/util/util.ts +++ b/core/src/util/util.ts @@ -85,6 +85,10 @@ export type Unpacked = T extends (infer U)[] export type ExcludesFalsy = (x: T | false | null | undefined) => x is T export type MakeOptional = Omit & Partial> +export interface Dictionary { + [index: string]: T +} + const MAX_BUFFER_SIZE = 1024 * 1024 // Used to control process-level operations during testing From f96163fe9855906bfe9fc49f4dd2827a9ea1d7e8 Mon Sep 17 00:00:00 2001 From: Vladimir Vagaytsev Date: Fri, 17 Nov 2023 11:52:21 +0100 Subject: [PATCH 3/4] chore: remove undocumented params from jsdocs --- core/src/util/util.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/src/util/util.ts b/core/src/util/util.ts index 898369aa98..cb75ce2dd7 100644 --- a/core/src/util/util.ts +++ b/core/src/util/util.ts @@ -770,9 +770,6 @@ export function isNotNull(v: T | null): v is T { * Find and return the index of the given `slice` within `array`. Returns -1 if the slice is not found. * * Adapted from https://stackoverflow.com/posts/29426078/revisions - * - * @param array - * @param slice */ export function findSlice(array: any[], slice: any[], fromIndex = 0) { let i = fromIndex From ebd6ca80e9bb89b42a9d26242a714fd6944071ea Mon Sep 17 00:00:00 2001 From: Vladimir Vagaytsev Date: Fri, 17 Nov 2023 12:02:40 +0100 Subject: [PATCH 4/4] chore: move platform and arch utils to own module --- core/src/commands/self-update.ts | 5 +- core/src/plugin/tools.ts | 2 +- core/src/util/arch-platform.ts | 58 +++++++++++++++++++ core/src/util/ext-tools.ts | 3 +- core/src/util/util.ts | 56 +----------------- core/test/unit/src/commands/tools.ts | 2 +- .../unit/src/commands/util/fetch-tools.ts | 2 +- 7 files changed, 68 insertions(+), 60 deletions(-) create mode 100644 core/src/util/arch-platform.ts diff --git a/core/src/commands/self-update.ts b/core/src/commands/self-update.ts index 8df629254c..f0c02a677f 100644 --- a/core/src/commands/self-update.ts +++ b/core/src/commands/self-update.ts @@ -14,8 +14,9 @@ import type { GlobalOptions, ParameterValues } from "../cli/params.js" import { BooleanParameter, ChoicesParameter, StringParameter } from "../cli/params.js" import { dedent } from "../util/string.js" import { basename, dirname, join, resolve } from "path" -import type { Architecture } from "../util/util.js" -import { getArchitecture, isDarwinARM, getPackageVersion, getPlatform } from "../util/util.js" +import type { Architecture } from "../util/arch-platform.js" +import { getArchitecture, isDarwinARM, getPlatform } from "../util/arch-platform.js" +import { getPackageVersion } from "../util/util.js" import { RuntimeError } from "../exceptions.js" import { makeTempDir } from "../util/fs.js" import { createReadStream, createWriteStream } from "fs" diff --git a/core/src/plugin/tools.ts b/core/src/plugin/tools.ts index 83f4c2d785..6ab0175cb6 100644 --- a/core/src/plugin/tools.ts +++ b/core/src/plugin/tools.ts @@ -8,7 +8,7 @@ import { createSchema, joi, joiIdentifier } from "../config/common.js" import { deline } from "../util/string.js" -import type { Architecture, Platform } from "../util/util.js" +import type { Architecture, Platform } from "../util/arch-platform.js" export interface ToolBuildSpec { platform: Platform diff --git a/core/src/util/arch-platform.ts b/core/src/util/arch-platform.ts new file mode 100644 index 0000000000..c8b2c777ce --- /dev/null +++ b/core/src/util/arch-platform.ts @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2018-2023 Garden Technologies, Inc. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +import { memoize } from "lodash-es" +import { execSync } from "child_process" + +const platformMap = { + win32: "windows" as const, +} +const archMap = { + x32: "386" as const, + x64: "amd64" as const, +} +export type Architecture = Exclude | (typeof archMap)[keyof typeof archMap] +export type Platform = + | Exclude + | (typeof platformMap)[keyof typeof platformMap] + +export function getPlatform(): Platform { + return platformMap[process.platform] || process.platform +} + +export function getArchitecture(): Architecture { + // Note: When node is running a x64 build, + // process.arch is always x64 even though the underlying CPU architecture may be arm64 + // To check if we are running under Rosetta, + // use the `isDarwinARM` function below + const arch = process.arch + return archMap[arch] || arch +} + +export const isDarwinARM = memoize(() => { + if (process.platform !== "darwin") { + return false + } + + if (process.arch === "arm64") { + return true + } else if (process.arch === "x64") { + // detect rosetta on Apple M cpu family macs + // see also https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment + // We use execSync here, because this function is called in a constructor + // otherwise we'd make the function async and call `spawn` + try { + execSync("sysctl -n -q sysctl.proc_translated", { encoding: "utf-8" }) + } catch (err) { + return false + } + return true + } + + return false +}) diff --git a/core/src/util/ext-tools.ts b/core/src/util/ext-tools.ts index 4442ea8581..6e2962c84c 100644 --- a/core/src/util/ext-tools.ts +++ b/core/src/util/ext-tools.ts @@ -10,7 +10,8 @@ import fsExtra from "fs-extra" const { pathExists, createWriteStream, ensureDir, chmod, remove, move, createReadStream } = fsExtra import { ConfigurationError, InternalError } from "../exceptions.js" import { join, dirname, basename, posix } from "path" -import { hashString, exec, getPlatform, getArchitecture, isDarwinARM } from "./util.js" +import { getArchitecture, getPlatform, isDarwinARM } from "./arch-platform.js" +import { hashString, exec } from "./util.js" import tar from "tar" import { GARDEN_GLOBAL_PATH } from "../constants.js" import type { Log } from "../logger/log-entry.js" diff --git a/core/src/util/util.ts b/core/src/util/util.ts index cb75ce2dd7..2633a80bfc 100644 --- a/core/src/util/util.ts +++ b/core/src/util/util.ts @@ -7,7 +7,6 @@ */ import { asyncExitHook, gracefulExit } from "@scg82/exit-hook" -import { execSync } from "child_process" import _spawn from "cross-spawn" import { createHash } from "node:crypto" import fsExtra from "fs-extra" @@ -21,7 +20,6 @@ import { isArray, isPlainObject, mapValues, - memoize, omit, pick, range, @@ -39,10 +37,10 @@ import { DEFAULT_GARDEN_CLOUD_DOMAIN, DOCS_BASE_URL, gardenEnv } from "../consta import { ChildProcessError, InternalError, + isErrnoException, ParameterError, RuntimeError, TimeoutError, - isErrnoException, } from "../exceptions.js" import type { Log } from "../logger/log-entry.js" import { getDefaultProfiler } from "./profiling.js" @@ -195,6 +193,7 @@ export function createOutputStream(log: Log, origin?: string) { return outputStream } + export interface ExecOpts extends ExecaOptions { stdout?: Writable stderr?: Writable @@ -598,57 +597,6 @@ export function isSubdir(path: string, ofPath: string): boolean { return !!(rel && !rel.startsWith("..") && !isAbsolute(rel)) } -// Used to make the platforms more consistent with other tools -const platformMap = { - win32: "windows" as const, -} - -const archMap = { - x32: "386" as const, - x64: "amd64" as const, -} - -export type Architecture = Exclude | (typeof archMap)[keyof typeof archMap] -export type Platform = - | Exclude - | (typeof platformMap)[keyof typeof platformMap] - -export function getPlatform(): Platform { - return platformMap[process.platform] || process.platform -} - -export function getArchitecture(): Architecture { - // Note: When node is running a x64 build, - // process.arch is always x64 even though the underlying CPU architecture may be arm64 - // To check if we are running under Rosetta, - // use the `isDarwinARM` function below - const arch = process.arch - return archMap[arch] || arch -} - -export const isDarwinARM = memoize(() => { - if (process.platform !== "darwin") { - return false - } - - if (process.arch === "arm64") { - return true - } else if (process.arch === "x64") { - // detect rosetta on Apple M cpu family macs - // see also https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment - // We use execSync here, because this function is called in a constructor - // otherwise we'd make the function async and call `spawn` - try { - execSync("sysctl -n -q sysctl.proc_translated", { encoding: "utf-8" }) - } catch (err) { - return false - } - return true - } - - return false -}) - export function getDurationMsec(start: Date, end: Date): number { return Math.round(end.getTime() - start.getTime()) } diff --git a/core/test/unit/src/commands/tools.ts b/core/test/unit/src/commands/tools.ts index cd530eb204..87f681bfd2 100644 --- a/core/test/unit/src/commands/tools.ts +++ b/core/test/unit/src/commands/tools.ts @@ -6,7 +6,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -import { getPlatform, getArchitecture } from "../../../../src/util/util.js" +import { getArchitecture, getPlatform } from "../../../../src/util/arch-platform.js" import type { TempDirectory } from "../../../helpers.js" import { makeTempDir, diff --git a/core/test/unit/src/commands/util/fetch-tools.ts b/core/test/unit/src/commands/util/fetch-tools.ts index 2b4bedd9fb..0d673eb582 100644 --- a/core/test/unit/src/commands/util/fetch-tools.ts +++ b/core/test/unit/src/commands/util/fetch-tools.ts @@ -6,7 +6,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -import { getPlatform, getArchitecture } from "../../../../../src/util/util.js" +import { getArchitecture, getPlatform } from "../../../../../src/util/arch-platform.js" import type { TempDirectory } from "../../../../helpers.js" import { createProjectConfig,