mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Cleanup setup.js and teardown.js
This commit is contained in:
parent
68a4064a5c
commit
de7bfa5841
2 changed files with 56 additions and 50 deletions
|
|
@ -2,28 +2,47 @@ import fs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { getGlobalConfigPath, addScannerToToml } from "./toml-utils.js";
|
import { getGlobalConfigPath, addScannerToToml } from "./toml-utils.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an empty bunfig.toml file if it doesn't exist
|
||||||
|
* @param {string} filePath - Path to the bunfig.toml file
|
||||||
|
*/
|
||||||
|
function ensureBunfigExists(filePath) {
|
||||||
|
if (!fs.existsSync(filePath)) {
|
||||||
|
const dir = path.dirname(filePath);
|
||||||
|
fs.mkdirSync(dir, { recursive: true });
|
||||||
|
fs.writeFileSync(filePath, "", "utf8");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main setup function that registers safe-chain-bun as a security scanner
|
* Main setup function that registers safe-chain-bun as a security scanner
|
||||||
* @param {string|undefined} configFile - Optional path to specific bunfig.toml file
|
* @param {string|undefined} configFile - Optional path to specific bunfig.toml file
|
||||||
*/
|
*/
|
||||||
export function setup(configFile) {
|
export function setup(configFile) {
|
||||||
try {
|
try {
|
||||||
const targetFile = configFile ? path.resolve(configFile) : getGlobalConfigPath();
|
let targetFile = configFile;
|
||||||
const isGlobal = !configFile;
|
if (!targetFile) {
|
||||||
|
targetFile = getGlobalConfigPath();
|
||||||
|
ensureBunfigExists(targetFile);
|
||||||
|
} else {
|
||||||
|
targetFile = path.resolve(configFile);
|
||||||
|
}
|
||||||
|
|
||||||
if (configFile && !fs.existsSync(targetFile)) {
|
if (configFile && !fs.existsSync(targetFile)) {
|
||||||
console.error(`❌ Config file not found: ${configFile}`);
|
console.error(`❌ Config file not found: ${configFile}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const updated = updateBunfigFile(targetFile, isGlobal);
|
const updated = updateBunfigFile(targetFile);
|
||||||
|
|
||||||
if (updated) {
|
if (updated) {
|
||||||
const displayPath = isGlobal ? "~/.bunfig.toml" : configFile;
|
console.log(
|
||||||
console.log(`✅ Safe-Chain-Bun registered as security scanner in ${displayPath}`);
|
`✅ Safe-Chain-Bun registered as security scanner in ${configFile}`
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
const displayPath = isGlobal ? "~/.bunfig.toml" : configFile;
|
console.log(
|
||||||
console.log(`ℹ️ Safe-Chain-Bun is already configured as security scanner in ${displayPath}`);
|
`ℹ️ Safe-Chain-Bun is already configured as security scanner in ${configFile}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`❌ Failed to setup Safe-Chain-Bun: ${error.message}`);
|
console.error(`❌ Failed to setup Safe-Chain-Bun: ${error.message}`);
|
||||||
|
|
@ -31,51 +50,38 @@ export function setup(configFile) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates or creates a bunfig.toml file with safe-chain-bun scanner configuration
|
* Updates a bunfig.toml file with safe-chain-bun scanner configuration
|
||||||
* @param {string} filePath - Path to the bunfig.toml file
|
* @param {string} filePath - Path to the bunfig.toml file
|
||||||
* @param {boolean} isGlobal - Whether this is the global config file
|
|
||||||
* @returns {boolean} True if file was updated, false if already configured
|
* @returns {boolean} True if file was updated, false if already configured
|
||||||
*/
|
*/
|
||||||
function updateBunfigFile(filePath, isGlobal) {
|
function updateBunfigFile(filePath) {
|
||||||
let content = "";
|
let content = "";
|
||||||
let fileExists = fs.existsSync(filePath);
|
let fileExists = fs.existsSync(filePath);
|
||||||
|
|
||||||
if (fileExists) {
|
if (!fileExists) {
|
||||||
try {
|
|
||||||
content = fs.readFileSync(filePath, "utf8");
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error(`Failed to read ${filePath}: ${error.message}`);
|
|
||||||
}
|
|
||||||
} else if (!isGlobal) {
|
|
||||||
// For specific files, they must exist
|
|
||||||
throw new Error(`Config file does not exist: ${filePath}`);
|
throw new Error(`Config file does not exist: ${filePath}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
content = fs.readFileSync(filePath, "utf8");
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Failed to read ${filePath}: ${error.message}`);
|
||||||
|
}
|
||||||
|
|
||||||
const result = addScannerToToml(content);
|
const result = addScannerToToml(content);
|
||||||
|
|
||||||
if (!result.changed) {
|
if (!result.changed) {
|
||||||
return false; // Already configured
|
return false; // Already configured
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Ensure directory exists for global config
|
|
||||||
if (isGlobal && !fileExists) {
|
|
||||||
const dir = path.dirname(filePath);
|
|
||||||
fs.mkdirSync(dir, { recursive: true });
|
|
||||||
console.log(`ℹ️ Created ${filePath} with Safe-Chain-Bun configuration`);
|
|
||||||
} else if (fileExists) {
|
|
||||||
console.log(`ℹ️ Updated existing bunfig.toml with Safe-Chain-Bun scanner`);
|
|
||||||
}
|
|
||||||
|
|
||||||
fs.writeFileSync(filePath, result.content, "utf8");
|
fs.writeFileSync(filePath, result.content, "utf8");
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.code === "EACCES") {
|
if (error.code === "EACCES") {
|
||||||
throw new Error(`Permission denied writing to ${filePath}`);
|
throw new Error(`Permission denied writing to ${filePath}`);
|
||||||
}
|
}
|
||||||
throw new Error(`Failed to write ${filePath}: ${error.message}`);
|
throw new Error(`Failed to write ${filePath}: ${error.message} (${error.code})`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,28 +8,28 @@ import { getGlobalConfigPath, removeScannerFromToml } from "./toml-utils.js";
|
||||||
*/
|
*/
|
||||||
export function teardown(configFile) {
|
export function teardown(configFile) {
|
||||||
try {
|
try {
|
||||||
const targetFile = configFile ? path.resolve(configFile) : getGlobalConfigPath();
|
const targetFile = configFile
|
||||||
const isGlobal = !configFile;
|
? path.resolve(configFile)
|
||||||
|
: getGlobalConfigPath();
|
||||||
|
|
||||||
if (!fs.existsSync(targetFile)) {
|
if (!fs.existsSync(targetFile)) {
|
||||||
const displayPath = isGlobal ? "~/.bunfig.toml" : configFile;
|
console.log(`ℹ️ Config file not found: ${targetFile}`);
|
||||||
console.log(`ℹ️ Config file not found: ${displayPath}`);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const content = fs.readFileSync(targetFile, "utf8");
|
const content = fs.readFileSync(targetFile, "utf8");
|
||||||
const result = removeScannerFromToml(content);
|
const result = removeScannerFromToml(content);
|
||||||
|
|
||||||
if (result.changed) {
|
if (result.changed) {
|
||||||
fs.writeFileSync(targetFile, result.content, "utf8");
|
fs.writeFileSync(targetFile, result.content, "utf8");
|
||||||
const displayPath = isGlobal ? "~/.bunfig.toml" : configFile;
|
console.log(`✅ Safe-Chain-Bun scanner removed from ${targetFile}`);
|
||||||
console.log(`✅ Safe-Chain-Bun scanner removed from ${displayPath}`);
|
|
||||||
} else {
|
} else {
|
||||||
const displayPath = isGlobal ? "~/.bunfig.toml" : configFile;
|
console.log(`ℹ️ Safe-Chain-Bun scanner not found in ${targetFile}`);
|
||||||
console.log(`ℹ️ Safe-Chain-Bun scanner not found in ${displayPath}`);
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`❌ Failed to remove Safe-Chain-Bun scanner: ${error.message}`);
|
console.error(
|
||||||
|
`❌ Failed to remove Safe-Chain-Bun scanner: ${error.message} (${error.code})`
|
||||||
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue