summaryrefslogtreecommitdiff
path: root/packages/cli/src/utils/paths.ts
diff options
context:
space:
mode:
authorTaylor Mullen <[email protected]>2025-04-17 18:06:21 -0400
committerN. Taylor Mullen <[email protected]>2025-04-17 15:29:34 -0700
commitcfc697a96d2e716a75e1c3b7f0f34fce81abaf1e (patch)
treee06bcba67ca71a874048aa887b17457dbd409bdf /packages/cli/src/utils/paths.ts
parent7928c1727f0b208ed34850cc89bbb36ea3dd23e5 (diff)
Run `npm run format`
- Also updated README.md accordingly. Part of https://b.corp.google.com/issues/411384603
Diffstat (limited to 'packages/cli/src/utils/paths.ts')
-rw-r--r--packages/cli/src/utils/paths.ts124
1 files changed, 63 insertions, 61 deletions
diff --git a/packages/cli/src/utils/paths.ts b/packages/cli/src/utils/paths.ts
index a5368463..60762c9d 100644
--- a/packages/cli/src/utils/paths.ts
+++ b/packages/cli/src/utils/paths.ts
@@ -5,7 +5,7 @@ import path from 'node:path'; // Import the 'path' module
* Returns the target directory, using the provided argument or the current working directory.
*/
export function getTargetDirectory(targetDirArg: string | undefined): string {
- return targetDirArg || process.cwd();
+ return targetDirArg || process.cwd();
}
/**
@@ -13,73 +13,72 @@ export function getTargetDirectory(targetDirArg: string | undefined): string {
* 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;
- }
+ if (filePath.length <= maxLen) {
+ return filePath;
+ }
- const parsedPath = path.parse(filePath);
- const root = parsedPath.root;
- const separator = path.sep;
+ 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
+ // 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}`;
+ // 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 firstDir = segments[0];
+ const startComponent = root + firstDir;
- const endPartSegments: string[] = [];
- // Base length: startComponent + separator + "..."
- let currentLength = startComponent.length + separator.length + 3;
+ 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;
+ // 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;
- }
+ 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);
- }
+ // 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}`;
+ // 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;
+ return result;
}
/**
@@ -91,12 +90,15 @@ export function shortenPath(filePath: string, maxLen: number = 35): string {
* @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);
+export function makeRelative(
+ targetPath: string,
+ rootDirectory: string,
+): string {
+ const resolvedTargetPath = path.resolve(targetPath);
+ const resolvedRootDirectory = path.resolve(rootDirectory);
- const relativePath = path.relative(resolvedRootDirectory, resolvedTargetPath);
+ const relativePath = path.relative(resolvedRootDirectory, resolvedTargetPath);
- // If the paths are the same, path.relative returns '', return '.' instead
- return relativePath || '.';
+ // If the paths are the same, path.relative returns '', return '.' instead
+ return relativePath || '.';
}