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' ) ;
2024-07-25 17:40:12 +02:00
const hasError = checkPathLength ( PROJECT _DIR ) ;
2024-07-03 15:54:52 +02:00
if ( hasError ) {
console . log ( '\n-----------------------------------' ) ;
console . log ( ERROR _COLOR , 'Path length check failed' ) ;
console . log ( '-----------------------------------\n' ) ;
if ( IS _CI && processExitCode ) {
process . exit ( processExitCode ) ;
}
} else {
console . log ( '\n-----------------------------------' ) ;
console . log ( SUCCESS _COLOR , 'Path length check passed' ) ;
console . log ( '-----------------------------------\n' ) ;
}
// Functions
/ * *
* Recursively check the path length of all files in a directory .
* @ param { string } dir - The directory to check for path lengths
* @ returns { boolean }
* /
2024-03-22 13:08:26 +01:00
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:59:11 +02:00
console . error ( ` ##vso[task.logissue type=error;sourcepath= ${ mapFileToSourcePath ( 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:59:11 +02:00
console . error ( ` ::error file= ${ mapFileToSourcePath ( 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-07-03 15:54:52 +02:00
console . error ( FILE _PATH _COLOR , mapFileToSourcePath ( filePath ) , '(exceeds by' , filePath . length - MAX _PATH _LENGTH , 'chars)' ) ;
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-07-03 12:59:11 +02:00
/ * *
* Maps a file path to a source path for CI logs .
* @ remark This might not always work as expected , especially on bundled files , but it ' s a best effort to map the file path to a source path .
* @ param { string } file - The file path to map to a source path
* @ returns { string }
* /
function mapFileToSourcePath ( file ) {
2024-07-03 15:54:52 +02:00
return file . replace ( PROJECT _DIR , 'src' ) . replace ( '.js' , '.ts' ) ;
2024-04-04 09:37:32 +02:00
}