UNPKG

secure-scan-js

Version:

A JavaScript implementation of Yelp's detect-secrets tool - no Python required

163 lines (147 loc) 5.42 kB
const { exec } = require("child_process"); const os = require("os"); const util = require("util"); const execAsync = util.promisify(exec); async function checkGitleaksInstalled() { try { const { stdout } = await execAsync("gitleaks version"); console.log(`Gitleaks is already installed: ${stdout.trim()}`); return true; } catch (error) { return false; } } async function installGitleaks() { const platform = os.platform(); try { console.log("Attempting to install Gitleaks..."); // First check if Gitleaks is already installed if (await checkGitleaksInstalled()) { return; } if (platform === "win32") { try { await execAsync("choco -v"); console.log("Installing Gitleaks using Chocolatey..."); await execAsync("choco install gitleaks -y"); return; } catch { // If Chocolatey isn't available, show manual installation instructions console.log("\n⚠️ Chocolatey is not installed on your system."); console.log("\nTo install Gitleaks, you have several options:"); console.log("\n1. Install using Chocolatey (Recommended):"); console.log( " a. First install Chocolatey by following instructions at:" ); console.log(" https://chocolatey.org/install"); console.log(" b. Then run: choco install gitleaks -y"); console.log("\n2. Manual Installation:"); console.log( " a. Visit: https://github.com/gitleaks/gitleaks/releases" ); console.log( " b. Download the latest Windows zip file (gitleaks_8.24.2_windows_x64.zip)" ); console.log(" c. Extract the gitleaks.exe file"); console.log( " d. Add it to your PATH or copy it to a directory in your PATH" ); console.log(" (e.g., C:\\Windows\\System32)"); console.log("\n3. Using Scoop (Alternative):"); console.log(" a. Install Scoop from https://scoop.sh"); console.log(" b. Run: scoop install gitleaks"); console.log("\nAfter installation, verify by running:"); console.log("gitleaks version"); throw new Error( "Please install Gitleaks using one of the methods above and try again." ); } } else if (platform === "darwin") { // macOS - try Homebrew try { await execAsync("brew -v"); console.log("Installing Gitleaks using Homebrew..."); await execAsync("brew install gitleaks"); } catch { throw new Error( "Homebrew is not installed. Please install Gitleaks manually:\n" + "1. Install Homebrew (https://brew.sh/)\n" + "2. Then run: brew install gitleaks" ); } } else { // Linux - try different package managers or handle existing installation try { // Try apt-get (Debian/Ubuntu) await execAsync("apt-get -v"); console.log("Installing Gitleaks using apt..."); await execAsync( "sudo apt-get update && sudo apt-get install -y gitleaks" ); } catch { try { // Try yum (RHEL/CentOS) await execAsync("yum -v"); console.log("Installing Gitleaks using yum..."); await execAsync("sudo yum install -y gitleaks"); } catch { // Try manual installation for CI environments like CodeBuild try { console.log("Attempting manual installation for CI environment..."); await execAsync(` curl -sSL https://github.com/gitleaks/gitleaks/releases/download/v8.24.3/gitleaks_8.24.3_linux_x64.tar.gz | tar -xz && sudo mv gitleaks /usr/local/bin/ && sudo chmod +x /usr/local/bin/gitleaks `); return; } catch (ciError) { throw new Error( "Could not install Gitleaks automatically. Please install manually:\n" + "Visit: https://github.com/gitleaks/gitleaks#installation" ); } } } } console.log("Gitleaks installed successfully!"); } catch (error) { if (error.message.includes("Please install Gitleaks")) { // This is our custom error with instructions, exit gracefully process.exit(1); } // For unexpected errors, show general guidance console.error("\n❌ Failed to install Gitleaks automatically."); console.log("\nPlease install Gitleaks manually:"); if (platform === "win32") { console.log("\nWindows Installation Options:"); console.log("1. Download the latest release from:"); console.log(" https://github.com/gitleaks/gitleaks/releases"); console.log("2. Extract the zip file"); console.log("3. Add gitleaks.exe to your system PATH"); console.log("\nOr use a package manager:"); console.log("- Chocolatey: choco install gitleaks"); console.log("- Scoop: scoop install gitleaks"); } else if (platform === "darwin") { console.log("\nmacOS Installation:"); console.log("1. Using Homebrew (recommended):"); console.log(" brew install gitleaks"); console.log("\n2. Manual installation:"); console.log( " Download from https://github.com/gitleaks/gitleaks/releases" ); } else { console.log("\nLinux Installation:"); console.log( "1. Download from https://github.com/gitleaks/gitleaks/releases" ); console.log("2. Extract and move to /usr/local/bin:"); console.log(" sudo mv gitleaks /usr/local/bin/"); console.log(" sudo chmod +x /usr/local/bin/gitleaks"); } console.log("\nAfter installation, verify by running:"); console.log("gitleaks version"); process.exit(1); } } // Run the installation installGitleaks().catch(console.error);