Add env var support for home dir

This commit is contained in:
Reinier Criel 2026-04-10 08:57:08 -07:00
parent 698a12082d
commit a0fb8d6b3d
4 changed files with 125 additions and 3 deletions

View file

@ -3,6 +3,7 @@ import * as os from "os";
import fs from "fs";
import path from "path";
import { ECOSYSTEM_JS, ECOSYSTEM_PY } from "../config/settings.js";
import { getSafeChainDir } from "../config/environmentVariables.js";
import { safeSpawn } from "../utils/safeSpawn.js";
import { ui } from "../environment/userInteraction.js";
@ -121,18 +122,34 @@ export function getPackageManagerList() {
return `${tools.join(", ")}, and ${lastTool} commands`;
}
/**
* Returns the safe-chain base directory.
* Uses SAFE_CHAIN_DIR environment variable when set, otherwise defaults to ~/.safe-chain.
* @returns {string}
*/
export function getSafeChainBaseDir() {
return getSafeChainDir() ?? path.join(os.homedir(), ".safe-chain");
}
/**
* @returns {string}
*/
export function getBinDir() {
return path.join(getSafeChainBaseDir(), "bin");
}
/**
* @returns {string}
*/
export function getShimsDir() {
return path.join(os.homedir(), ".safe-chain", "shims");
return path.join(getSafeChainBaseDir(), "shims");
}
/**
* @returns {string}
*/
export function getScriptsDir() {
return path.join(os.homedir(), ".safe-chain", "scripts");
return path.join(getSafeChainBaseDir(), "scripts");
}
/**

View file

@ -1,6 +1,6 @@
import { describe, it, beforeEach, afterEach, mock } from "node:test";
import assert from "node:assert";
import { tmpdir } from "node:os";
import { tmpdir, homedir } from "node:os";
import fs from "node:fs";
import path from "path";
@ -15,6 +15,7 @@ describe("removeLinesMatchingPatternTests", () => {
mock.module("node:os", {
namedExports: {
EOL: "\r\n", // Simulate Windows line endings
homedir,
tmpdir: tmpdir,
platform: () => "linux",
},
@ -182,3 +183,66 @@ describe("removeLinesMatchingPatternTests", () => {
assert.strictEqual(resultLines.length, 5, "Should have exactly 5 lines");
});
});
describe("getSafeChainBaseDir / getBinDir / getShimsDir / getScriptsDir", () => {
const customDir = "/usr/local/.safe-chain";
let originalSafeChainDir;
beforeEach(() => {
originalSafeChainDir = process.env.SAFE_CHAIN_DIR;
delete process.env.SAFE_CHAIN_DIR;
});
afterEach(() => {
if (originalSafeChainDir !== undefined) {
process.env.SAFE_CHAIN_DIR = originalSafeChainDir;
} else {
delete process.env.SAFE_CHAIN_DIR;
}
});
it("defaults base dir to ~/.safe-chain when SAFE_CHAIN_DIR is not set", async () => {
const { getSafeChainBaseDir } = await import("./helpers.js");
assert.strictEqual(getSafeChainBaseDir(), path.join(homedir(), ".safe-chain"));
});
it("uses SAFE_CHAIN_DIR as base dir when set", async () => {
process.env.SAFE_CHAIN_DIR = customDir;
const { getSafeChainBaseDir } = await import("./helpers.js");
assert.strictEqual(getSafeChainBaseDir(), customDir);
});
it("getBinDir returns ~/.safe-chain/bin by default", async () => {
const { getBinDir } = await import("./helpers.js");
assert.strictEqual(getBinDir(), path.join(homedir(), ".safe-chain", "bin"));
});
it("getBinDir returns custom dir + /bin when SAFE_CHAIN_DIR is set", async () => {
process.env.SAFE_CHAIN_DIR = customDir;
const { getBinDir } = await import("./helpers.js");
assert.strictEqual(getBinDir(), `${customDir}/bin`);
});
it("getShimsDir returns ~/.safe-chain/shims by default", async () => {
const { getShimsDir } = await import("./helpers.js");
assert.strictEqual(getShimsDir(), path.join(homedir(), ".safe-chain", "shims"));
});
it("getShimsDir returns custom dir + /shims when SAFE_CHAIN_DIR is set", async () => {
process.env.SAFE_CHAIN_DIR = customDir;
const { getShimsDir } = await import("./helpers.js");
assert.strictEqual(getShimsDir(), `${customDir}/shims`);
});
it("getScriptsDir returns ~/.safe-chain/scripts by default", async () => {
const { getScriptsDir } = await import("./helpers.js");
assert.strictEqual(getScriptsDir(), path.join(homedir(), ".safe-chain", "scripts"));
});
it("getScriptsDir returns custom dir + /scripts when SAFE_CHAIN_DIR is set", async () => {
process.env.SAFE_CHAIN_DIR = customDir;
const { getScriptsDir } = await import("./helpers.js");
assert.strictEqual(getScriptsDir(), `${customDir}/scripts`);
});
});