/
githubmirror
/
angular-cli
Обзор
Документация
Войти
/
githubmirror
/
angular-cli
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/angular/build/src/utils/path.ts
66 строк
2 KB
Charles Lyding
refactor(@angular/build): remove unnecessary realpath resolution for workspace root
28 июл 2026, 11:00
28 июл 2026, 11:00
6c6bb21
Код
Авторство
О чём код?
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ import { isAbsolute, posix, relative, resolve } from 'node:path'; import { platform } from 'node:process'; const WINDOWS_PATH_SEPERATOR_REGEXP = /\\/g; /** * Converts a Windows-style file path to a POSIX-compliant path. * * This function replaces all backslashes (`\`) with forward slashes (`/`). * It is a no-op on POSIX systems (e.g., Linux, macOS), as the conversion * only runs on Windows (`win32`). * * @param path - The file path to convert. * @returns The POSIX-compliant file path. * * @example * ```ts * // On a Windows system: * toPosixPath('C:\\Users\\Test\\file.txt'); * // => 'C:/Users/Test/file.txt' * * // On a POSIX system (Linux/macOS): * toPosixPath('/home/user/file.txt'); * // => '/home/user/file.txt' * ``` */ export function toPosixPath(path: string): string { return platform === 'win32' ? path.replace(WINDOWS_PATH_SEPERATOR_REGEXP, posix.sep) : path; } /** * Determines if a path is a subdirectory or file within a parent directory. * * @param parent - The parent directory path. * @param child - The child path to check. * @returns `true` if the child path is within the parent directory, `false` otherwise. */ export function isSubDirectory(parent: string, child: string): boolean { const resolvedParent = resolve(parent); const resolvedChild = resolve(parent, child); const relativePath = toPosixPath(relative(resolvedParent, resolvedChild)); return relativePath !== '..' && !relativePath.startsWith('../') && !isAbsolute(relativePath); } /** * Canonicalizes a file path by normalising Windows drive-letter casing to uppercase. * * @param pathString - The file path to canonicalize. * @returns The canonicalized file path. */ export function canonicalizePath(pathString: string): string { if (platform === 'win32' && /^[a-z]:/.test(pathString)) { return pathString[0].toUpperCase() + pathString.slice(1); } return pathString; }