|
5 | 5 | * but the paths in reports need to match the actual repository structure |
6 | 6 | * for GitHub annotations and file references to work correctly. |
7 | 7 | */ |
| 8 | +const SLASH_CHAR_CODE = "/".charCodeAt(0); |
| 9 | + |
| 10 | +const trimTrailingSlashes = (value) => { |
| 11 | + if (!value) { |
| 12 | + return value; |
| 13 | + } |
| 14 | + |
| 15 | + let end = value.length; |
| 16 | + while (end > 0 && value.charCodeAt(end - 1) === SLASH_CHAR_CODE) { |
| 17 | + end -= 1; |
| 18 | + } |
| 19 | + |
| 20 | + return value.slice(0, end); |
| 21 | +}; |
| 22 | + |
| 23 | +const removeSingleLeadingSlash = (value) => { |
| 24 | + if (!value || value.charCodeAt(0) !== SLASH_CHAR_CODE) { |
| 25 | + return value; |
| 26 | + } |
| 27 | + |
| 28 | + return value.slice(1); |
| 29 | +}; |
| 30 | + |
8 | 31 | export class PathRewriter { |
9 | 32 | /** |
10 | 33 | * Create a path rewriter |
11 | | - * @param {string|null} pathMapping - Path mapping in format "from:to" or null to disable |
| 34 | + * @param {string|null} pathMapping - Path mapping(s) in format "from:to" or multiple mappings separated by newlines/commas |
12 | 35 | */ |
13 | 36 | constructor(pathMapping = null) { |
14 | | - this.fromPath = null; |
15 | | - this.toPath = null; |
| 37 | + this.mappings = []; |
16 | 38 |
|
17 | 39 | if (pathMapping && pathMapping.trim().length > 0) { |
18 | | - this._parsePathMapping(pathMapping); |
| 40 | + this._parsePathMappings(pathMapping); |
19 | 41 | } |
20 | 42 | } |
21 | 43 |
|
22 | 44 | /** |
23 | | - * Parse path mapping string |
24 | | - * @param {string} pathMapping - Path mapping in format "from:to" |
| 45 | + * Parse path mapping string(s) |
| 46 | + * @param {string} pathMapping - Path mapping(s) in format "from:to", can be multiple separated by newlines or commas |
25 | 47 | * @private |
26 | 48 | */ |
27 | | - _parsePathMapping(pathMapping) { |
28 | | - const parts = pathMapping.split(":"); |
29 | | - if (parts.length !== 2) { |
30 | | - throw new Error( |
31 | | - `Invalid path-mapping format: "${pathMapping}". Expected format: "from_path:to_path"`, |
32 | | - ); |
33 | | - } |
| 49 | + _parsePathMappings(pathMapping) { |
| 50 | + // Split by newlines or commas, filter empty strings |
| 51 | + const lines = pathMapping |
| 52 | + .split(/[\n,]/) |
| 53 | + .map((line) => line.trim()) |
| 54 | + .filter((line) => line.length > 0); |
34 | 55 |
|
35 | | - this.fromPath = parts[0].trim(); |
36 | | - this.toPath = parts[1].trim(); |
| 56 | + for (const line of lines) { |
| 57 | + const parts = line.split(":"); |
| 58 | + if (parts.length !== 2) { |
| 59 | + throw new Error( |
| 60 | + `Invalid path-mapping format: "${line}". Expected format: "from_path:to_path"`, |
| 61 | + ); |
| 62 | + } |
37 | 63 |
|
38 | | - if (this.fromPath.length === 0 || this.toPath.length === 0) { |
39 | | - throw new Error( |
40 | | - `Invalid path-mapping format: "${pathMapping}". Paths cannot be empty.`, |
41 | | - ); |
42 | | - } |
| 64 | + const fromPath = parts[0].trim(); |
| 65 | + const toPath = parts[1].trim(); |
| 66 | + |
| 67 | + if (fromPath.length === 0 || toPath.length === 0) { |
| 68 | + throw new Error( |
| 69 | + `Invalid path-mapping format: "${line}". Paths cannot be empty.`, |
| 70 | + ); |
| 71 | + } |
43 | 72 |
|
44 | | - // Normalize paths - remove trailing slashes for consistency |
45 | | - this.fromPath = this.fromPath.replace(/\/+$/, ""); |
46 | | - this.toPath = this.toPath.replace(/\/+$/, ""); |
| 73 | + // Normalize paths - remove trailing slashes for consistency |
| 74 | + this.mappings.push({ |
| 75 | + from: trimTrailingSlashes(fromPath), |
| 76 | + to: trimTrailingSlashes(toPath), |
| 77 | + }); |
| 78 | + } |
47 | 79 | } |
48 | 80 |
|
49 | 81 | /** |
50 | 82 | * Check if path rewriting is enabled |
51 | 83 | * @returns {boolean} True if path mapping is configured |
52 | 84 | */ |
53 | 85 | isEnabled() { |
54 | | - return this.fromPath !== null && this.toPath !== null; |
| 86 | + return this.mappings.length > 0; |
55 | 87 | } |
56 | 88 |
|
57 | 89 | /** |
58 | | - * Rewrite a file path |
| 90 | + * Rewrite a file path using the first matching mapping |
59 | 91 | * @param {string} path - Original path from report |
60 | | - * @returns {string} Rewritten path or original if no mapping configured |
| 92 | + * @returns {string} Rewritten path or original if no mapping matches |
61 | 93 | */ |
62 | 94 | rewritePath(path) { |
63 | 95 | if (!this.isEnabled() || !path) { |
64 | 96 | return path; |
65 | 97 | } |
66 | 98 |
|
67 | | - // Handle both absolute and relative paths |
68 | | - let normalizedPath = path; |
| 99 | + // Try each mapping in order until one matches |
| 100 | + for (const mapping of this.mappings) { |
| 101 | + const result = this._applyMapping(path, mapping); |
| 102 | + if (result !== path) { |
| 103 | + // Mapping was applied |
| 104 | + return result; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // No mapping matched, return original |
| 109 | + return path; |
| 110 | + } |
69 | 111 |
|
| 112 | + /** |
| 113 | + * Apply a single mapping to a path |
| 114 | + * @param {string} path - Original path |
| 115 | + * @param {Object} mapping - Mapping object with from and to properties |
| 116 | + * @returns {string} Rewritten path or original if mapping doesn't match |
| 117 | + * @private |
| 118 | + */ |
| 119 | + _applyMapping(path, mapping) { |
70 | 120 | // If path starts with fromPath, replace it |
71 | | - if (normalizedPath.startsWith(this.fromPath)) { |
72 | | - normalizedPath = |
73 | | - this.toPath + normalizedPath.substring(this.fromPath.length); |
| 121 | + if (path.startsWith(mapping.from)) { |
| 122 | + return mapping.to + path.substring(mapping.from.length); |
74 | 123 | } |
75 | | - // Also handle case where fromPath has leading slash but path doesn't |
76 | | - else if (normalizedPath.startsWith(this.fromPath.replace(/^\//, ""))) { |
77 | | - const fromPathNoLeadingSlash = this.fromPath.replace(/^\//, ""); |
78 | | - normalizedPath = |
79 | | - this.toPath + normalizedPath.substring(fromPathNoLeadingSlash.length); |
| 124 | + |
| 125 | + if (mapping.from.startsWith("/")) { |
| 126 | + const fromPathNoLeadingSlash = removeSingleLeadingSlash(mapping.from); |
| 127 | + if (path.startsWith(fromPathNoLeadingSlash)) { |
| 128 | + return mapping.to + path.substring(fromPathNoLeadingSlash.length); |
| 129 | + } |
80 | 130 | } |
81 | 131 |
|
82 | | - return normalizedPath; |
| 132 | + return path; |
83 | 133 | } |
84 | 134 |
|
85 | 135 | /** |
86 | 136 | * Get info about the path mapping configuration |
87 | | - * @returns {Object|null} Object with from and to paths, or null if not enabled |
| 137 | + * @returns {Array|null} Array of mapping objects with from and to properties, or null if not enabled |
88 | 138 | */ |
89 | | - getMapping() { |
| 139 | + getMappings() { |
90 | 140 | if (!this.isEnabled()) { |
91 | 141 | return null; |
92 | 142 | } |
93 | 143 |
|
94 | | - return { |
95 | | - from: this.fromPath, |
96 | | - to: this.toPath, |
97 | | - }; |
| 144 | + return this.mappings.map((m) => ({ |
| 145 | + from: m.from, |
| 146 | + to: m.to, |
| 147 | + })); |
98 | 148 | } |
99 | 149 | } |
0 commit comments