Files
SeedPass/torch/scripts/check-innerhtml.mjs
T
google-labs-jules[bot]andPR0M3TH3AN 55eec8c490 Install and setup TORCH agent system
- Initialize Node.js environment with `package.json`
- Install `torch-lock` from GitHub
- Initialize `torch/` configuration and prompts
- Add `node_modules` to `.gitignore`
- Add `lock:*` scripts to `package.json` for scheduler integration
- Update `check-file-size.mjs` and `check-innerhtml.mjs` to support `--update` and `BASELINE` logic required by prompts

Co-authored-by: PR0M3TH3AN <[email protected]>
2026-02-15 23:36:50 +00:00

137 lines
3.7 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import { parseArgs } from 'node:util';
const BASELINE = {};
const DEFAULT_EXCLUDED_DIRS = ['node_modules', '.git', 'dist', 'artifacts', 'test_logs', 'coverage'];
const DEFAULT_TARGET_EXTENSIONS = ['.js', '.mjs', '.ts', '.html'];
const DEFAULT_EXCLUDED_FILES = [];
function countInnerHTML(filePath) {
try {
const content = fs.readFileSync(filePath, 'utf8');
// Basic regex to find assignment to innerHTML
const regex = /\.innerHTML\s*=/g;
const matches = content.match(regex);
return matches ? matches.length : 0;
} catch (error) {
console.error(`Error reading ${filePath}: ${error.message}`);
return 0;
}
}
function main() {
const { values } = parseArgs({
options: {
'exclude-dir': { type: 'string', multiple: true },
'exclude-file': { type: 'string', multiple: true },
'no-defaults': { type: 'boolean' },
'report': { type: 'boolean' },
'update': { type: 'boolean' },
'help': { type: 'boolean' },
},
strict: false,
});
if (values.help) {
console.log(`
Usage: check-innerhtml.mjs [options]
Options:
--exclude-dir <dir> Exclude directory (can be used multiple times)
--exclude-file <file> Exclude file (can be used multiple times)
--no-defaults Do not use default exclusions
--report Report mode (exit code 0 even if violations found)
--update Generate new BASELINE object
--help Show this help
`);
process.exit(0);
}
const noDefaults = values['no-defaults'] || false;
const EXCLUDED_DIRS = noDefaults ? [] : [...DEFAULT_EXCLUDED_DIRS];
if (values['exclude-dir']) {
EXCLUDED_DIRS.push(...values['exclude-dir']);
}
const EXCLUDED_FILES = noDefaults ? [] : [...DEFAULT_EXCLUDED_FILES];
if (values['exclude-file']) {
EXCLUDED_FILES.push(...values['exclude-file']);
}
const TARGET_EXTENSIONS = DEFAULT_TARGET_EXTENSIONS;
function scanDirectory(dir) {
let files = [];
try {
const items = fs.readdirSync(dir);
for (const item of items) {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
if (!EXCLUDED_DIRS.includes(item)) {
files = files.concat(scanDirectory(fullPath));
}
} else if (stat.isFile()) {
if (EXCLUDED_FILES.includes(item)) {
continue;
}
if (TARGET_EXTENSIONS.includes(path.extname(item))) {
files.push(fullPath);
}
}
}
} catch (error) {
console.error(`Error scanning ${dir}: ${error.message}`);
}
return files;
}
const reportMode = values.report;
const updateMode = values.update;
const files = scanDirectory('.');
if (updateMode) {
const newBaseline = {};
for (const file of files) {
const count = countInnerHTML(file);
if (count > 0) {
newBaseline[file] = count;
}
}
console.log(`const BASELINE = ${JSON.stringify(newBaseline, null, 2)};`);
return;
}
let totalAssignments = 0;
let offenderCount = 0;
for (const file of files) {
const count = countInnerHTML(file);
const limit = BASELINE[file] || 0;
if (count > limit) {
console.log(`${file}: ${count} assignments (limit: ${limit}, excess: ${count - limit})`);
totalAssignments += count;
offenderCount++;
} else if (reportMode && count > 0) {
console.log(`${file}: ${count} assignments (grandfathered, limit: ${limit})`);
totalAssignments += count;
}
}
if (reportMode && totalAssignments > 0) {
console.log(`Total assignments: ${totalAssignments}`);
}
if (!reportMode && offenderCount > 0) {
process.exit(1);
}
}
main();