File size: 6,776 Bytes
84c1942 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | /* eslint-disable no-param-reassign */
/* eslint-disable no-continue */
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/;
function splitPath(filename: string) {
return splitPathRe.exec(filename).slice(1);
}
// resolves . and .. elements in a path array with directory names there
// must be no slashes or device names (c:\) in the array
// (so also no leading and trailing slashes - it does not distinguish
// relative and absolute paths)
function normalizeArray(parts: string[], allowAboveRoot?: boolean) {
const res = [];
for (let i = 0; i < parts.length; i += 1) {
const p = parts[i];
// ignore empty parts
if (!p || p === '.') continue; // eslint-disable-line no-continue
if (p === '..') {
if (res.length && res[res.length - 1] !== '..') {
res.pop();
} else if (allowAboveRoot) {
res.push('..');
}
} else {
res.push(p);
}
}
return res;
}
export function isAbsolute(path: string) {
return path.charAt(0) === '/';
}
export function normalize(path: string) {
const isAbs = isAbsolute(path);
const trailingSlash = path && path[path.length - 1] === '/';
let newPath = path;
// Normalize the path
newPath = normalizeArray(newPath.split('/'), !isAbs).join('/');
if (!newPath && !isAbs) {
newPath = '.';
}
if (newPath && trailingSlash) {
newPath += '/';
}
return (isAbs ? '/' : '') + newPath;
}
export function join(...paths: Array<any>) {
let path = '';
for (let i = 0; i < paths.length; i += 1) {
const segment = paths[i];
if (typeof segment !== 'string') {
throw new TypeError('Arguments to path.join must be strings');
}
if (segment) {
if (!path) {
path += segment;
} else {
path += `/${segment}`;
}
}
}
return normalize(path);
}
export function dirname(path: string) {
const result = splitPath(path);
const root = result[0];
let dir = result[1];
if (!root && !dir) {
// No dirname whatsoever
return '.';
}
if (dir) {
// It has a dirname, strip trailing slash
dir = dir.substr(0, dir.length - 1);
}
return root + dir;
}
export function basename(p: string, ext: string = '') {
// Special case: Normalize will modify this to '.'
if (p === '') {
return p;
}
// Normalize the string first to remove any weirdness.
const path = normalize(p);
// Get the last part of the string.
const sections = path.split('/');
const lastPart = sections[sections.length - 1];
// Special case: If it's empty, then we have a string like so: foo/
// Meaning, 'foo' is guaranteed to be a directory.
if (lastPart === '' && sections.length > 1) {
return sections[sections.length - 2];
}
// Remove the extension, if need be.
if (ext.length > 0) {
const lastPartExt = lastPart.substr(lastPart.length - ext.length);
if (lastPartExt === ext) {
return lastPart.substr(0, lastPart.length - ext.length);
}
}
return lastPart;
}
export function absolute(path: string) {
if (path.startsWith('/')) {
return path;
}
if (path.startsWith('./')) {
return path.replace('./', '/');
}
return '/' + path;
}
function assertPath(path) {
if (typeof path !== 'string') {
throw new TypeError(
'Path must be a string. Received ' + JSON.stringify(path)
);
}
}
export function extname(path: string): string {
assertPath(path);
let startDot = -1;
let startPart = 0;
let end = -1;
let matchedSlash = true;
// Track the state of characters (if any) we see before our first dot and
// after any path separator we find
let preDotState = 0;
for (let i = path.length - 1; i >= 0; --i) {
const code = path.charCodeAt(i);
if (code === 47) {
// If we reached a path separator that was not part of a set of path
// separators at the end of the string, stop now
if (!matchedSlash) {
startPart = i + 1;
break;
}
// eslint-disable-next-line
continue;
}
if (end === -1) {
// We saw the first non-path separator, mark this as the end of our
// extension
matchedSlash = false;
end = i + 1;
}
if (code === 46 /* . */) {
// If this is our first dot, mark it as the start of our extension
if (startDot === -1) startDot = i;
else if (preDotState !== 1) preDotState = 1;
} else if (startDot !== -1) {
// We saw a non-dot and non-path separator before our dot, so we should
// have a good chance at having a non-empty extension
preDotState = -1;
}
}
if (
startDot === -1 ||
end === -1 ||
// We saw a non-dot character immediately before the dot
preDotState === 0 ||
// The (right-most) trimmed path component is exactly '..'
(preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)
) {
return '';
}
return path.slice(startDot, end);
}
export function resolve(...args) {
let resolvedPath = '';
let resolvedAbsolute = false;
for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
const path = i >= 0 ? args[i] : process.cwd();
// Skip empty and invalid entries
if (typeof path !== 'string') {
throw new TypeError('Arguments to path.resolve must be strings');
} else if (!path) {
continue;
}
resolvedPath = path + '/' + resolvedPath;
resolvedAbsolute = path[0] === '/';
}
// At this point the path should be resolved to a full absolute path, but
// handle relative paths to be safe (might happen when process.cwd() fails)
// Normalize the path
resolvedPath = normalizeArray(
resolvedPath.split('/'),
!resolvedAbsolute
).join('/');
return (resolvedAbsolute ? '/' : '') + resolvedPath || '.';
}
function trimArray(arr) {
const lastIndex = arr.length - 1;
let start = 0;
for (; start <= lastIndex; start++) {
if (arr[start]) break;
}
let end = lastIndex;
for (; end >= 0; end--) {
if (arr[end]) break;
}
if (start === 0 && end === lastIndex) return arr;
if (start > end) return [];
return arr.slice(start, end + 1);
}
export function relative(from: string, to: string) {
from = resolve(from).substr(1);
to = resolve(to).substr(1);
const fromParts = trimArray(from.split('/'));
const toParts = trimArray(to.split('/'));
const length = Math.min(fromParts.length, toParts.length);
let samePartsLength = length;
for (let i = 0; i < length; i++) {
if (fromParts[i] !== toParts[i]) {
samePartsLength = i;
break;
}
}
let outputParts = [];
for (let i = samePartsLength; i < fromParts.length; i++) {
outputParts.push('..');
}
outputParts = outputParts.concat(toParts.slice(samePartsLength));
return outputParts.join('/');
}
|