From f793bb84670de534362ebf358c9d08edca2f9727 Mon Sep 17 00:00:00 2001 From: Sander Declerck Date: Wed, 17 Sep 2025 15:26:06 +0200 Subject: [PATCH] Check if directory exists before creating a new shell startup file --- .../src/shell-integration/helpers.js | 29 +++++++++++++++---- .../safe-chain/src/shell-integration/setup.js | 11 ++++++- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/packages/safe-chain/src/shell-integration/helpers.js b/packages/safe-chain/src/shell-integration/helpers.js index 7efface..9785e0a 100644 --- a/packages/safe-chain/src/shell-integration/helpers.js +++ b/packages/safe-chain/src/shell-integration/helpers.js @@ -1,6 +1,7 @@ import { spawnSync } from "child_process"; import * as os from "os"; import fs from "fs"; +import path from "path"; export const knownAikidoTools = [ { tool: "npm", aikidoCommand: "aikido-npm" }, @@ -43,12 +44,17 @@ function shouldRemoveLine(line, pattern) { if (line.length > maxLineLength) { // safe-chain only adds lines shorter than maxLineLength - // so if the line is longer, it must be from a different + // so if the line is longer, it must be from a different // source and could be dangerous to remove return false; } - if (line.includes("\n") || line.includes("\r") || line.includes("\u2028") || line.includes("\u2029")) { + if ( + line.includes("\n") || + line.includes("\r") || + line.includes("\u2028") || + line.includes("\u2029") + ) { // If the line contains newlines, something has gone wrong in splitting // \u2028 and \u2029 are Unicode line separator characters (line and paragraph separators) return false; @@ -58,11 +64,22 @@ function shouldRemoveLine(line, pattern) { } export function addLineToFile(filePath, line) { - if (!fs.existsSync(filePath)) { - fs.writeFileSync(filePath, "", "utf-8"); - } + createFileIfNotExists(filePath); const fileContent = fs.readFileSync(filePath, "utf-8"); - const updatedContent = fileContent + os.EOL + line; + const updatedContent = fileContent + os.EOL + line + os.EOL; fs.writeFileSync(filePath, updatedContent, "utf-8"); } + +function createFileIfNotExists(filePath) { + if (fs.existsSync(filePath)) { + return; + } + + const dir = path.dirname(filePath); + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + } + + fs.writeFileSync(filePath, "", "utf-8"); +} diff --git a/packages/safe-chain/src/shell-integration/setup.js b/packages/safe-chain/src/shell-integration/setup.js index 792aa3c..b58eb51 100644 --- a/packages/safe-chain/src/shell-integration/setup.js +++ b/packages/safe-chain/src/shell-integration/setup.js @@ -56,11 +56,13 @@ export async function setup() { */ function setupShell(shell) { let success = false; + let error; try { shell.teardown(knownAikidoTools); // First, tear down to prevent duplicate aliases success = shell.setup(knownAikidoTools); - } catch { + } catch (err) { success = false; + error = err; } if (success) { @@ -75,6 +77,13 @@ function setupShell(shell) { "Setup failed" )}. Please check your ${shell.name} configuration.` ); + if (error) { + let message = ` Error: ${error.message}`; + if (error.code) { + message += ` (code: ${error.code})`; + } + ui.writeError(message); + } } return success;