Cleanup setup.js and teardown.js

This commit is contained in:
Sander Declerck 2025-09-08 10:19:16 +02:00
parent 68a4064a5c
commit de7bfa5841
No known key found for this signature in database
2 changed files with 56 additions and 50 deletions

View file

@ -2,28 +2,47 @@ import fs from "fs";
import path from "path";
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
* @param {string|undefined} configFile - Optional path to specific bunfig.toml file
*/
export function setup(configFile) {
try {
const targetFile = configFile ? path.resolve(configFile) : getGlobalConfigPath();
const isGlobal = !configFile;
let targetFile = configFile;
if (!targetFile) {
targetFile = getGlobalConfigPath();
ensureBunfigExists(targetFile);
} else {
targetFile = path.resolve(configFile);
}
if (configFile && !fs.existsSync(targetFile)) {
console.error(`❌ Config file not found: ${configFile}`);
process.exit(1);
}
const updated = updateBunfigFile(targetFile, isGlobal);
const updated = updateBunfigFile(targetFile);
if (updated) {
const displayPath = isGlobal ? "~/.bunfig.toml" : configFile;
console.log(`✅ Safe-Chain-Bun registered as security scanner in ${displayPath}`);
console.log(
`✅ Safe-Chain-Bun registered as security scanner in ${configFile}`
);
} else {
const displayPath = isGlobal ? "~/.bunfig.toml" : configFile;
console.log(` Safe-Chain-Bun is already configured as security scanner in ${displayPath}`);
console.log(
` Safe-Chain-Bun is already configured as security scanner in ${configFile}`
);
}
} catch (error) {
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 {boolean} isGlobal - Whether this is the global config file
* @returns {boolean} True if file was updated, false if already configured
*/
function updateBunfigFile(filePath, isGlobal) {
function updateBunfigFile(filePath) {
let content = "";
let fileExists = fs.existsSync(filePath);
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
if (!fileExists) {
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);
if (!result.changed) {
return false; // Already configured
}
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");
return true;
} catch (error) {
if (error.code === "EACCES") {
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})`);
}
}

View file

@ -8,28 +8,28 @@ import { getGlobalConfigPath, removeScannerFromToml } from "./toml-utils.js";
*/
export function teardown(configFile) {
try {
const targetFile = configFile ? path.resolve(configFile) : getGlobalConfigPath();
const isGlobal = !configFile;
const targetFile = configFile
? path.resolve(configFile)
: getGlobalConfigPath();
if (!fs.existsSync(targetFile)) {
const displayPath = isGlobal ? "~/.bunfig.toml" : configFile;
console.log(` Config file not found: ${displayPath}`);
console.log(` Config file not found: ${targetFile}`);
return;
}
const content = fs.readFileSync(targetFile, "utf8");
const result = removeScannerFromToml(content);
if (result.changed) {
fs.writeFileSync(targetFile, result.content, "utf8");
const displayPath = isGlobal ? "~/.bunfig.toml" : configFile;
console.log(`✅ Safe-Chain-Bun scanner removed from ${displayPath}`);
console.log(`✅ Safe-Chain-Bun scanner removed from ${targetFile}`);
} else {
const displayPath = isGlobal ? "~/.bunfig.toml" : configFile;
console.log(` Safe-Chain-Bun scanner not found in ${displayPath}`);
console.log(` Safe-Chain-Bun scanner not found in ${targetFile}`);
}
} 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);
}
}
}