|
| 1 | +// Slightly modified (no IE8 support, ES6) and transcribed to TypeScript |
| 2 | +// https://raw.githubusercontent.com/calvinmetcalf/rollup-plugin-node-builtins/master/src/es6/path.js |
| 3 | + |
| 4 | +/** JSDoc */ |
| 5 | +function normalizeArray(parts: string[], allowAboveRoot?: boolean): string[] { |
| 6 | + // if the path tries to go above the root, `up` ends up > 0 |
| 7 | + let up = 0; |
| 8 | + for (let i = parts.length - 1; i >= 0; i--) { |
| 9 | + const last = parts[i]; |
| 10 | + if (last === '.') { |
| 11 | + parts.splice(i, 1); |
| 12 | + } else if (last === '..') { |
| 13 | + parts.splice(i, 1); |
| 14 | + up++; |
| 15 | + } else if (up) { |
| 16 | + parts.splice(i, 1); |
| 17 | + up--; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + // if the path is allowed to go above the root, restore leading ..s |
| 22 | + if (allowAboveRoot) { |
| 23 | + for (; up--; up) { |
| 24 | + parts.unshift('..'); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + return parts; |
| 29 | +} |
| 30 | + |
| 31 | +// Split a filename into [root, dir, basename, ext], unix version |
| 32 | +// 'root' is just a slash, or nothing. |
| 33 | +const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; |
| 34 | +/** JSDoc */ |
| 35 | +function splitPath(filename: string): string[] { |
| 36 | + const parts = splitPathRe.exec(filename); |
| 37 | + return parts ? parts.slice(1) : []; |
| 38 | +} |
| 39 | + |
| 40 | +// path.resolve([from ...], to) |
| 41 | +// posix version |
| 42 | +/** JSDoc */ |
| 43 | +export function resolve(...args: string[]): string { |
| 44 | + let resolvedPath = ''; |
| 45 | + let resolvedAbsolute = false; |
| 46 | + |
| 47 | + for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) { |
| 48 | + const path = i >= 0 ? args[i] : '/'; |
| 49 | + |
| 50 | + // Skip empty entries |
| 51 | + if (!path) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + resolvedPath = `${path}/${resolvedPath}`; |
| 56 | + resolvedAbsolute = path.charAt(0) === '/'; |
| 57 | + } |
| 58 | + |
| 59 | + // At this point the path should be resolved to a full absolute path, but |
| 60 | + // handle relative paths to be safe (might happen when process.cwd() fails) |
| 61 | + |
| 62 | + // Normalize the path |
| 63 | + resolvedPath = normalizeArray(resolvedPath.split('/').filter(p => !!p), !resolvedAbsolute).join('/'); |
| 64 | + |
| 65 | + return (resolvedAbsolute ? '/' : '') + resolvedPath || '.'; |
| 66 | +} |
| 67 | + |
| 68 | +/** JSDoc */ |
| 69 | +function trim(arr: string[]): string[] { |
| 70 | + let start = 0; |
| 71 | + for (; start < arr.length; start++) { |
| 72 | + if (arr[start] !== '') { |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + let end = arr.length - 1; |
| 78 | + for (; end >= 0; end--) { |
| 79 | + if (arr[end] !== '') { |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (start > end) { |
| 85 | + return []; |
| 86 | + } |
| 87 | + return arr.slice(start, end - start + 1); |
| 88 | +} |
| 89 | + |
| 90 | +// path.relative(from, to) |
| 91 | +// posix version |
| 92 | +/** JSDoc */ |
| 93 | +export function relative(from: string, to: string): string { |
| 94 | + // tslint:disable:no-parameter-reassignment |
| 95 | + from = resolve(from).substr(1); |
| 96 | + to = resolve(to).substr(1); |
| 97 | + |
| 98 | + const fromParts = trim(from.split('/')); |
| 99 | + const toParts = trim(to.split('/')); |
| 100 | + |
| 101 | + const length = Math.min(fromParts.length, toParts.length); |
| 102 | + let samePartsLength = length; |
| 103 | + for (let i = 0; i < length; i++) { |
| 104 | + if (fromParts[i] !== toParts[i]) { |
| 105 | + samePartsLength = i; |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + let outputParts = []; |
| 111 | + for (let i = samePartsLength; i < fromParts.length; i++) { |
| 112 | + outputParts.push('..'); |
| 113 | + } |
| 114 | + |
| 115 | + outputParts = outputParts.concat(toParts.slice(samePartsLength)); |
| 116 | + |
| 117 | + return outputParts.join('/'); |
| 118 | +} |
| 119 | + |
| 120 | +// path.normalize(path) |
| 121 | +// posix version |
| 122 | +/** JSDoc */ |
| 123 | +export function normalize(path: string): string { |
| 124 | + const isPathAbsolute = isAbsolute(path); |
| 125 | + const trailingSlash = path.substr(-1) === '/'; |
| 126 | + |
| 127 | + // Normalize the path |
| 128 | + let normalizedPath = normalizeArray(path.split('/').filter(p => !!p), !isPathAbsolute).join('/'); |
| 129 | + |
| 130 | + if (!normalizedPath && !isPathAbsolute) { |
| 131 | + normalizedPath = '.'; |
| 132 | + } |
| 133 | + if (normalizedPath && trailingSlash) { |
| 134 | + normalizedPath += '/'; |
| 135 | + } |
| 136 | + |
| 137 | + return (isPathAbsolute ? '/' : '') + normalizedPath; |
| 138 | +} |
| 139 | + |
| 140 | +// posix version |
| 141 | +/** JSDoc */ |
| 142 | +export function isAbsolute(path: string): boolean { |
| 143 | + return path.charAt(0) === '/'; |
| 144 | +} |
| 145 | + |
| 146 | +// posix version |
| 147 | +/** JSDoc */ |
| 148 | +export function join(...args: string[]): string { |
| 149 | + return normalize(args.join('/')); |
| 150 | +} |
| 151 | + |
| 152 | +/** JSDoc */ |
| 153 | +export function dirname(path: string): string { |
| 154 | + const result = splitPath(path); |
| 155 | + const root = result[0]; |
| 156 | + let dir = result[1]; |
| 157 | + |
| 158 | + if (!root && !dir) { |
| 159 | + // No dirname whatsoever |
| 160 | + return '.'; |
| 161 | + } |
| 162 | + |
| 163 | + if (dir) { |
| 164 | + // It has a dirname, strip trailing slash |
| 165 | + dir = dir.substr(0, dir.length - 1); |
| 166 | + } |
| 167 | + |
| 168 | + return root + dir; |
| 169 | +} |
| 170 | + |
| 171 | +/** JSDoc */ |
| 172 | +export function basename(path: string, ext?: string): string { |
| 173 | + let f = splitPath(path)[2]; |
| 174 | + if (ext && f.substr(ext.length * -1) === ext) { |
| 175 | + f = f.substr(0, f.length - ext.length); |
| 176 | + } |
| 177 | + return f; |
| 178 | +} |
0 commit comments