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

81 lines
2.9 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');
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}
*/
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) {
console.error(`##vso[task.logissue type=error;sourcepath=${mapFileToSourcePath(filePath)};]Path exceeds maximum length of ${MAX_PATH_LENGTH} characters: ${filePath} with ${filePath.length} characters`);
} else if (IS_GITHUB_ACTIONS) {
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.`);
} else {
2024-07-03 15:54:52 +02:00
console.error(FILE_PATH_COLOR, mapFileToSourcePath(filePath), '(exceeds by', filePath.length - MAX_PATH_LENGTH, 'chars)');
}
}
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;
}
/**
* 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
}