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

@ -55,3 +55,14 @@ export function getMinimumPackageAgeExclusions() {
export function getMalwareListBaseUrl() {
return process.env.SAFE_CHAIN_MALWARE_LIST_BASE_URL;
}
/**
* Gets the safe-chain base directory from environment variable.
* When set, all safe-chain data (bin, shims, scripts) will be placed under this directory
* instead of the default ~/.safe-chain, enabling system-wide installations.
* Example: "/usr/local/.safe-chain"
* @returns {string | undefined}
*/
export function getSafeChainDir() {
return process.env.SAFE_CHAIN_DIR;
}

View file

@ -0,0 +1,30 @@
import { describe, it, beforeEach, afterEach } from "node:test";
import assert from "node:assert";
const { getSafeChainDir } = await import("./environmentVariables.js");
describe("getSafeChainDir", () => {
let original;
beforeEach(() => {
original = process.env.SAFE_CHAIN_DIR;
});
afterEach(() => {
if (original !== undefined) {
process.env.SAFE_CHAIN_DIR = original;
} else {
delete process.env.SAFE_CHAIN_DIR;
}
});
it("returns undefined when SAFE_CHAIN_DIR is not set", () => {
delete process.env.SAFE_CHAIN_DIR;
assert.strictEqual(getSafeChainDir(), undefined);
});
it("returns the value of SAFE_CHAIN_DIR when set", () => {
process.env.SAFE_CHAIN_DIR = "/usr/local/.safe-chain";
assert.strictEqual(getSafeChainDir(), "/usr/local/.safe-chain");
});
});