( pathValue: string, directories: string[] )
| 4 | * Prepends path entries while preserving existing entries and order. |
| 5 | */ |
| 6 | export function prependPathEntries( |
| 7 | pathValue: string, |
| 8 | directories: string[] |
| 9 | ): string { |
| 10 | const pathParts = pathValue.split(path.delimiter).filter(Boolean); |
| 11 | const prepended: string[] = []; |
| 12 | |
| 13 | for (const directory of directories) { |
| 14 | if (!pathParts.includes(directory) && !prepended.includes(directory)) { |
| 15 | prepended.push(directory); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | if (prepended.length === 0) { |
| 20 | return pathValue; |
| 21 | } |
| 22 | |
| 23 | return pathValue === '' || pathValue === path.delimiter |
| 24 | ? `${prepended.join(path.delimiter)}${pathValue}` |
| 25 | : [...prepended, pathValue].join(path.delimiter); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Splits a PATH value into non-empty directory entries. |
no test coverage detected