mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Check if directory exists before creating a new shell startup file
This commit is contained in:
parent
846c62e4e0
commit
f793bb8467
2 changed files with 33 additions and 7 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
import { spawnSync } from "child_process";
|
import { spawnSync } from "child_process";
|
||||||
import * as os from "os";
|
import * as os from "os";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
export const knownAikidoTools = [
|
export const knownAikidoTools = [
|
||||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||||
|
|
@ -48,7 +49,12 @@ function shouldRemoveLine(line, pattern) {
|
||||||
return false;
|
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
|
// If the line contains newlines, something has gone wrong in splitting
|
||||||
// \u2028 and \u2029 are Unicode line separator characters (line and paragraph separators)
|
// \u2028 and \u2029 are Unicode line separator characters (line and paragraph separators)
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -58,11 +64,22 @@ function shouldRemoveLine(line, pattern) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function addLineToFile(filePath, line) {
|
export function addLineToFile(filePath, line) {
|
||||||
if (!fs.existsSync(filePath)) {
|
createFileIfNotExists(filePath);
|
||||||
fs.writeFileSync(filePath, "", "utf-8");
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
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");
|
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");
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -56,11 +56,13 @@ export async function setup() {
|
||||||
*/
|
*/
|
||||||
function setupShell(shell) {
|
function setupShell(shell) {
|
||||||
let success = false;
|
let success = false;
|
||||||
|
let error;
|
||||||
try {
|
try {
|
||||||
shell.teardown(knownAikidoTools); // First, tear down to prevent duplicate aliases
|
shell.teardown(knownAikidoTools); // First, tear down to prevent duplicate aliases
|
||||||
success = shell.setup(knownAikidoTools);
|
success = shell.setup(knownAikidoTools);
|
||||||
} catch {
|
} catch (err) {
|
||||||
success = false;
|
success = false;
|
||||||
|
error = err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
|
|
@ -75,6 +77,13 @@ function setupShell(shell) {
|
||||||
"Setup failed"
|
"Setup failed"
|
||||||
)}. Please check your ${shell.name} configuration.`
|
)}. 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;
|
return success;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue