Files
Umbraco-CMS/src/Umbraco.Web.UI.Client/devops/build/check-path-length.js

64 lines
2.3 KiB
JavaScript
Raw Normal View History

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';
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
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;
files.forEach(file => {
const filePath = join(dir, file);
if (filePath.length > MAX_PATH_LENGTH) {
2024-04-04 09:37:32 +02:00
hasError = true;
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`);
} 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.`);
} 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);
}
}
if (statSync(filePath).isDirectory()) {
2024-04-04 09:37:32 +02:00
const subHasError = checkPathLength(filePath);
if (subHasError) {
hasError = true;
}
}
});
2024-04-04 09:37:32 +02:00
return hasError;
}
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');
}