2020-02-03 18:46:22 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var isWindows = process.platform === 'win32';
|
|
|
|
|
2022-06-28 16:39:30 +08:00
|
|
|
// Regex to split a windows path into into [dir, root, basename, name, ext]
|
|
|
|
var splitWindowsRe =
|
|
|
|
/^(((?:[a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?[\\\/]?)(?:[^\\\/]*[\\\/])*)((\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))[\\\/]*$/;
|
2020-02-03 18:46:22 +08:00
|
|
|
|
|
|
|
var win32 = {};
|
|
|
|
|
|
|
|
function win32SplitPath(filename) {
|
2022-06-28 16:39:30 +08:00
|
|
|
return splitWindowsRe.exec(filename).slice(1);
|
2020-02-03 18:46:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
win32.parse = function(pathString) {
|
|
|
|
if (typeof pathString !== 'string') {
|
|
|
|
throw new TypeError(
|
|
|
|
"Parameter 'pathString' must be a string, not " + typeof pathString
|
|
|
|
);
|
|
|
|
}
|
|
|
|
var allParts = win32SplitPath(pathString);
|
2022-06-28 16:39:30 +08:00
|
|
|
if (!allParts || allParts.length !== 5) {
|
2020-02-03 18:46:22 +08:00
|
|
|
throw new TypeError("Invalid path '" + pathString + "'");
|
|
|
|
}
|
|
|
|
return {
|
2022-06-28 16:39:30 +08:00
|
|
|
root: allParts[1],
|
|
|
|
dir: allParts[0] === allParts[1] ? allParts[0] : allParts[0].slice(0, -1),
|
2020-02-03 18:46:22 +08:00
|
|
|
base: allParts[2],
|
2022-06-28 16:39:30 +08:00
|
|
|
ext: allParts[4],
|
|
|
|
name: allParts[3]
|
2020-02-03 18:46:22 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-06-28 16:39:30 +08:00
|
|
|
// Split a filename into [dir, root, basename, name, ext], unix version
|
2020-02-03 18:46:22 +08:00
|
|
|
// 'root' is just a slash, or nothing.
|
|
|
|
var splitPathRe =
|
2022-06-28 16:39:30 +08:00
|
|
|
/^((\/?)(?:[^\/]*\/)*)((\.{1,2}|[^\/]+?|)(\.[^.\/]*|))[\/]*$/;
|
2020-02-03 18:46:22 +08:00
|
|
|
var posix = {};
|
|
|
|
|
|
|
|
|
|
|
|
function posixSplitPath(filename) {
|
|
|
|
return splitPathRe.exec(filename).slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
posix.parse = function(pathString) {
|
|
|
|
if (typeof pathString !== 'string') {
|
|
|
|
throw new TypeError(
|
|
|
|
"Parameter 'pathString' must be a string, not " + typeof pathString
|
|
|
|
);
|
|
|
|
}
|
|
|
|
var allParts = posixSplitPath(pathString);
|
2022-06-28 16:39:30 +08:00
|
|
|
if (!allParts || allParts.length !== 5) {
|
2020-02-03 18:46:22 +08:00
|
|
|
throw new TypeError("Invalid path '" + pathString + "'");
|
|
|
|
}
|
2022-06-28 16:39:30 +08:00
|
|
|
|
2020-02-03 18:46:22 +08:00
|
|
|
return {
|
2022-06-28 16:39:30 +08:00
|
|
|
root: allParts[1],
|
|
|
|
dir: allParts[0].slice(0, -1),
|
2020-02-03 18:46:22 +08:00
|
|
|
base: allParts[2],
|
2022-06-28 16:39:30 +08:00
|
|
|
ext: allParts[4],
|
|
|
|
name: allParts[3],
|
2020-02-03 18:46:22 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (isWindows)
|
|
|
|
module.exports = win32.parse;
|
|
|
|
else /* posix */
|
|
|
|
module.exports = posix.parse;
|
|
|
|
|
|
|
|
module.exports.posix = posix.parse;
|
|
|
|
module.exports.win32 = win32.parse;
|