mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-10-09 21:14:22 +00:00
chore(ui): scope prek pre-commit to staged files, drop legacy husky (#11118)
Co-authored-by: Pablo F.G <pablo.fernandez@prowler.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Pablo F.G
Claude Opus 4.7
parent
9bd4e4b65c
commit
f2e6a3264d
@@ -7,21 +7,25 @@
|
||||
* If not, it runs the repository's setup script to install prek.
|
||||
*/
|
||||
|
||||
const { execSync } = require('child_process');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { execSync } = require("child_process");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
/**
|
||||
* Check if prek framework is managing git hooks
|
||||
*/
|
||||
function isPrekInstalled(gitRoot) {
|
||||
const hookPath = path.join(gitRoot, '.git', 'hooks', 'pre-commit');
|
||||
const hookPath = path.join(gitRoot, ".git", "hooks", "pre-commit");
|
||||
|
||||
try {
|
||||
if (!fs.existsSync(hookPath)) return false;
|
||||
|
||||
const content = fs.readFileSync(hookPath, 'utf8');
|
||||
return content.includes('prek') || content.includes('pre-commit') || content.includes('INSTALL_PYTHON');
|
||||
const content = fs.readFileSync(hookPath, "utf8");
|
||||
return (
|
||||
content.includes("prek") ||
|
||||
content.includes("pre-commit") ||
|
||||
content.includes("INSTALL_PYTHON")
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
@@ -32,9 +36,9 @@ function isPrekInstalled(gitRoot) {
|
||||
*/
|
||||
function getGitRoot() {
|
||||
try {
|
||||
return execSync('git rev-parse --show-toplevel', {
|
||||
encoding: 'utf8',
|
||||
stdio: ['pipe', 'pipe', 'pipe']
|
||||
return execSync("git rev-parse --show-toplevel", {
|
||||
encoding: "utf8",
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
}).trim();
|
||||
} catch {
|
||||
return null;
|
||||
@@ -45,51 +49,61 @@ function getGitRoot() {
|
||||
* Run the repository setup script
|
||||
*/
|
||||
function runSetupScript(gitRoot) {
|
||||
const setupScript = path.join(gitRoot, 'scripts', 'setup-git-hooks.sh');
|
||||
const setupScript = path.join(gitRoot, "scripts", "setup-git-hooks.sh");
|
||||
|
||||
if (!fs.existsSync(setupScript)) {
|
||||
throw new Error('Setup script not found');
|
||||
throw new Error("Setup script not found");
|
||||
}
|
||||
|
||||
execSync(`bash "${setupScript}"`, {
|
||||
cwd: gitRoot,
|
||||
stdio: 'inherit'
|
||||
stdio: "inherit",
|
||||
});
|
||||
}
|
||||
|
||||
// Main execution
|
||||
|
||||
// Skip in Docker/CI environments
|
||||
if (process.env.DOCKER || process.env.CI || process.env.KUBERNETES_SERVICE_HOST) {
|
||||
console.log('⚠️ Running in containerized environment. Skipping git hooks setup.');
|
||||
if (
|
||||
process.env.DOCKER ||
|
||||
process.env.CI ||
|
||||
process.env.KUBERNETES_SERVICE_HOST
|
||||
) {
|
||||
console.log(
|
||||
"⚠️ Running in containerized environment. Skipping git hooks setup.",
|
||||
);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const gitRoot = getGitRoot();
|
||||
|
||||
if (!gitRoot) {
|
||||
console.log('⚠️ Not in a git repository. Skipping git hooks setup.');
|
||||
console.log("⚠️ Not in a git repository. Skipping git hooks setup.");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (isPrekInstalled(gitRoot)) {
|
||||
console.log('✅ Git hooks managed by prek framework');
|
||||
console.log(' UI hooks will be called automatically for UI files');
|
||||
console.log("✅ Git hooks managed by prek framework");
|
||||
console.log(" UI hooks will be called automatically for UI files");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Prek not installed - set it up
|
||||
console.log('⚠️ Prek hooks not installed');
|
||||
console.log('📦 Installing prek hooks...');
|
||||
console.log('');
|
||||
console.log("⚠️ Prek hooks not installed");
|
||||
console.log("📦 Installing prek hooks...");
|
||||
console.log("");
|
||||
|
||||
try {
|
||||
runSetupScript(gitRoot);
|
||||
console.log('');
|
||||
console.log('✅ Prek hooks installed successfully');
|
||||
console.log("");
|
||||
console.log("✅ Prek hooks installed successfully");
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to setup git hooks');
|
||||
console.error(' Please run manually from repo root: ./scripts/setup-git-hooks.sh');
|
||||
console.error(' Or install prek manually: https://prek.j178.dev/installation/');
|
||||
console.error("❌ Failed to setup git hooks");
|
||||
console.error(
|
||||
" Please run manually from repo root: ./scripts/setup-git-hooks.sh",
|
||||
);
|
||||
console.error(
|
||||
" Or install prek manually: https://prek.j178.dev/installation/",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
function readJSON(p) {
|
||||
return JSON.parse(fs.readFileSync(p, 'utf8'));
|
||||
return JSON.parse(fs.readFileSync(p, "utf8"));
|
||||
}
|
||||
|
||||
function getInstalledVersion(pkgName) {
|
||||
try {
|
||||
const parts = pkgName.split('/');
|
||||
const pkgPath = path.join('node_modules', ...parts, 'package.json');
|
||||
const parts = pkgName.split("/");
|
||||
const pkgPath = path.join("node_modules", ...parts, "package.json");
|
||||
const meta = readJSON(pkgPath);
|
||||
return meta.version;
|
||||
} catch (e) {
|
||||
@@ -25,35 +25,37 @@ function collect(sectionName, obj) {
|
||||
name,
|
||||
from: declared,
|
||||
to: installed || null,
|
||||
strategy: 'installed',
|
||||
strategy: "installed",
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function main() {
|
||||
// If node_modules is missing, skip to avoid generating noisy diffs
|
||||
if (!fs.existsSync('node_modules')) {
|
||||
console.log('Skip: node_modules not found. Run npm install first.');
|
||||
if (!fs.existsSync("node_modules")) {
|
||||
console.log("Skip: node_modules not found. Run npm install first.");
|
||||
return;
|
||||
}
|
||||
|
||||
const pkg = readJSON('package.json');
|
||||
const pkg = readJSON("package.json");
|
||||
const entries = [
|
||||
...collect('dependencies', pkg.dependencies),
|
||||
...collect('devDependencies', pkg.devDependencies),
|
||||
...collect("dependencies", pkg.dependencies),
|
||||
...collect("devDependencies", pkg.devDependencies),
|
||||
];
|
||||
|
||||
// Stable sort by section then name
|
||||
entries.sort((a, b) =>
|
||||
a.section === b.section ? a.name.localeCompare(b.name) : a.section.localeCompare(b.section)
|
||||
a.section === b.section
|
||||
? a.name.localeCompare(b.name)
|
||||
: a.section.localeCompare(b.section),
|
||||
);
|
||||
|
||||
const outPath = path.join(process.cwd(), 'dependency-log.json');
|
||||
const outPath = path.join(process.cwd(), "dependency-log.json");
|
||||
// Merge with previous to preserve generatedAt when unchanged
|
||||
let prevMap = new Map();
|
||||
if (fs.existsSync(outPath)) {
|
||||
try {
|
||||
const prev = JSON.parse(fs.readFileSync(outPath, 'utf8'));
|
||||
const prev = JSON.parse(fs.readFileSync(outPath, "utf8"));
|
||||
for (const e of prev) {
|
||||
prevMap.set(`${e.section}::${e.name}`, e);
|
||||
}
|
||||
@@ -78,10 +80,10 @@ function main() {
|
||||
return { ...e, from: prev.from, generatedAt: prev.generatedAt || now };
|
||||
});
|
||||
|
||||
const nextContent = JSON.stringify(merged, null, 2) + '\n';
|
||||
const nextContent = JSON.stringify(merged, null, 2) + "\n";
|
||||
if (fs.existsSync(outPath)) {
|
||||
try {
|
||||
const prevContent = fs.readFileSync(outPath, 'utf8');
|
||||
const prevContent = fs.readFileSync(outPath, "utf8");
|
||||
if (prevContent === nextContent) {
|
||||
console.log(`No changes for ${outPath} (entries: ${entries.length}).`);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user