Replace path.relative with a helper function that factors in case sensitivity

This commit is contained in:
Ladislau Szomoru
2022-01-20 11:44:02 +01:00
parent f317abfd68
commit c3bd29b3d0
3 changed files with 27 additions and 26 deletions

View File

@@ -8,6 +8,7 @@ import { dirname, sep } from 'path';
import { Readable } from 'stream';
import { promises as fs, createReadStream } from 'fs';
import * as byline from 'byline';
import path = require('path');
export const isMacintosh = process.platform === 'darwin';
export const isWindows = process.platform === 'win32';
@@ -287,6 +288,16 @@ export function detectUnicodeEncoding(buffer: Buffer): Encoding | null {
return null;
}
function normalizePath(path: string): string {
// Windows & Mac are currently being handled
// as case insensitive file systems in VS Code.
if (isWindows || isMacintosh) {
return path.toLowerCase();
}
return path;
}
export function isDescendant(parent: string, descendant: string): boolean {
if (parent === descendant) {
return true;
@@ -296,25 +307,15 @@ export function isDescendant(parent: string, descendant: string): boolean {
parent += sep;
}
// Windows & Mac are currently being handled
// as case insensitive file systems in VS Code.
if (isWindows || isMacintosh) {
parent = parent.toLowerCase();
descendant = descendant.toLowerCase();
}
return descendant.startsWith(parent);
return normalizePath(descendant).startsWith(normalizePath(parent));
}
export function pathEquals(a: string, b: string): boolean {
// Windows & Mac are currently being handled
// as case insensitive file systems in VS Code.
if (isWindows || isMacintosh) {
a = a.toLowerCase();
b = b.toLowerCase();
}
return normalizePath(a) === normalizePath(b);
}
return a === b;
export function relativePath(from: string, to: string): string {
return path.relative(normalizePath(from), normalizePath(to));
}
export function* splitInChunks(array: string[], maxChunkLength: number): IterableIterator<string[]> {