Handle comments from the PR

This commit is contained in:
Sander Declerck 2025-07-18 14:23:51 +02:00
parent 6c269a1bb5
commit 36c195f5a9
No known key found for this signature in database
14 changed files with 166 additions and 181 deletions

View file

@ -42,5 +42,6 @@
"bugs": { "bugs": {
"url": "https://github.com/AikidoSec/safe-chain/issues" "url": "https://github.com/AikidoSec/safe-chain/issues"
}, },
"homepage": "https://github.com/AikidoSec/safe-chain#readme" "homepage": "https://github.com/AikidoSec/safe-chain#readme",
"packageManager": "npm@11.4.1+sha512.fcee43884166b6f9c5d04535fb95650e9708b6948a1f797eddf40e9778646778a518dfa32651b1c62ff36f4ac42becf177ca46ca27d53f24b539190c8d91802b"
} }

View file

@ -13,16 +13,12 @@ export const knownAikidoTools = [
]; ];
export function doesExecutableExistOnSystem(executableName) { export function doesExecutableExistOnSystem(executableName) {
try { if (os.platform() === "win32") {
if (os.platform() === "win32") { const result = spawnSync("where", [executableName], { stdio: "ignore" });
const result = spawnSync("where", [executableName], { stdio: "ignore" }); return result.status === 0;
return result.status === 0; } else {
} else { const result = spawnSync("which", [executableName], { stdio: "ignore" });
const result = spawnSync("which", [executableName], { stdio: "ignore" }); return result.status === 0;
return result.status === 0;
}
} catch {
return false;
} }
} }

View file

