mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Add env var support for home dir
This commit is contained in:
parent
698a12082d
commit
a0fb8d6b3d
4 changed files with 125 additions and 3 deletions
|
|
@ -55,3 +55,14 @@ export function getMinimumPackageAgeExclusions() {
|
||||||
export function getMalwareListBaseUrl() {
|
export function getMalwareListBaseUrl() {
|
||||||
return process.env.SAFE_CHAIN_MALWARE_LIST_BASE_URL;
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
30
packages/safe-chain/src/config/environmentVariables.spec.js
Normal file
30
packages/safe-chain/src/config/environmentVariables.spec.js
Normal 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");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -3,6 +3,7 @@ import * as os from "os";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { ECOSYSTEM_JS, ECOSYSTEM_PY } from "../config/settings.js";
|
import { ECOSYSTEM_JS, ECOSYSTEM_PY } from "../config/settings.js";
|
||||||
|
import { getSafeChainDir } from "../config/environmentVariables.js";
|
||||||
import { safeSpawn } from "../utils/safeSpawn.js";
|
import { safeSpawn } from "../utils/safeSpawn.js";
|
||||||
import { ui } from "../environment/userInteraction.js";
|
import { ui } from "../environment/userInteraction.js";
|
||||||
|
|
||||||
|
|
@ -121,18 +122,34 @@ export function getPackageManagerList() {
|
||||||
return `${tools.join(", ")}, and ${lastTool} commands`;
|
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}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
export function getShimsDir() {
|
export function getShimsDir() {
|
||||||
return path.join(os.homedir(), ".safe-chain", "shims");
|
return path.join(getSafeChainBaseDir(), "shims");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
export function getScriptsDir() {
|
export function getScriptsDir() {
|
||||||
return path.join(os.homedir(), ".safe-chain", "scripts");
|
return path.join(getSafeChainBaseDir(), "scripts");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { describe, it, beforeEach, afterEach, mock } from "node:test";
|
import { describe, it, beforeEach, afterEach, mock } from "node:test";
|
||||||
import assert from "node:assert";
|
import assert from "node:assert";
|
||||||
import { tmpdir } from "node:os";
|
import { tmpdir, homedir } from "node:os";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
|
||||||
|
|
@ -15,6 +15,7 @@ describe("removeLinesMatchingPatternTests", () => {
|
||||||
mock.module("node:os", {
|
mock.module("node:os", {
|
||||||
namedExports: {
|
namedExports: {
|
||||||
EOL: "\r\n", // Simulate Windows line endings
|
EOL: "\r\n", // Simulate Windows line endings
|
||||||
|
homedir,
|
||||||
tmpdir: tmpdir,
|
tmpdir: tmpdir,
|
||||||
platform: () => "linux",
|
platform: () => "linux",
|
||||||
},
|
},
|
||||||
|
|
@ -182,3 +183,66 @@ describe("removeLinesMatchingPatternTests", () => {
|
||||||
assert.strictEqual(resultLines.length, 5, "Should have exactly 5 lines");
|
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`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue