1- #!/usr/bin/env node
21/**
32 * @fileoverview Validates that no individual files exceed size threshold.
43 *
87 * - Excludes: node_modules, .git, dist, build, coverage directories
98 */
109
11- import { promises as fs } from 'node:fs' ;
12- import path from 'node:path' ;
13- import { fileURLToPath } from 'node:url' ;
14- import loggerPkg from '@socketsecurity/lib/logger' ;
10+ import { promises as fs } from 'node:fs'
11+ import path from 'node:path'
12+ import { fileURLToPath } from 'node:url'
1513
16- const logger = loggerPkg . getDefaultLogger ( ) ;
14+ import loggerPkg from '@socketsecurity/lib/logger'
1715
18- const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
19- const rootPath = path . join ( __dirname , '..' ) ;
16+ const logger = loggerPkg . getDefaultLogger ( )
2017
21- // Maximum file size: 2MB
22- const MAX_FILE_SIZE = 2 * 1024 * 1024 ; // 2,097,152 bytes
18+ const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) )
19+ const rootPath = path . join ( __dirname , '..' )
20+
21+ // Maximum file size: 2MB (2,097,152 bytes)
22+ const MAX_FILE_SIZE = 2 * 1024 * 1024
2323
2424// Directories to skip
2525const SKIP_DIRS = new Set ( [
@@ -36,28 +36,30 @@ const SKIP_DIRS = new Set([
3636 '.vercel' ,
3737 '.vscode' ,
3838 'tmp' ,
39- ] ) ;
39+ ] )
4040
4141/**
4242 * Format bytes to human-readable size.
4343 */
4444function formatBytes ( bytes ) {
45- if ( bytes === 0 ) return '0 B' ;
46- const k = 1024 ;
47- const sizes = [ 'B' , 'KB' , 'MB' , 'GB' ] ;
48- const i = Math . floor ( Math . log ( bytes ) / Math . log ( k ) ) ;
49- return `${ ( bytes / Math . pow ( k , i ) ) . toFixed ( 2 ) } ${ sizes [ i ] } ` ;
45+ if ( bytes === 0 ) {
46+ return '0 B'
47+ }
48+ const k = 1024
49+ const sizes = [ 'B' , 'KB' , 'MB' , 'GB' ]
50+ const i = Math . floor ( Math . log ( bytes ) / Math . log ( k ) )
51+ return `${ ( bytes / k ** i ) . toFixed ( 2 ) } ${ sizes [ i ] } `
5052}
5153
5254/**
5355 * Recursively scan directory for files exceeding size limit.
5456 */
5557async function scanDirectory ( dir , violations = [ ] ) {
5658 try {
57- const entries = await fs . readdir ( dir , { withFileTypes : true } ) ;
59+ const entries = await fs . readdir ( dir , { withFileTypes : true } )
5860
5961 for ( const entry of entries ) {
60- const fullPath = path . join ( dir , entry . name ) ;
62+ const fullPath = path . join ( dir , entry . name )
6163
6264 if ( entry . isDirectory ( ) ) {
6365 // Skip excluded directories and hidden directories (except .claude, .config, .github)
@@ -68,19 +70,19 @@ async function scanDirectory(dir, violations = []) {
6870 entry . name === '.config' ||
6971 entry . name === '.github' )
7072 ) {
71- await scanDirectory ( fullPath , violations ) ;
73+ await scanDirectory ( fullPath , violations )
7274 }
7375 } else if ( entry . isFile ( ) ) {
7476 try {
75- const stats = await fs . stat ( fullPath ) ;
77+ const stats = await fs . stat ( fullPath )
7678 if ( stats . size > MAX_FILE_SIZE ) {
77- const relativePath = path . relative ( rootPath , fullPath ) ;
79+ const relativePath = path . relative ( rootPath , fullPath )
7880 violations . push ( {
7981 file : relativePath ,
8082 size : stats . size ,
8183 formattedSize : formatBytes ( stats . size ) ,
8284 maxSize : formatBytes ( MAX_FILE_SIZE ) ,
83- } ) ;
85+ } )
8486 }
8587 } catch {
8688 // Skip files we can't stat
@@ -91,58 +93,60 @@ async function scanDirectory(dir, violations = []) {
9193 // Skip directories we can't read
9294 }
9395
94- return violations ;
96+ return violations
9597}
9698
9799/**
98100 * Validate file sizes in repository.
99101 */
100102async function validateFileSizes ( ) {
101- const violations = await scanDirectory ( rootPath ) ;
103+ const violations = await scanDirectory ( rootPath )
102104
103105 // Sort by size descending (largest first)
104- violations . sort ( ( a , b ) => b . size - a . size ) ;
106+ violations . sort ( ( a , b ) => b . size - a . size )
105107
106- return violations ;
108+ return violations
107109}
108110
109111async function main ( ) {
110112 try {
111- const violations = await validateFileSizes ( ) ;
113+ const violations = await validateFileSizes ( )
112114
113115 if ( violations . length === 0 ) {
114- logger . success ( 'All files are within size limits' ) ;
115- process . exitCode = 0 ;
116- return ;
116+ logger . success ( 'All files are within size limits' )
117+ process . exitCode = 0
118+ return
117119 }
118120
119- logger . fail ( 'File size violations found' ) ;
120- logger . log ( '' ) ;
121- logger . log ( `Maximum allowed file size: ${ formatBytes ( MAX_FILE_SIZE ) } ` ) ;
122- logger . log ( '' ) ;
123- logger . log ( 'Files exceeding limit:' ) ;
124- logger . log ( '' ) ;
121+ logger . fail ( 'File size violations found' )
122+ logger . log ( '' )
123+ logger . log ( `Maximum allowed file size: ${ formatBytes ( MAX_FILE_SIZE ) } ` )
124+ logger . log ( '' )
125+ logger . log ( 'Files exceeding limit:' )
126+ logger . log ( '' )
125127
126128 for ( const violation of violations ) {
127- logger . log ( ` ${ violation . file } ` ) ;
128- logger . log ( ` Size: ${ violation . formattedSize } ` ) ;
129- logger . log ( ` Exceeds limit by: ${ formatBytes ( violation . size - MAX_FILE_SIZE ) } ` ) ;
130- logger . log ( '' ) ;
129+ logger . log ( ` ${ violation . file } ` )
130+ logger . log ( ` Size: ${ violation . formattedSize } ` )
131+ logger . log (
132+ ` Exceeds limit by: ${ formatBytes ( violation . size - MAX_FILE_SIZE ) } ` ,
133+ )
134+ logger . log ( '' )
131135 }
132136
133137 logger . log (
134138 'Reduce file sizes, move large files to external storage, or exclude from repository.' ,
135- ) ;
136- logger . log ( '' ) ;
139+ )
140+ logger . log ( '' )
137141
138- process . exitCode = 1 ;
142+ process . exitCode = 1
139143 } catch ( error ) {
140- logger . fail ( `Validation failed: ${ error . message } ` ) ;
141- process . exitCode = 1 ;
144+ logger . fail ( `Validation failed: ${ error . message } ` )
145+ process . exitCode = 1
142146 }
143147}
144148
145149main ( ) . catch ( error => {
146- logger . fail ( `Validation failed: ${ error } ` ) ;
147- process . exitCode = 1 ;
148- } ) ;
150+ logger . fail ( `Validation failed: ${ error } ` )
151+ process . exitCode = 1
152+ } )
0 commit comments