Skip to content

Commit

Permalink
fix(helm): Use graceful-fs
Browse files Browse the repository at this point in the history
Fix EPERM errors when renaming files on Windows
  • Loading branch information
tommy351 committed Jun 2, 2024
1 parent 4d41189 commit 0291c03
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
2 changes: 2 additions & 0 deletions packages/helm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@
"@kosko/exec-utils": "workspace:^",
"cachedir": "^2.4.0",
"fast-safe-stringify": "^2.1.1",
"graceful-fs": "^4.2.11",
"js-yaml": "^4.1.0",
"tmp-promise": "^3.0.3"
},
"devDependencies": {
"@kosko/build-scripts": "workspace:^",
"@kosko/jest-preset": "workspace:^",
"@kosko/yaml": "workspace:^",
"@types/graceful-fs": "^4.1.9",
"@types/js-yaml": "^4.0.5",
"kubernetes-models": "^4.1.0"
},
Expand Down
16 changes: 8 additions & 8 deletions packages/helm/src/load.ts
Original file line number Diff line number Diff line change
@@ -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 fs from "graceful-fs";
import {
spawn,
booleanArg,
Expand Down Expand Up @@ -243,7 +243,7 @@ async function runHelm(args: readonly string[]) {
async function fileExists(path: string): Promise<boolean> {
try {
// Check if `Chart.yaml` exists
const stats = await stat(path);
const stats = await fs.promises.stat(path);
return stats.isFile();
} catch (err) {
if (getErrorCode(err) !== "ENOENT") throw err;
Expand Down Expand Up @@ -271,7 +271,7 @@ function getChartBaseName(chart: string): string {
async function getChartMetadata(
chart: string
): Promise<Record<string, unknown>> {
const content = await readFile(join(chart, "Chart.yaml"), "utf8");
const content = await fs.promises.readFile(join(chart, "Chart.yaml"), "utf8");
const metadata = yaml.load(content);

if (isRecord(metadata)) return metadata;
Expand Down Expand Up @@ -317,7 +317,7 @@ async function pullChart(

// Read the lock file to get the cache path
try {
const lockContent = await readFile(lockPath, "utf8");
const lockContent = await fs.promises.readFile(lockPath, "utf8");
return { chart: join(cacheDir, lockContent) };
} catch (err) {
if (getErrorCode(err) !== "ENOENT") throw err;
Expand Down Expand Up @@ -353,11 +353,11 @@ async function pullChart(
});
const dest = join(cacheDir, chartHash);

await mkdir(cacheDir, { recursive: true });
await fs.promises.mkdir(cacheDir, { recursive: true });

// Move the chart to the cache directory
try {
await rename(chartDir, dest);
await fs.promises.rename(chartDir, dest);
} catch (err) {
// If the cache directory already exists, it probably means that another
// process has already pulled the chart. In this case, we can ignore the
Expand All @@ -372,7 +372,7 @@ async function pullChart(
}

// Write lock file
await writeFile(lockPath, chartHash);
await fs.promises.writeFile(lockPath, chartHash);

return { chart: dest };
} finally {
Expand All @@ -384,7 +384,7 @@ async function pullChart(
async function writeValues(values: unknown) {
const file = await tmp.file();

await writeFile(file.path, stringify(values));
await fs.promises.writeFile(file.path, stringify(values));

return file;
}
Expand Down
56 changes: 31 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0291c03

Please sign in to comment.