Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# Run GasGuard staged-file scan
node scripts/pre-commit-scan.js
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "gasguard-monorepo",
"private": true,
"scripts": {
"prepare": "husky install",
"build": "tsc -b",
"build:engine": "tsc -p libs/engine/tsconfig.json",
"build:api": "nest build",
Expand All @@ -28,6 +29,7 @@
"rxjs": "^7.8.1"
},
"devDependencies": {
"husky": "^8.0.0",
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
Expand Down
60 changes: 60 additions & 0 deletions scripts/pre-commit-scan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env node
const { execSync, spawnSync } = require('child_process');
const path = require('path');

function getStagedFiles() {
try {
const out = execSync('git diff --cached --name-only --diff-filter=ACM', { encoding: 'utf8' });
return out.split(/\r?\n/).map(s => s.trim()).filter(Boolean);
} catch (err) {
console.error('Failed to get staged files:', err.message);
process.exit(1);
}
}

function filterScannable(files) {
const exts = ['.sol', '.vy', '.rs'];
return files.filter(f => exts.includes(path.extname(f)));
}

function runScanOnFile(file) {
console.log(`Running GasGuard scan on staged file: ${file}`);

// Execute the monorepo CLI using ts-node to run the TypeScript source directly.
// This avoids requiring a pre-built CLI binary during development.
const runner = 'node';
const args = ['-r', 'ts-node/register', 'packages/cli/src/index.ts', 'scan', file, '--no-summary', '--format', 'text'];

const res = spawnSync(runner, args, { stdio: 'inherit' });
if (res.error) {
console.error('Failed to start scan process:', res.error);
return res.status || 1;
}
return res.status;
}

async function main() {
const staged = getStagedFiles();
const targets = filterScannable(staged);

if (targets.length === 0) {
console.log('No scannable staged files found. Skipping GasGuard pre-commit scan.');
process.exit(0);
}

let failed = false;
for (const f of targets) {
const code = runScanOnFile(f);
if (code !== 0) {
failed = true;
console.error(`GasGuard scan failed for ${f} (exit ${code})`);
break;
}
}

if (failed) process.exit(1);
console.log('GasGuard pre-commit scan passed.');
process.exit(0);
}

main();
Loading