|
| 1 | +const isWin = process.platform === 'win32'; |
| 2 | +/*! |
| 3 | + * removeTrailingSeparator <https://github.com/darsain/remove-trailing-separator> |
| 4 | + * |
| 5 | + * Inlined from: |
| 6 | + * Copyright (c) darsain. |
| 7 | + * Released under the ISC License. |
| 8 | + */ |
| 9 | +function removeTrailingSeparator(str) { |
| 10 | + var i = str.length - 1; |
| 11 | + if (i < 2) { |
| 12 | + return str; |
| 13 | + } |
| 14 | + while (isSeparator(str, i)) { |
| 15 | + i--; |
| 16 | + } |
| 17 | + return str.substr(0, i + 1); |
| 18 | +}; |
| 19 | + |
| 20 | +function isSeparator(str, i) { |
| 21 | + var char = str[i]; |
| 22 | + return i > 0 && (char === '/' || (isWin && char === '\\')); |
| 23 | +} |
| 24 | + |
| 25 | +/*! |
| 26 | + * normalize-path <https://github.com/jonschlinkert/normalize-path> |
| 27 | + * |
| 28 | + * Inlined from: |
| 29 | + * Copyright (c) 2014-2017, Jon Schlinkert. |
| 30 | + * Released under the MIT License. |
| 31 | + */ |
| 32 | +function normalizePath(str, stripTrailing) { |
| 33 | + if (typeof str !== 'string') { |
| 34 | + throw new TypeError('expected a string'); |
| 35 | + } |
| 36 | + str = str.replace(/[\\\/]+/g, '/'); |
| 37 | + if (stripTrailing !== false) { |
| 38 | + str = removeTrailingSeparator(str); |
| 39 | + } |
| 40 | + return str; |
| 41 | +}; |
| 42 | + |
| 43 | +/*! |
| 44 | + * unixify <https://github.com/jonschlinkert/unixify> |
| 45 | + * |
| 46 | + * Inlined from: |
| 47 | + * Copyright (c) 2014, 2017, Jon Schlinkert. |
| 48 | + * Released under the MIT License. |
| 49 | + */ |
| 50 | +export function unixify(filepath, stripTrailing = true) { |
| 51 | + if(isWin) { |
| 52 | + filepath = normalizePath(filepath, stripTrailing); |
| 53 | + return filepath.replace(/^([a-zA-Z]+:|\.\/)/, ''); |
| 54 | + } |
| 55 | + return filepath; |
| 56 | +}; |
| 57 | + |
| 58 | +/* |
| 59 | +* Corrects a windows path to unix format (including \\?\c:...) |
| 60 | +*/ |
| 61 | +export function correctPath(filepath) { |
| 62 | + return unixify( filepath.replace(/^\\\\\?\\.:\\/,'\\')); |
| 63 | +} |
0 commit comments