mirror of
https://github.com/PR0M3TH3AN/TORCH.git
synced 2026-10-27 07:51:52 +00:00
refactor: make exclusions in check scripts configurable
Refactor `scripts/check-file-size.mjs` and `scripts/check-innerhtml.mjs` to use `node:util.parseArgs` for parsing command-line arguments. This allows users to configure directory and file exclusions via `--exclude-dir`, `--exclude-file`, and `--no-defaults`, instead of relying on hardcoded lists. This change improves versatility while maintaining backward compatibility by keeping the original hardcoded lists as defaults. It also adds support for `--max-lines` in `check-file-size.mjs` and `--help` in both scripts. Verification: - Manually verified that both scripts respect the new flags and defaults. - Confirmed that `--report` mode still works as expected. - Confirmed that unknown flags (like `--update`) are safely ignored (due to `strict: false`). Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
1 parent
695adb6dc9
commit
6fdc092441
2 files changed
+133
-50
No files matched your search
+67
-27
@@ -1,9 +1,10 @@
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
import { parseArgs } from 'node:util';
|
||||||
|
|
||||||
const MAX_LINES = 300;
|
const DEFAULT_MAX_LINES = 300;
|
||||||
const EXCLUDED_DIRS = ['node_modules', '.git', 'dist', 'artifacts', 'test_logs', 'coverage'];
|
const DEFAULT_EXCLUDED_DIRS = ['node_modules', '.git', 'dist', 'artifacts', 'test_logs', 'coverage'];
|
||||||
const EXCLUDED_FILES = ['package-lock.json'];
|
const DEFAULT_EXCLUDED_FILES = ['package-lock.json'];
|
||||||
|
|
||||||
function countLines(filePath) {
|
function countLines(filePath) {
|
||||||
try {
|
try {
|
||||||
@@ -15,36 +16,75 @@ function countLines(filePath) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function scanDirectory(dir) {
|
function main() {
|
||||||
let files = [];
|
const { values } = parseArgs({
|
||||||
try {
|
options: {
|
||||||
const items = fs.readdirSync(dir);
|
'max-lines': { type: 'string' },
|
||||||
for (const item of items) {
|
'exclude-dir': { type: 'string', multiple: true },
|
||||||
const fullPath = path.join(dir, item);
|
'exclude-file': { type: 'string', multiple: true },
|
||||||
const stat = fs.statSync(fullPath);
|
'no-defaults': { type: 'boolean' },
|
||||||
|
'report': { type: 'boolean' },
|
||||||
|
'help': { type: 'boolean' },
|
||||||
|
},
|
||||||
|
strict: false,
|
||||||
|
});
|
||||||
|
|
||||||
if (stat.isDirectory()) {
|
if (values.help) {
|
||||||
if (!EXCLUDED_DIRS.includes(item)) {
|
console.log(`
|
||||||
files = files.concat(scanDirectory(fullPath));
|
Usage: check-file-size.mjs [options]
|
||||||
}
|
|
||||||
} else if (stat.isFile()) {
|
Options:
|
||||||
if (EXCLUDED_FILES.includes(item)) {
|
--max-lines <n> Max lines allowed (default: ${DEFAULT_MAX_LINES})
|
||||||
continue;
|
--exclude-dir <dir> Exclude directory (can be used multiple times)
|
||||||
}
|
--exclude-file <file> Exclude file (can be used multiple times)
|
||||||
if (['.js', '.mjs', '.ts', '.html', '.css', '.json', '.md'].includes(path.extname(item))) {
|
--no-defaults Do not use default exclusions
|
||||||
files.push(fullPath);
|
--report Report mode (exit code 0 even if violations found)
|
||||||
|
--help Show this help
|
||||||
|
`);
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
const MAX_LINES = values['max-lines'] ? parseInt(values['max-lines'], 10) : DEFAULT_MAX_LINES;
|
||||||
|
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']);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (['.js', '.mjs', '.ts', '.html', '.css', '.json', '.md'].includes(path.extname(item))) {
|
||||||
|
files.push(fullPath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error scanning ${dir}: ${error.message}`);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
return files;
|
||||||
console.error(`Error scanning ${dir}: ${error.message}`);
|
|
||||||
}
|
}
|
||||||
return files;
|
|
||||||
}
|
|
||||||
|
|
||||||
function main() {
|
const reportMode = values.report;
|
||||||
const args = process.argv.slice(2);
|
|
||||||
const reportMode = args.includes('--report');
|
|
||||||
|
|
||||||
const files = scanDirectory('.');
|
const files = scanDirectory('.');
|
||||||
let oversizedCount = 0;
|
let oversizedCount = 0;
|
||||||
|
|||||||
+66
-23
@@ -1,8 +1,10 @@
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
import { parseArgs } from 'node:util';
|
||||||
|
|
||||||
const EXCLUDED_DIRS = ['node_modules', '.git', 'dist', 'artifacts', 'test_logs', 'coverage'];
|
const DEFAULT_EXCLUDED_DIRS = ['node_modules', '.git', 'dist', 'artifacts', 'test_logs', 'coverage'];
|
||||||
const TARGET_EXTENSIONS = ['.js', '.mjs', '.ts', '.html'];
|
const DEFAULT_TARGET_EXTENSIONS = ['.js', '.mjs', '.ts', '.html'];
|
||||||
|
const DEFAULT_EXCLUDED_FILES = [];
|
||||||
|
|
||||||
function countInnerHTML(filePath) {
|
function countInnerHTML(filePath) {
|
||||||
try {
|
try {
|
||||||
@@ -17,33 +19,74 @@ function countInnerHTML(filePath) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function scanDirectory(dir) {
|
function main() {
|
||||||
let files = [];
|
const { values } = parseArgs({
|
||||||
try {
|
options: {
|
||||||
const items = fs.readdirSync(dir);
|
'exclude-dir': { type: 'string', multiple: true },
|
||||||
for (const item of items) {
|
'exclude-file': { type: 'string', multiple: true },
|
||||||
const fullPath = path.join(dir, item);
|
'no-defaults': { type: 'boolean' },
|
||||||
const stat = fs.statSync(fullPath);
|
'report': { type: 'boolean' },
|
||||||
|
'help': { type: 'boolean' },
|
||||||
|
},
|
||||||
|
strict: false,
|
||||||
|
});
|
||||||
|
|
||||||
if (stat.isDirectory()) {
|
if (values.help) {
|
||||||
if (!EXCLUDED_DIRS.includes(item)) {
|
console.log(`
|
||||||
files = files.concat(scanDirectory(fullPath));
|
Usage: check-innerhtml.mjs [options]
|
||||||
}
|
|
||||||
} else if (stat.isFile()) {
|
Options:
|
||||||
if (TARGET_EXTENSIONS.includes(path.extname(item))) {
|
--exclude-dir <dir> Exclude directory (can be used multiple times)
|
||||||
files.push(fullPath);
|
--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)
|
||||||
|
--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}`);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
return files;
|
||||||
console.error(`Error scanning ${dir}: ${error.message}`);
|
|
||||||
}
|
}
|
||||||
return files;
|
|
||||||
}
|
|
||||||
|
|
||||||
function main() {
|
const reportMode = values.report;
|
||||||
const args = process.argv.slice(2);
|
|
||||||
const reportMode = args.includes('--report');
|
|
||||||
|
|
||||||
const files = scanDirectory('.');
|
const files = scanDirectory('.');
|
||||||
let totalAssignments = 0;
|
let totalAssignments = 0;
|
||||||
|
|||||||
Reference in new issue
Block a user