Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: restructuring util/util.ts (part 1) #5431

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core/src/commands/self-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 0 additions & 4 deletions core/src/mutagen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,10 +783,6 @@ export interface SyncSession {
excludedConflicts?: number
}

/**
*
*/

/**
* Returns mutagen data directory path based on the project dir.
*
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugin/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions core/src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -39,10 +39,6 @@ import type {
import type { ObjectSchema } from "@hapi/joi"
import { GardenSdkPlugin } from "./plugin/sdk.js"

interface Dictionary<T> {
[index: string]: T
}

export async function loadAndResolvePlugins(
log: Log,
projectRoot: string,
Expand Down
5 changes: 1 addition & 4 deletions core/src/plugins/kubernetes/helm/helm-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
[index: string]: T
}

export const HELM_VERSION = "3.12.0"

export const helm3Spec: PluginToolSpec = {
Expand Down
58 changes: 58 additions & 0 deletions core/src/util/arch-platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2018-2023 Garden Technologies, Inc. <[email protected]>
*
* 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<NodeJS.Architecture, keyof typeof archMap> | (typeof archMap)[keyof typeof archMap]
export type Platform =
| Exclude<NodeJS.Platform, keyof typeof platformMap>
| (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
})
3 changes: 2 additions & 1 deletion core/src/util/ext-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
63 changes: 6 additions & 57 deletions core/src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -21,7 +20,6 @@ import {
isArray,
isPlainObject,
mapValues,
memoize,
omit,
pick,
range,
Expand All @@ -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"
Expand Down Expand Up @@ -85,6 +83,10 @@ export type Unpacked<T> = T extends (infer U)[]
export type ExcludesFalsy = <T>(x: T | false | null | undefined) => x is T
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>

export interface Dictionary<T> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a refactoring so definitely not a blocker, but I guess we could replace that with Record<string, T>?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Maybe, this type was introduced as a short-handed type for string-keyed Record. In that case, we can just re-declare it as type Dictionary<T> = Record<string, T>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be addressed in the next PR, there will be a series of PRs to refactor util/util.ts :)

[index: string]: T
}

const MAX_BUFFER_SIZE = 1024 * 1024

// Used to control process-level operations during testing
Expand Down Expand Up @@ -191,6 +193,7 @@ export function createOutputStream(log: Log, origin?: string) {

return outputStream
}

export interface ExecOpts extends ExecaOptions {
stdout?: Writable
stderr?: Writable
Expand Down Expand Up @@ -594,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<NodeJS.Architecture, keyof typeof archMap> | (typeof archMap)[keyof typeof archMap]
export type Platform =
| Exclude<NodeJS.Platform, keyof typeof platformMap>
| (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())
}
Expand Down Expand Up @@ -766,9 +718,6 @@ export function isNotNull<T>(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
Expand Down
2 changes: 1 addition & 1 deletion core/test/unit/src/commands/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion core/test/unit/src/commands/util/fetch-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down