UNPKG

5.55 kBJavaScriptView Raw
1/*---------------------------------------------------------------------------------------------
2 * Copyright (c) Microsoft Corporation. All rights reserved.
3 * Licensed under the MIT License. See License.txt in the project root for license information.
4 *--------------------------------------------------------------------------------------------*/
5'use strict';
6var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7 if (k2 === undefined) k2 = k;
8 var desc = Object.getOwnPropertyDescriptor(m, k);
9 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10 desc = { enumerable: true, get: function() { return m[k]; } };
11 }
12 Object.defineProperty(o, k2, desc);
13}) : (function(o, m, k, k2) {
14 if (k2 === undefined) k2 = k;
15 o[k2] = m[k];
16}));
17var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
18 Object.defineProperty(o, "default", { enumerable: true, value: v });
19}) : function(o, v) {
20 o["default"] = v;
21});
22var __importStar = (this && this.__importStar) || function (mod) {
23 if (mod && mod.__esModule) return mod;
24 var result = {};
25 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
26 __setModuleDefault(result, mod);
27 return result;
28};
29Object.defineProperty(exports, "__esModule", { value: true });
30exports.Utils = void 0;
31const nodePath = __importStar(require("path"));
32const posixPath = nodePath.posix || nodePath;
33const slash = '/';
34var Utils;
35(function (Utils) {
36 /**
37 * Joins one or more input paths to the path of URI.
38 * '/' is used as the directory separation character.
39 *
40 * The resolved path will be normalized. That means:
41 * - all '..' and '.' segments are resolved.
42 * - multiple, sequential occurences of '/' are replaced by a single instance of '/'.
43 * - trailing separators are preserved.
44 *
45 * @param uri The input URI.
46 * @param paths The paths to be joined with the path of URI.
47 * @returns A URI with the joined path. All other properties of the URI (scheme, authority, query, fragments, ...) will be taken from the input URI.
48 */
49 function joinPath(uri, ...paths) {
50 return uri.with({ path: posixPath.join(uri.path, ...paths) });
51 }
52 Utils.joinPath = joinPath;
53 /**
54 * Resolves one or more paths against the path of a URI.
55 * '/' is used as the directory separation character.
56 *
57 * The resolved path will be normalized. That means:
58 * - all '..' and '.' segments are resolved.
59 * - multiple, sequential occurences of '/' are replaced by a single instance of '/'.
60 * - trailing separators are removed.
61 *
62 * @param uri The input URI.
63 * @param paths The paths to resolve against the path of URI.
64 * @returns A URI with the resolved path. All other properties of the URI (scheme, authority, query, fragments, ...) will be taken from the input URI.
65 */
66 function resolvePath(uri, ...paths) {
67 let path = uri.path;
68 let slashAdded = false;
69 if (path[0] !== slash) {
70 path = slash + path; // make the path abstract: for posixPath.resolve the first segments has to be absolute or cwd is used.
71 slashAdded = true;
72 }
73 let resolvedPath = posixPath.resolve(path, ...paths);
74 if (slashAdded && resolvedPath[0] === slash && !uri.authority) {
75 resolvedPath = resolvedPath.substring(1);
76 }
77 return uri.with({ path: resolvedPath });
78 }
79 Utils.resolvePath = resolvePath;
80 /**
81 * Returns a URI where the path is the directory name of the input uri, similar to the Unix dirname command.
82 * In the path, '/' is recognized as the directory separation character. Trailing directory separators are ignored.
83 * The orignal URI is returned if the URIs path is empty or does not contain any path segments.
84 *
85 * @param uri The input URI.
86 * @return The last segment of the URIs path.
87 */
88 function dirname(uri) {
89 if (uri.path.length === 0 || uri.path === slash) {
90 return uri;
91 }
92 let path = posixPath.dirname(uri.path);
93 if (path.length === 1 && path.charCodeAt(0) === 46 /* CharCode.Period */) {
94 path = '';
95 }
96 return uri.with({ path });
97 }
98 Utils.dirname = dirname;
99 /**
100 * Returns the last segment of the path of a URI, similar to the Unix basename command.
101 * In the path, '/' is recognized as the directory separation character. Trailing directory separators are ignored.
102 * The empty string is returned if the URIs path is empty or does not contain any path segments.
103 *
104 * @param uri The input URI.
105 * @return The base name of the URIs path.
106 */
107 function basename(uri) {
108 return posixPath.basename(uri.path);
109 }
110 Utils.basename = basename;
111 /**
112 * Returns the extension name of the path of a URI, similar to the Unix extname command.
113 * In the path, '/' is recognized as the directory separation character. Trailing directory separators are ignored.
114 * The empty string is returned if the URIs path is empty or does not contain any path segments.
115 *
116 * @param uri The input URI.
117 * @return The extension name of the URIs path.
118 */
119 function extname(uri) {
120 return posixPath.extname(uri.path);
121 }
122 Utils.extname = extname;
123})(Utils || (exports.Utils = Utils = {}));