mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Move safe-chain package to packages/safe-chain
This commit is contained in:
parent
fc9a9ca129
commit
7673d32912
68 changed files with 85 additions and 52 deletions
68
packages/safe-chain/src/shell-integration/helpers.js
Normal file
68
packages/safe-chain/src/shell-integration/helpers.js
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
import { spawnSync } from "child_process";
|
||||
import * as os from "os";
|
||||
import fs from "fs";
|
||||
|
||||
export const knownAikidoTools = [
|
||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "npx", aikidoCommand: "aikido-npx" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" },
|
||||
{ tool: "pnpm", aikidoCommand: "aikido-pnpm" },
|
||||
{ tool: "pnpx", aikidoCommand: "aikido-pnpx" },
|
||||
// When adding a new tool here, also update the expected alias in the tests (setup.spec.js, teardown.spec.js)
|
||||
// and add the documentation for the new tool in the README.md
|
||||
];
|
||||
|
||||
export function doesExecutableExistOnSystem(executableName) {
|
||||
if (os.platform() === "win32") {
|
||||
const result = spawnSync("where", [executableName], { stdio: "ignore" });
|
||||
return result.status === 0;
|
||||
} else {
|
||||
const result = spawnSync("which", [executableName], { stdio: "ignore" });
|
||||
return result.status === 0;
|
||||
}
|
||||
}
|
||||
|
||||
export function removeLinesMatchingPattern(filePath, pattern) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
const lines = fileContent.split(/[\r\n\u2028\u2029]+/);
|
||||
const updatedLines = lines.filter((line) => !shouldRemoveLine(line, pattern));
|
||||
fs.writeFileSync(filePath, updatedLines.join(os.EOL), "utf-8");
|
||||
}
|
||||
|
||||
const maxLineLength = 100;
|
||||
function shouldRemoveLine(line, pattern) {
|
||||
const isPatternMatch = pattern.test(line);
|
||||
|
||||
if (!isPatternMatch) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (line.length > maxLineLength) {
|
||||
// safe-chain only adds lines shorter than maxLineLength
|
||||
// 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 the line contains newlines, something has gone wrong in splitting
|
||||
// \u2028 and \u2029 are Unicode line separator characters (line and paragraph separators)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export function addLineToFile(filePath, line) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
fs.writeFileSync(filePath, "", "utf-8");
|
||||
}
|
||||
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
const updatedContent = fileContent + os.EOL + line;
|
||||
fs.writeFileSync(filePath, updatedContent, "utf-8");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue