@ts-common/azure-js-dev-tools
Version:
Developer dependencies for TypeScript related projects
134 lines • 4.78 kB
JavaScript
;
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getParentFolderPath = exports.getPathName = exports.getName = exports.isRooted = exports.getRootPath = exports.pathWithoutFileExtension = exports.normalizePath = exports.pathRelativeTo = exports.resolvePath = exports.joinPath = void 0;
var tslib_1 = require("tslib");
var path = tslib_1.__importStar(require("path"));
/**
* Join the provided path segments using a forward slash (/) as a path separator.
* @param pathSegments The path segments to resolve.
* @returns The resolved path.
*/
function joinPath() {
var _a;
var pathSegments = [];
for (var _i = 0; _i < arguments.length; _i++) {
pathSegments[_i] = arguments[_i];
}
return normalizePath((_a = path.posix).join.apply(_a, tslib_1.__spread(pathSegments)));
}
exports.joinPath = joinPath;
/**
* Resolve the provided path segments using a forward slash (/) as a path separator.
* @param pathSegments The path segments to resolve.
* @returns The resolved path.
*/
function resolvePath() {
var _a;
var pathSegments = [];
for (var _i = 0; _i < arguments.length; _i++) {
pathSegments[_i] = arguments[_i];
}
return normalizePath((_a = path.posix).resolve.apply(_a, tslib_1.__spread(pathSegments)));
}
exports.resolvePath = resolvePath;
/**
* Get the relative path from the provided basePath to the provided absolutePath. For example,
* pathRelativeTo("/my/path", "/") will return "my/path".
*/
function pathRelativeTo(absolutePath, basePath) {
var result = normalizePath(path.relative(normalizePath(basePath), normalizePath(absolutePath)));
if (result.endsWith("/..")) {
result += "/";
}
return result;
}
exports.pathRelativeTo = pathRelativeTo;
/**
* Replace all of the backslashes (\) with forward slashes (/), unless the provided osPlatform is
* win32. If the osPlatform is win32, then all forward slashes (/) will be replaced with backslahes
* (\).
* @param pathString The path to normalize.
* @returns The normalized path.
*/
function normalizePath(pathString, osPlatform) {
var result;
if (!pathString) {
result = pathString;
}
else if (osPlatform === "win32") {
result = pathString.replace(/\//g, "\\");
}
else {
result = pathString.replace(/\\/g, "/");
}
return result;
}
exports.normalizePath = normalizePath;
/**
* Return the provided path without its file extension.
* @param path The path.
*/
function pathWithoutFileExtension(path) {
var lastDot = path.lastIndexOf(".");
return lastDot === -1 ? path : path.substring(0, lastDot);
}
exports.pathWithoutFileExtension = pathWithoutFileExtension;
/**
* Get the root path of the provided path string. If the provided path string is relative (not
* rooted), then undefined will be returned.
* @param pathString The path to get the root of.
*/
function getRootPath(pathString) {
var result;
if (pathString) {
result = path.win32.parse(pathString).root || undefined;
if (!result) {
result = path.posix.parse(pathString).root || undefined;
}
}
return result;
}
exports.getRootPath = getRootPath;
/**
* Check whether or not the provided pathString is rooted (absolute).
* @param pathString The path to check.
* @returns Whether or not the provided pathString is rooted (absolute).
*/
function isRooted(pathString) {
return !!getRootPath(pathString);
}
exports.isRooted = isRooted;
/**
* Get the name/last segment of the provided path string.
* @param pathString The path to get the name/last segment of.
* @returns The name/last segment of the provided path string.
*/
function getName(pathString) {
return getPathName(pathString);
}
exports.getName = getName;
/**
* Get the name/last segment of the provided path string.
* @param pathString The path to get the name/last segment of.
* @returns The name/last segment of the provided path string.
*/
function getPathName(pathString) {
var lastSlashIndex = Math.max(pathString.lastIndexOf("/"), pathString.lastIndexOf("\\"));
return lastSlashIndex === -1 ? pathString : pathString.substring(lastSlashIndex + 1);
}
exports.getPathName = getPathName;
/**
* Get the path to the parent folder of the provided path string.
* @param pathString The path to the get the parent folder path of.
* @returns The path to the parent folder of the provided path string.
*/
function getParentFolderPath(pathString) {
return path.dirname(pathString);
}
exports.getParentFolderPath = getParentFolderPath;
//# sourceMappingURL=path.js.map