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 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,27 +50,24 @@ 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) {
throw new Error(`Config file does not exist: ${filePath}`);
}
try { try {
content = fs.readFileSync(filePath, "utf8"); content = fs.readFileSync(filePath, "utf8");
} catch (error) { } catch (error) {
throw new Error(`Failed to read ${filePath}: ${error.message}`); 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}`);
}
const result = addScannerToToml(content); const result = addScannerToToml(content);
@ -60,22 +76,12 @@ function updateBunfigFile(filePath, isGlobal) {
} }
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})`);
} }
} }

View file

@ -8,12 +8,12 @@ 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;
} }
@ -22,14 +22,14 @@ export function teardown(configFile) {
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);
} }
} }