2024-03-22 13:08:26 +01:00
import { readdirSync , statSync } from 'fs' ;
import { join } from 'path' ;
const PROJECT _DIR = process . argv [ 2 ] ? ? '.' ;
const MAX _PATH _LENGTH = process . argv [ 3 ] ? ? 140 ;
const IS _CI = process . env . CI === 'true' ;
2024-03-22 14:03:02 +01:00
const IS _AZURE _PIPELINES = process . env . TF _BUILD === 'true' ;
2024-03-22 13:08:26 +01:00
const IS _GITHUB _ACTIONS = process . env . GITHUB _ACTIONS === 'true' ;
const FILE _PATH _COLOR = '\x1b[36m%s\x1b[0m' ;
2024-04-04 09:37:32 +02:00
const ERROR _COLOR = '\x1b[31m%s\x1b[0m' ;
const SUCCESS _COLOR = '\x1b[32m%s\x1b[0m' ;
const processExitCode = 1 ; // Default to 1 to fail the build, 0 to just log the issues
2024-03-22 13:08:26 +01:00
console . log ( ` Checking path length in ${ PROJECT _DIR } for paths exceeding ${ MAX _PATH _LENGTH } ... ` ) ;
console . log ( 'CI detected:' , IS _CI ) ;
console . log ( '\n-----------------------------------' ) ;
console . log ( 'Results:' ) ;
console . log ( '-----------------------------------\n' ) ;
function checkPathLength ( dir ) {
const files = readdirSync ( dir ) ;
2024-04-04 09:37:32 +02:00
let hasError = false ;
2024-03-22 13:08:26 +01:00
files . forEach ( file => {
const filePath = join ( dir , file ) ;
if ( filePath . length > MAX _PATH _LENGTH ) {
2024-04-04 09:37:32 +02:00
hasError = true ;
2024-03-22 13:08:26 +01:00
if ( IS _AZURE _PIPELINES ) {
2024-07-03 12:05:08 +02:00
console . error ( ` ##vso[task.logissue type=error;sourcepath= ${ filePath } ;]Path exceeds maximum length of ${ MAX _PATH _LENGTH } characters: ${ filePath } with ${ filePath . length } characters ` ) ;
2024-03-22 13:08:26 +01:00
} else if ( IS _GITHUB _ACTIONS ) {
2024-07-03 12:05:08 +02:00
console . error ( ` ::error file= ${ filePath } ,title=Path exceeds ${ MAX _PATH _LENGTH } characters::Paths should not be longer than ${ MAX _PATH _LENGTH } characters to support WIN32 systems. The file ${ filePath } exceeds that with ${ filePath . length - MAX _PATH _LENGTH } characters. ` ) ;
2024-03-22 13:08:26 +01:00
} else {
2024-03-22 13:47:40 +01:00
console . error ( ` Path exceeds maximum length of ${ MAX _PATH _LENGTH } characters: ${ FILE _PATH _COLOR } ` , filePath , filePath . length - MAX _PATH _LENGTH ) ;
2024-03-22 13:08:26 +01:00
}
}
if ( statSync ( filePath ) . isDirectory ( ) ) {
2024-04-04 09:37:32 +02:00
const subHasError = checkPathLength ( filePath ) ;
if ( subHasError ) {
hasError = true ;
}
2024-03-22 13:08:26 +01:00
}
} ) ;
2024-04-04 09:37:32 +02:00
return hasError ;
2024-03-22 13:08:26 +01:00
}
2024-04-04 09:37:32 +02:00
const hasError = checkPathLength ( PROJECT _DIR , MAX _PATH _LENGTH ) ;
if ( hasError ) {
console . error ( '\n-----------------------------------' ) ;
console . error ( ERROR _COLOR , 'Path length check failed' ) ;
console . error ( '-----------------------------------\n' ) ;
2024-04-04 09:37:54 +02:00
if ( IS _CI && processExitCode ) {
2024-04-04 09:37:32 +02:00
process . exit ( processExitCode ) ;
}
} else {
console . log ( '\n-----------------------------------' ) ;
console . log ( SUCCESS _COLOR , 'Path length check passed' ) ;
console . log ( '-----------------------------------\n' ) ;
}