diff --git a/packages/helm/src/fs.ts b/packages/helm/src/fs.ts new file mode 100644 index 000000000..d09c41d3b --- /dev/null +++ b/packages/helm/src/fs.ts @@ -0,0 +1,27 @@ +import { getErrorCode } from "@kosko/common-utils"; +import { cp, rename, rm, stat } from "node:fs/promises"; + +export async function move(src: string, dest: string): Promise { + try { + await rename(src, dest); + } catch (err) { + if (getErrorCode(err) !== "EXDEV") throw err; + + await cp(src, dest, { + recursive: true, + errorOnExist: true, + preserveTimestamps: true + }); + await rm(src, { recursive: true, force: true }); + } +} + +export async function fileExists(path: string): Promise { + try { + const stats = await stat(path); + return stats.isFile(); + } catch (err) { + if (getErrorCode(err) !== "ENOENT") throw err; + return false; + } +} diff --git a/packages/helm/src/load.ts b/packages/helm/src/load.ts index 6d08ef16f..7bea178dd 100644 --- a/packages/helm/src/load.ts +++ b/packages/helm/src/load.ts @@ -1,6 +1,6 @@ import { LoadOptions, loadString, Manifest } from "@kosko/yaml"; import tmp from "tmp-promise"; -import { writeFile, stat, rename, readFile, mkdir } from "node:fs/promises"; +import { writeFile, readFile, mkdir } from "node:fs/promises"; import { spawn, booleanArg, @@ -14,6 +14,7 @@ import { createHash } from "node:crypto"; import { dirname, join } from "node:path"; import { env } from "node:process"; import yaml from "js-yaml"; +import { fileExists, move } from "./fs"; const FILE_EXIST_ERROR_CODES = new Set(["EEXIST", "ENOTEMPTY"]); const OCI_PREFIX = "oci://"; @@ -253,17 +254,6 @@ function chartManifestExists(path: string) { return fileExists(getChartManifestPath(path)); } -async function fileExists(path: string): Promise { - try { - // Check if `Chart.yaml` exists - const stats = await stat(path); - return stats.isFile(); - } catch (err) { - if (getErrorCode(err) !== "ENOENT") throw err; - return false; - } -} - async function isLocalChart( options: Pick ): Promise { @@ -320,7 +310,7 @@ async function moveChartToCacheDir(src: string, dest: string): Promise { await mkdir(dirname(dest), { recursive: true }); try { - await rename(src, dest); + await move(src, dest); } catch (err) { const code = getErrorCode(err);