diff options
| author | Evan Senter <[email protected]> | 2025-04-19 19:45:42 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-04-19 19:45:42 +0100 |
| commit | 3fce6cea27d3e6129d6c06e528b62e1b11bf7094 (patch) | |
| tree | 244b8e9ab94f902d65d4bda8739a6538e377ed17 /packages/cli/src/utils/paths.ts | |
| parent | 0c9e1ef61be7db53e6e73b7208b649cd8cbed6c3 (diff) | |
Starting to modularize into separate cli / server packages. (#55)
* Starting to move a lot of code into packages/server
* More of the massive refactor, builds and runs, some issues though.
* Fixing outstanding issue with double messages.
* Fixing a minor UI issue.
* Fixing the build post-merge.
* Running formatting.
* Addressing comments.
Diffstat (limited to 'packages/cli/src/utils/paths.ts')
| -rw-r--r-- | packages/cli/src/utils/paths.ts | 102 |
1 files changed, 0 insertions, 102 deletions
diff --git a/packages/cli/src/utils/paths.ts b/packages/cli/src/utils/paths.ts deleted file mode 100644 index f1a42131..00000000 --- a/packages/cli/src/utils/paths.ts +++ /dev/null @@ -1,102 +0,0 @@ -/** - * @license - * Copyright 2025 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -import path from 'node:path'; // Import the 'path' module - -/** - * Shortens a path string if it exceeds maxLen, prioritizing the start and end segments. - * Example: /path/to/a/very/long/file.txt -> /path/.../long/file.txt - */ -export function shortenPath(filePath: string, maxLen: number = 35): string { - if (filePath.length <= maxLen) { - return filePath; - } - - const parsedPath = path.parse(filePath); - const root = parsedPath.root; - const separator = path.sep; - - // Get segments of the path *after* the root - const relativePath = filePath.substring(root.length); - const segments = relativePath.split(separator).filter((s) => s !== ''); // Filter out empty segments - - // Handle cases with no segments after root (e.g., "/", "C:\") or only one segment - if (segments.length <= 1) { - // Fallback to simple start/end truncation for very short paths or single segments - const keepLen = Math.floor((maxLen - 3) / 2); - // Ensure keepLen is not negative if maxLen is very small - if (keepLen <= 0) { - return filePath.substring(0, maxLen - 3) + '...'; - } - const start = filePath.substring(0, keepLen); - const end = filePath.substring(filePath.length - keepLen); - return `${start}...${end}`; - } - - const firstDir = segments[0]; - const startComponent = root + firstDir; - - const endPartSegments: string[] = []; - // Base length: startComponent + separator + "..." - let currentLength = startComponent.length + separator.length + 3; - - // Iterate backwards through segments (excluding the first one) - for (let i = segments.length - 1; i >= 1; i--) { - const segment = segments[i]; - // Length needed if we add this segment: current + separator + segment - const lengthWithSegment = currentLength + separator.length + segment.length; - - if (lengthWithSegment <= maxLen) { - endPartSegments.unshift(segment); // Add to the beginning of the end part - currentLength = lengthWithSegment; - } else { - // Adding this segment would exceed maxLen - break; - } - } - - // Construct the final path - let result = startComponent + separator + '...'; - if (endPartSegments.length > 0) { - result += separator + endPartSegments.join(separator); - } - - // As a final check, if the result is somehow still too long (e.g., startComponent + ... is too long) - // fallback to simple truncation of the original path - if (result.length > maxLen) { - const keepLen = Math.floor((maxLen - 3) / 2); - if (keepLen <= 0) { - return filePath.substring(0, maxLen - 3) + '...'; - } - const start = filePath.substring(0, keepLen); - const end = filePath.substring(filePath.length - keepLen); - return `${start}...${end}`; - } - - return result; -} - -/** - * Calculates the relative path from a root directory to a target path. - * Ensures both paths are resolved before calculating. - * Returns '.' if the target path is the same as the root directory. - * - * @param targetPath The absolute or relative path to make relative. - * @param rootDirectory The absolute path of the directory to make the target path relative to. - * @returns The relative path from rootDirectory to targetPath. - */ -export function makeRelative( - targetPath: string, - rootDirectory: string, -): string { - const resolvedTargetPath = path.resolve(targetPath); - const resolvedRootDirectory = path.resolve(rootDirectory); - - const relativePath = path.relative(resolvedRootDirectory, resolvedTargetPath); - - // If the paths are the same, path.relative returns '', return '.' instead - return relativePath || '.'; -} |