@ -51,6 +51,7 @@ export async function setup() {
function setupShell(shell) { function setupShell(shell) {
let success = false; let success = false;
try { try {
shell.teardown(knownAikidoTools); // First, tear down to prevent duplicate aliases
success = shell.setup(knownAikidoTools); success = shell.setup(knownAikidoTools);
} catch { } catch {
success = false; success = false;

View file

@ -3,15 +3,23 @@ import bash from "./supported-shells/bash.js";
import powershell from "./supported-shells/powershell.js"; import powershell from "./supported-shells/powershell.js";
import windowsPowershell from "./supported-shells/windowsPowershell.js"; import windowsPowershell from "./supported-shells/windowsPowershell.js";
import fish from "./supported-shells/fish.js"; import fish from "./supported-shells/fish.js";
import { ui } from "../environment/userInteraction.js";
export function detectShells() { export function detectShells() {
let possibleShells = [zsh, bash, powershell, windowsPowershell, fish]; let possibleShells = [zsh, bash, powershell, windowsPowershell, fish];
let availableShells = []; let availableShells = [];
for (const shell of possibleShells) { try {
if (shell.isInstalled()) { for (const shell of possibleShells) {
availableShells.push(shell); if (shell.isInstalled()) {
availableShells.push(shell);
}
} }
} catch (error) {
ui.writeError(
`We were not able to detect which shells are installed on your system. Please check your shell configuration. Error: ${error.message}`
);
return [];
} }
return availableShells; return availableShells;

View file

@ -13,19 +13,19 @@ function isInstalled() {
return doesExecutableExistOnSystem(executableName); return doesExecutableExistOnSystem(executableName);
} }
function teardown() { function teardown(tools) {
const startupFile = getStartupFile(); const startupFile = getStartupFile();
// Removes all aliases starting with "alias npm=", "alias npx=", or "alias yarn=" for (const { tool } of tools) {
// This will remove the safe-chain aliases for npm, npx, and yarn commands. // Remove any existing alias for the tool
removeLinesMatchingPattern(startupFile, /^alias\s+(npm|npx|yarn)=/); removeLinesMatchingPattern(startupFile, new RegExp(`^alias\\s+${tool}=`));
}
return true; return true;
} }
function setup(tools) { function setup(tools) {
const startupFile = getStartupFile(); const startupFile = getStartupFile();
teardown();
for (const { tool, aikidoCommand } of tools) { for (const { tool, aikidoCommand } of tools) {
addLineToFile( addLineToFile(

View file

@ -3,6 +3,7 @@ import assert from "node:assert";
import { tmpdir } from "node:os"; import { tmpdir } from "node:os";
import fs from "node:fs"; import fs from "node:fs";
import path from "path"; import path from "path";
import { knownAikidoTools } from "../helpers.js";
describe("Bash shell integration", () => { describe("Bash shell integration", () => {
let mockStartupFile; let mockStartupFile;
@ -69,7 +70,7 @@ describe("Bash shell integration", () => {
const tools = [ const tools = [
{ tool: "npm", aikidoCommand: "aikido-npm" }, { tool: "npm", aikidoCommand: "aikido-npm" },
{ tool: "npx", aikidoCommand: "aikido-npx" }, { tool: "npx", aikidoCommand: "aikido-npx" },
{ tool: "yarn", aikidoCommand: "aikido-yarn" } { tool: "yarn", aikidoCommand: "aikido-yarn" },
]; ];
const result = bash.setup(tools); const result = bash.setup(tools);
@ -87,22 +88,6 @@ describe("Bash shell integration", () => {
); );
}); });
it("should call teardown before setup", () => {
// Pre-populate file with existing aliases
fs.writeFileSync(
mockStartupFile,
'alias npm="old-npm"\nalias npx="old-npx"\n',
"utf-8"
);
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
bash.setup(tools);
const content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(!content.includes('alias npm="old-npm"'));
assert.ok(content.includes('alias npm="aikido-npm"'));
});
it("should handle empty tools array", () => { it("should handle empty tools array", () => {
const result = bash.setup([]); const result = bash.setup([]);
assert.strictEqual(result, true); assert.strictEqual(result, true);
@ -128,7 +113,7 @@ describe("Bash shell integration", () => {
fs.writeFileSync(mockStartupFile, initialContent, "utf-8"); fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
const result = bash.teardown(); const result = bash.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
@ -144,7 +129,7 @@ describe("Bash shell integration", () => {
fs.unlinkSync(mockStartupFile); fs.unlinkSync(mockStartupFile);
} }
const result = bash.teardown(); const result = bash.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
}); });
@ -157,7 +142,7 @@ describe("Bash shell integration", () => {
fs.writeFileSync(mockStartupFile, initialContent, "utf-8"); fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
const result = bash.teardown(); const result = bash.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
@ -183,7 +168,7 @@ describe("Bash shell integration", () => {
it("should handle complete setup and teardown cycle", () => { it("should handle complete setup and teardown cycle", () => {
const tools = [ const tools = [
{ tool: "npm", aikidoCommand: "aikido-npm" }, { tool: "npm", aikidoCommand: "aikido-npm" },
{ tool: "yarn", aikidoCommand: "aikido-yarn" } { tool: "yarn", aikidoCommand: "aikido-yarn" },
]; ];
// Setup // Setup
@ -193,7 +178,7 @@ describe("Bash shell integration", () => {
assert.ok(content.includes('alias yarn="aikido-yarn"')); assert.ok(content.includes('alias yarn="aikido-yarn"'));
// Teardown // Teardown
bash.teardown(); bash.teardown(tools);
content = fs.readFileSync(mockStartupFile, "utf-8"); content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(!content.includes("alias npm=")); assert.ok(!content.includes("alias npm="));
assert.ok(!content.includes("alias yarn=")); assert.ok(!content.includes("alias yarn="));
@ -203,6 +188,7 @@ describe("Bash shell integration", () => {
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }]; const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
bash.setup(tools); bash.setup(tools);
bash.teardown(tools);
bash.setup(tools); bash.setup(tools);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");

View file

@ -13,19 +13,22 @@ function isInstalled() {
return doesExecutableExistOnSystem(executableName); return doesExecutableExistOnSystem(executableName);
} }
function teardown() { function teardown(tools) {
const startupFile = getStartupFile(); const startupFile = getStartupFile();
// Removes all aliases starting with "alias npm=", "alias npx=", or "alias yarn=" for (const { tool } of tools) {
// This will remove the safe-chain aliases for npm, npx, and yarn commands. // Remove any existing alias for the tool
removeLinesMatchingPattern(startupFile, /^alias\s+(npm|npx|yarn)\s+/); removeLinesMatchingPattern(
startupFile,
new RegExp(`^alias\\s+${tool}\\s+`)
);
}
return true; return true;
} }
function setup(tools) { function setup(tools) {
const startupFile = getStartupFile(); const startupFile = getStartupFile();
teardown();
for (const { tool, aikidoCommand } of tools) { for (const { tool, aikidoCommand } of tools) {
addLineToFile( addLineToFile(

View file

@ -3,6 +3,7 @@ import assert from "node:assert";
import { tmpdir } from "node:os"; import { tmpdir } from "node:os";
import fs from "node:fs"; import fs from "node:fs";
import path from "path"; import path from "path";
import { knownAikidoTools } from "../helpers.js";
describe("Fish shell integration", () => { describe("Fish shell integration", () => {
let mockStartupFile; let mockStartupFile;
@ -26,10 +27,10 @@ describe("Fish shell integration", () => {
if (!fs.existsSync(filePath)) return; if (!fs.existsSync(filePath)) return;
const content = fs.readFileSync(filePath, "utf-8"); const content = fs.readFileSync(filePath, "utf-8");
const lines = content.split("\n"); const lines = content.split("\n");
const filteredLines = lines.filter(line => !pattern.test(line)); const filteredLines = lines.filter((line) => !pattern.test(line));
fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8"); fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8");
} },
} },
}); });
// Mock child_process execSync // Mock child_process execSync
@ -69,28 +70,22 @@ describe("Fish shell integration", () => {
const tools = [ const tools = [
{ tool: "npm", aikidoCommand: "aikido-npm" }, { tool: "npm", aikidoCommand: "aikido-npm" },
{ tool: "npx", aikidoCommand: "aikido-npx" }, { tool: "npx", aikidoCommand: "aikido-npx" },
{ tool: "yarn", aikidoCommand: "aikido-yarn" } { tool: "yarn", aikidoCommand: "aikido-yarn" },
]; ];
const result = fish.setup(tools); const result = fish.setup(tools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(content.includes('alias npm "aikido-npm" # Safe-chain alias for npm')); assert.ok(
assert.ok(content.includes('alias npx "aikido-npx" # Safe-chain alias for npx')); content.includes('alias npm "aikido-npm" # Safe-chain alias for npm')
assert.ok(content.includes('alias yarn "aikido-yarn" # Safe-chain alias for yarn')); );
}); assert.ok(
content.includes('alias npx "aikido-npx" # Safe-chain alias for npx')
it("should call teardown before setup", () => { );
// Pre-populate file with existing aliases assert.ok(
fs.writeFileSync(mockStartupFile, 'alias npm "old-npm"\nalias npx "old-npx"\n', "utf-8"); content.includes('alias yarn "aikido-yarn" # Safe-chain alias for yarn')
);
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
fish.setup(tools);
const content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(!content.includes('alias npm "old-npm"'));
assert.ok(content.includes('alias npm "aikido-npm"'));
}); });
it("should handle empty tools array", () => { it("should handle empty tools array", () => {
@ -113,12 +108,12 @@ describe("Fish shell integration", () => {
"alias npx 'aikido-npx'", "alias npx 'aikido-npx'",
"alias yarn 'aikido-yarn'", "alias yarn 'aikido-yarn'",
"alias ls 'ls --color=auto'", "alias ls 'ls --color=auto'",
"alias grep 'grep --color=auto'" "alias grep 'grep --color=auto'",
].join("\n"); ].join("\n");
fs.writeFileSync(mockStartupFile, initialContent, "utf-8"); fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
const result = fish.teardown(); const result = fish.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
@ -134,7 +129,7 @@ describe("Fish shell integration", () => {
fs.unlinkSync(mockStartupFile); fs.unlinkSync(mockStartupFile);
} }
const result = fish.teardown(); const result = fish.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
}); });
@ -142,12 +137,12 @@ describe("Fish shell integration", () => {
const initialContent = [ const initialContent = [
"#!/usr/bin/env fish", "#!/usr/bin/env fish",
"alias ls 'ls --color=auto'", "alias ls 'ls --color=auto'",
"set PATH $PATH ~/bin" "set PATH $PATH ~/bin",
].join("\n"); ].join("\n");
fs.writeFileSync(mockStartupFile, initialContent, "utf-8"); fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
const result = fish.teardown(); const result = fish.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
@ -173,7 +168,7 @@ describe("Fish shell integration", () => {
it("should handle complete setup and teardown cycle", () => { it("should handle complete setup and teardown cycle", () => {
const tools = [ const tools = [
{ tool: "npm", aikidoCommand: "aikido-npm" }, { tool: "npm", aikidoCommand: "aikido-npm" },
{ tool: "yarn", aikidoCommand: "aikido-yarn" } { tool: "yarn", aikidoCommand: "aikido-yarn" },
]; ];
// Setup // Setup
@ -183,7 +178,7 @@ describe("Fish shell integration", () => {
assert.ok(content.includes('alias yarn "aikido-yarn"')); assert.ok(content.includes('alias yarn "aikido-yarn"'));
// Teardown // Teardown
fish.teardown(); fish.teardown(tools);
content = fs.readFileSync(mockStartupFile, "utf-8"); content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(!content.includes("alias npm ")); assert.ok(!content.includes("alias npm "));
assert.ok(!content.includes("alias yarn ")); assert.ok(!content.includes("alias yarn "));
@ -193,6 +188,7 @@ describe("Fish shell integration", () => {
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }]; const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
fish.setup(tools); fish.setup(tools);
fish.teardown(tools);
fish.setup(tools); fish.setup(tools);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");

View file

@ -13,19 +13,22 @@ function isInstalled() {
return doesExecutableExistOnSystem(executableName); return doesExecutableExistOnSystem(executableName);
} }
function teardown() { function teardown(tools) {
const startupFile = getStartupFile(); const startupFile = getStartupFile();
// Removes all aliases starting with "Set-Alias npm=", "Set-Alias npx=", or "Set-Alias yarn=" for (const { tool } of tools) {
// This will remove the safe-chain aliases for npm, npx, and yarn commands. // Remove any existing alias for the tool
removeLinesMatchingPattern(startupFile, /^Set-Alias\s+(npm|npx|yarn)\s+/); removeLinesMatchingPattern(
startupFile,
new RegExp(`^Set-Alias\\s+${tool}\\s+`)
);
}
return true; return true;
} }
function setup(tools) { function setup(tools) {
const startupFile = getStartupFile(); const startupFile = getStartupFile();
teardown();
for (const { tool, aikidoCommand } of tools) { for (const { tool, aikidoCommand } of tools) {
addLineToFile( addLineToFile(

View file

@ -3,6 +3,7 @@ import assert from "node:assert";
import { tmpdir } from "node:os"; import { tmpdir } from "node:os";
import fs from "node:fs"; import fs from "node:fs";
import path from "path"; import path from "path";
import { knownAikidoTools } from "../helpers.js";
describe("PowerShell Core shell integration", () => { describe("PowerShell Core shell integration", () => {
let mockStartupFile; let mockStartupFile;
@ -10,7 +11,10 @@ describe("PowerShell Core shell integration", () => {
beforeEach(async () => { beforeEach(async () => {
// Create temporary startup file for testing // Create temporary startup file for testing
mockStartupFile = path.join(tmpdir(), `test-powershell-profile-${Date.now()}.ps1`); mockStartupFile = path.join(
tmpdir(),
`test-powershell-profile-${Date.now()}.ps1`
);
// Mock the helpers module // Mock the helpers module
mock.module("../helpers.js", { mock.module("../helpers.js", {
@ -26,10 +30,10 @@ describe("PowerShell Core shell integration", () => {
if (!fs.existsSync(filePath)) return; if (!fs.existsSync(filePath)) return;
const content = fs.readFileSync(filePath, "utf-8"); const content = fs.readFileSync(filePath, "utf-8");
const lines = content.split("\n"); const lines = content.split("\n");
const filteredLines = lines.filter(line => !pattern.test(line)); const filteredLines = lines.filter((line) => !pattern.test(line));
fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8"); fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8");
} },
} },
}); });
// Mock child_process execSync // Mock child_process execSync
@ -69,28 +73,24 @@ describe("PowerShell Core shell integration", () => {
const tools = [ const tools = [
{ tool: "npm", aikidoCommand: "aikido-npm" }, { tool: "npm", aikidoCommand: "aikido-npm" },
{ tool: "npx", aikidoCommand: "aikido-npx" }, { tool: "npx", aikidoCommand: "aikido-npx" },
{ tool: "yarn", aikidoCommand: "aikido-yarn" } { tool: "yarn", aikidoCommand: "aikido-yarn" },
]; ];
const result = powershell.setup(tools); const result = powershell.setup(tools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(content.includes('Set-Alias npm aikido-npm # Safe-chain alias for npm')); assert.ok(
assert.ok(content.includes('Set-Alias npx aikido-npx # Safe-chain alias for npx')); content.includes("Set-Alias npm aikido-npm # Safe-chain alias for npm")
assert.ok(content.includes('Set-Alias yarn aikido-yarn # Safe-chain alias for yarn')); );
}); assert.ok(
content.includes("Set-Alias npx aikido-npx # Safe-chain alias for npx")
it("should call teardown before setup", () => { );
// Pre-populate file with existing aliases assert.ok(
fs.writeFileSync(mockStartupFile, 'Set-Alias npm old-npm\nSet-Alias npx old-npx\n', "utf-8"); content.includes(
"Set-Alias yarn aikido-yarn # Safe-chain alias for yarn"
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }]; )
powershell.setup(tools); );
const content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(!content.includes('Set-Alias npm old-npm'));
assert.ok(content.includes('Set-Alias npm aikido-npm'));
}); });
it("should handle empty tools array", () => { it("should handle empty tools array", () => {
@ -113,12 +113,12 @@ describe("PowerShell Core shell integration", () => {
"Set-Alias npx aikido-npx", "Set-Alias npx aikido-npx",
"Set-Alias yarn aikido-yarn", "Set-Alias yarn aikido-yarn",
"Set-Alias ls Get-ChildItem", "Set-Alias ls Get-ChildItem",
"Set-Alias grep Select-String" "Set-Alias grep Select-String",
].join("\n"); ].join("\n");
fs.writeFileSync(mockStartupFile, initialContent, "utf-8"); fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
const result = powershell.teardown(); const result = powershell.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
@ -134,7 +134,7 @@ describe("PowerShell Core shell integration", () => {
fs.unlinkSync(mockStartupFile); fs.unlinkSync(mockStartupFile);
} }
const result = powershell.teardown(); const result = powershell.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
}); });
@ -142,12 +142,12 @@ describe("PowerShell Core shell integration", () => {
const initialContent = [ const initialContent = [
"# PowerShell profile", "# PowerShell profile",
"Set-Alias ls Get-ChildItem", "Set-Alias ls Get-ChildItem",
"$env:PATH += ';C:\\Tools'" "$env:PATH += ';C:\\Tools'",
].join("\n"); ].join("\n");
fs.writeFileSync(mockStartupFile, initialContent, "utf-8"); fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
const result = powershell.teardown(); const result = powershell.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
@ -173,17 +173,17 @@ describe("PowerShell Core shell integration", () => {
it("should handle complete setup and teardown cycle", () => { it("should handle complete setup and teardown cycle", () => {
const tools = [ const tools = [
{ tool: "npm", aikidoCommand: "aikido-npm" }, { tool: "npm", aikidoCommand: "aikido-npm" },
{ tool: "yarn", aikidoCommand: "aikido-yarn" } { tool: "yarn", aikidoCommand: "aikido-yarn" },
]; ];
// Setup // Setup
powershell.setup(tools); powershell.setup(tools);
let content = fs.readFileSync(mockStartupFile, "utf-8"); let content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(content.includes('Set-Alias npm aikido-npm')); assert.ok(content.includes("Set-Alias npm aikido-npm"));
assert.ok(content.includes('Set-Alias yarn aikido-yarn')); assert.ok(content.includes("Set-Alias yarn aikido-yarn"));
// Teardown // Teardown
powershell.teardown(); powershell.teardown(tools);
content = fs.readFileSync(mockStartupFile, "utf-8"); content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(!content.includes("Set-Alias npm ")); assert.ok(!content.includes("Set-Alias npm "));
assert.ok(!content.includes("Set-Alias yarn ")); assert.ok(!content.includes("Set-Alias yarn "));
@ -193,6 +193,7 @@ describe("PowerShell Core shell integration", () => {
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }]; const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
powershell.setup(tools); powershell.setup(tools);
powershell.teardown(tools);
powershell.setup(tools); powershell.setup(tools);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");

View file

@ -13,19 +13,22 @@ function isInstalled() {
return doesExecutableExistOnSystem(executableName); return doesExecutableExistOnSystem(executableName);
} }
function teardown() { function teardown(tools) {
const startupFile = getStartupFile(); const startupFile = getStartupFile();
// Removes all aliases starting with "Set-Alias npm=", "Set-Alias npx=", or "Set-Alias yarn=" for (const { tool } of tools) {
// This will remove the safe-chain aliases for npm, npx, and yarn commands. // Remove any existing alias for the tool
removeLinesMatchingPattern(startupFile, /^Set-Alias\s+(npm|npx|yarn)\s+/); removeLinesMatchingPattern(
startupFile,
new RegExp(`^Set-Alias\\s+${tool}\\s+`)
);
}
return true; return true;
} }
function setup(tools) { function setup(tools) {
const startupFile = getStartupFile(); const startupFile = getStartupFile();
teardown();
for (const { tool, aikidoCommand } of tools) { for (const { tool, aikidoCommand } of tools) {
addLineToFile( addLineToFile(

View file

@ -3,6 +3,7 @@ import assert from "node:assert";
import { tmpdir } from "node:os"; import { tmpdir } from "node:os";
import fs from "node:fs"; import fs from "node:fs";
import path from "path"; import path from "path";
import { knownAikidoTools } from "../helpers.js";
describe("Windows PowerShell shell integration", () => { describe("Windows PowerShell shell integration", () => {
let mockStartupFile; let mockStartupFile;
@ -10,7 +11,10 @@ describe("Windows PowerShell shell integration", () => {
beforeEach(async () => { beforeEach(async () => {
// Create temporary startup file for testing // Create temporary startup file for testing
mockStartupFile = path.join(tmpdir(), `test-windows-powershell-profile-${Date.now()}.ps1`); mockStartupFile = path.join(
tmpdir(),
`test-windows-powershell-profile-${Date.now()}.ps1`
);
// Mock the helpers module // Mock the helpers module
mock.module("../helpers.js", { mock.module("../helpers.js", {
@ -26,10 +30,10 @@ describe("Windows PowerShell shell integration", () => {
if (!fs.existsSync(filePath)) return; if (!fs.existsSync(filePath)) return;
const content = fs.readFileSync(filePath, "utf-8"); const content = fs.readFileSync(filePath, "utf-8");
const lines = content.split("\n"); const lines = content.split("\n");
const filteredLines = lines.filter(line => !pattern.test(line)); const filteredLines = lines.filter((line) => !pattern.test(line));
fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8"); fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8");
} },
} },
}); });
// Mock child_process execSync // Mock child_process execSync
@ -69,28 +73,24 @@ describe("Windows PowerShell shell integration", () => {
const tools = [ const tools = [
{ tool: "npm", aikidoCommand: "aikido-npm" }, { tool: "npm", aikidoCommand: "aikido-npm" },
{ tool: "npx", aikidoCommand: "aikido-npx" }, { tool: "npx", aikidoCommand: "aikido-npx" },
{ tool: "yarn", aikidoCommand: "aikido-yarn" } { tool: "yarn", aikidoCommand: "aikido-yarn" },
]; ];
const result = windowsPowershell.setup(tools); const result = windowsPowershell.setup(tools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(content.includes('Set-Alias npm aikido-npm # Safe-chain alias for npm')); assert.ok(
assert.ok(content.includes('Set-Alias npx aikido-npx # Safe-chain alias for npx')); content.includes("Set-Alias npm aikido-npm # Safe-chain alias for npm")
assert.ok(content.includes('Set-Alias yarn aikido-yarn # Safe-chain alias for yarn')); );
}); assert.ok(
content.includes("Set-Alias npx aikido-npx # Safe-chain alias for npx")
it("should call teardown before setup", () => { );
// Pre-populate file with existing aliases assert.ok(
fs.writeFileSync(mockStartupFile, 'Set-Alias npm old-npm\nSet-Alias npx old-npx\n', "utf-8"); content.includes(
"Set-Alias yarn aikido-yarn # Safe-chain alias for yarn"
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }]; )
windowsPowershell.setup(tools); );
const content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(!content.includes('Set-Alias npm old-npm'));
assert.ok(content.includes('Set-Alias npm aikido-npm'));
}); });
it("should handle empty tools array", () => { it("should handle empty tools array", () => {
@ -113,12 +113,12 @@ describe("Windows PowerShell shell integration", () => {
"Set-Alias npx aikido-npx", "Set-Alias npx aikido-npx",
"Set-Alias yarn aikido-yarn", "Set-Alias yarn aikido-yarn",
"Set-Alias ls Get-ChildItem", "Set-Alias ls Get-ChildItem",
"Set-Alias grep Select-String" "Set-Alias grep Select-String",
].join("\n"); ].join("\n");
fs.writeFileSync(mockStartupFile, initialContent, "utf-8"); fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
const result = windowsPowershell.teardown(); const result = windowsPowershell.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
@ -134,7 +134,7 @@ describe("Windows PowerShell shell integration", () => {
fs.unlinkSync(mockStartupFile); fs.unlinkSync(mockStartupFile);
} }
const result = windowsPowershell.teardown(); const result = windowsPowershell.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
}); });
@ -142,12 +142,12 @@ describe("Windows PowerShell shell integration", () => {
const initialContent = [ const initialContent = [
"# Windows PowerShell profile", "# Windows PowerShell profile",
"Set-Alias ls Get-ChildItem", "Set-Alias ls Get-ChildItem",
"$env:PATH += ';C:\\Tools'" "$env:PATH += ';C:\\Tools'",
].join("\n"); ].join("\n");
fs.writeFileSync(mockStartupFile, initialContent, "utf-8"); fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
const result = windowsPowershell.teardown(); const result = windowsPowershell.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
@ -173,17 +173,17 @@ describe("Windows PowerShell shell integration", () => {
it("should handle complete setup and teardown cycle", () => { it("should handle complete setup and teardown cycle", () => {
const tools = [ const tools = [
{ tool: "npm", aikidoCommand: "aikido-npm" }, { tool: "npm", aikidoCommand: "aikido-npm" },
{ tool: "yarn", aikidoCommand: "aikido-yarn" } { tool: "yarn", aikidoCommand: "aikido-yarn" },
]; ];
// Setup // Setup
windowsPowershell.setup(tools); windowsPowershell.setup(tools);
let content = fs.readFileSync(mockStartupFile, "utf-8"); let content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(content.includes('Set-Alias npm aikido-npm')); assert.ok(content.includes("Set-Alias npm aikido-npm"));
assert.ok(content.includes('Set-Alias yarn aikido-yarn')); assert.ok(content.includes("Set-Alias yarn aikido-yarn"));
// Teardown // Teardown
windowsPowershell.teardown(); windowsPowershell.teardown(tools);
content = fs.readFileSync(mockStartupFile, "utf-8"); content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(!content.includes("Set-Alias npm ")); assert.ok(!content.includes("Set-Alias npm "));
assert.ok(!content.includes("Set-Alias yarn ")); assert.ok(!content.includes("Set-Alias yarn "));
@ -193,6 +193,7 @@ describe("Windows PowerShell shell integration", () => {
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }]; const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
windowsPowershell.setup(tools); windowsPowershell.setup(tools);
windowsPowershell.teardown(tools);
windowsPowershell.setup(tools); windowsPowershell.setup(tools);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");

View file

@ -13,19 +13,19 @@ function isInstalled() {
return doesExecutableExistOnSystem(executableName); return doesExecutableExistOnSystem(executableName);
} }
function teardown() { function teardown(tools) {
const startupFile = getStartupFile(); const startupFile = getStartupFile();
// Removes all aliases starting with "alias npm=", "alias npx=", or "alias yarn=" for (const { tool } of tools) {
// This will remove the safe-chain aliases for npm, npx, and yarn commands. // Remove any existing alias for the tool
removeLinesMatchingPattern(startupFile, /^alias\s+(npm|npx|yarn)=/); removeLinesMatchingPattern(startupFile, new RegExp(`^alias\\s+${tool}=`));
}
return true; return true;
} }
function setup(tools) { function setup(tools) {
const startupFile = getStartupFile(); const startupFile = getStartupFile();
teardown();
for (const { tool, aikidoCommand } of tools) { for (const { tool, aikidoCommand } of tools) {
addLineToFile( addLineToFile(

View file

@ -3,6 +3,7 @@ import assert from "node:assert";
import { tmpdir } from "node:os"; import { tmpdir } from "node:os";
import fs from "node:fs"; import fs from "node:fs";
import path from "path"; import path from "path";
import { knownAikidoTools } from "../helpers.js";
describe("Zsh shell integration", () => { describe("Zsh shell integration", () => {
let mockStartupFile; let mockStartupFile;
@ -69,7 +70,7 @@ describe("Zsh shell integration", () => {
const tools = [ const tools = [
{ tool: "npm", aikidoCommand: "aikido-npm" }, { tool: "npm", aikidoCommand: "aikido-npm" },
{ tool: "npx", aikidoCommand: "aikido-npx" }, { tool: "npx", aikidoCommand: "aikido-npx" },
{ tool: "yarn", aikidoCommand: "aikido-yarn" } { tool: "yarn", aikidoCommand: "aikido-yarn" },
]; ];
const result = zsh.setup(tools); const result = zsh.setup(tools);
@ -87,22 +88,6 @@ describe("Zsh shell integration", () => {
); );
}); });
it("should call teardown before setup", () => {
// Pre-populate file with existing aliases
fs.writeFileSync(
mockStartupFile,
'alias npm="old-npm"\nalias npx="old-npx"\n',
"utf-8"
);
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
zsh.setup(tools);
const content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(!content.includes('alias npm="old-npm"'));
assert.ok(content.includes('alias npm="aikido-npm"'));
});
it("should handle empty tools array", () => { it("should handle empty tools array", () => {
const result = zsh.setup([]); const result = zsh.setup([]);
assert.strictEqual(result, true); assert.strictEqual(result, true);
@ -128,7 +113,7 @@ describe("Zsh shell integration", () => {
fs.writeFileSync(mockStartupFile, initialContent, "utf-8"); fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
const result = zsh.teardown(); const result = zsh.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
@ -144,7 +129,7 @@ describe("Zsh shell integration", () => {
fs.unlinkSync(mockStartupFile); fs.unlinkSync(mockStartupFile);
} }
const result = zsh.teardown(); const result = zsh.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
}); });
@ -157,7 +142,7 @@ describe("Zsh shell integration", () => {
fs.writeFileSync(mockStartupFile, initialContent, "utf-8"); fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
const result = zsh.teardown(); const result = zsh.teardown(knownAikidoTools);
assert.strictEqual(result, true); assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
@ -183,7 +168,7 @@ describe("Zsh shell integration", () => {
it("should handle complete setup and teardown cycle", () => { it("should handle complete setup and teardown cycle", () => {
const tools = [ const tools = [
{ tool: "npm", aikidoCommand: "aikido-npm" }, { tool: "npm", aikidoCommand: "aikido-npm" },
{ tool: "yarn", aikidoCommand: "aikido-yarn" } { tool: "yarn", aikidoCommand: "aikido-yarn" },
]; ];
// Setup // Setup
@ -193,7 +178,7 @@ describe("Zsh shell integration", () => {
assert.ok(content.includes('alias yarn="aikido-yarn"')); assert.ok(content.includes('alias yarn="aikido-yarn"'));
// Teardown // Teardown
zsh.teardown(); zsh.teardown(tools);
content = fs.readFileSync(mockStartupFile, "utf-8"); content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(!content.includes("alias npm=")); assert.ok(!content.includes("alias npm="));
assert.ok(!content.includes("alias yarn=")); assert.ok(!content.includes("alias yarn="));
@ -203,6 +188,7 @@ describe("Zsh shell integration", () => {
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }]; const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
zsh.setup(tools); zsh.setup(tools);
zsh.teardown(tools);
zsh.setup(tools); zsh.setup(tools);
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");