2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2015-03-14 03:21:47 +01:00
|
|
|
const util = require('util');
|
2015-01-21 17:36:59 +01:00
|
|
|
const isWindows = process.platform === 'win32';
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2015-03-14 03:21:47 +01:00
|
|
|
function assertPath(path) {
|
|
|
|
if (typeof path !== 'string') {
|
|
|
|
throw new TypeError('Path must be a string. Received ' +
|
|
|
|
util.inspect(path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-07 01:06:27 +01:00
|
|
|
// resolves . and .. elements in a path array with directory names there
|
2014-11-21 09:22:07 +01:00
|
|
|
// must be no slashes or device names (c:\) in the array
|
2011-01-07 01:06:27 +01:00
|
|
|
// (so also no leading and trailing slashes - it does not distinguish
|
|
|
|
// relative and absolute paths)
|
2011-01-06 06:39:00 +01:00
|
|
|
function normalizeArray(parts, allowAboveRoot) {
|
2014-11-21 09:22:07 +01:00
|
|
|
var res = [];
|
|
|
|
for (var i = 0; i < parts.length; i++) {
|
|
|
|
var p = parts[i];
|
|
|
|
|
|
|
|
// ignore empty parts
|
|
|
|
if (!p || p === '.')
|
|
|
|
continue;
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2014-11-21 09:22:07 +01:00
|
|
|
if (p === '..') {
|
|
|
|
if (res.length && res[res.length - 1] !== '..') {
|
|
|
|
res.pop();
|
|
|
|
} else if (allowAboveRoot) {
|
|
|
|
res.push('..');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.push(p);
|
2011-01-06 06:39:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 09:22:07 +01:00
|
|
|
return res;
|
2010-10-26 23:41:06 +02:00
|
|
|
}
|
|
|
|
|
2015-05-23 06:42:12 +02:00
|
|
|
// Returns an array with empty elements removed from either end of the input
|
|
|
|
// array or the original array if no elements need to be removed
|
|
|
|
function trimArray(arr) {
|
|
|
|
var lastIndex = arr.length - 1;
|
|
|
|
var start = 0;
|
|
|
|
for (; start <= lastIndex; start++) {
|
|
|
|
if (arr[start])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
var 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);
|
|
|
|
}
|
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
// Regex to split a windows path into three parts: [*, device, slash,
|
|
|
|
// tail] windows-only
|
2015-01-21 17:36:59 +01:00
|
|
|
const splitDeviceRe =
|
2013-06-11 02:09:54 +02:00
|
|
|
/^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/;
|
|
|
|
|
|
|
|
// Regex to split the tail part of the above into [*, dir, basename, ext]
|
2015-01-21 17:36:59 +01:00
|
|
|
const splitTailRe =
|
2013-06-11 02:09:54 +02:00
|
|
|
/^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/;
|
|
|
|
|
|
|
|
var win32 = {};
|
|
|
|
|
|
|
|
// Function to split a filename into [root, dir, basename, ext]
|
2014-04-08 23:48:24 +02:00
|
|
|
function win32SplitPath(filename) {
|
2013-06-11 02:09:54 +02:00
|
|
|
// Separate device+slash from tail
|
2016-01-12 22:04:50 +01:00
|
|
|
const result = splitDeviceRe.exec(filename);
|
|
|
|
const device = (result[1] || '') + (result[2] || '');
|
|
|
|
const tail = result[3];
|
2013-06-11 02:09:54 +02:00
|
|
|
// Split the tail into dir, basename and extension
|
2016-01-12 22:04:50 +01:00
|
|
|
const result2 = splitTailRe.exec(tail);
|
|
|
|
const dir = result2[1];
|
|
|
|
const basename = result2[2];
|
|
|
|
const ext = result2[3];
|
2013-06-11 02:09:54 +02:00
|
|
|
return [device, dir, basename, ext];
|
2014-04-08 23:48:24 +02:00
|
|
|
}
|
2010-11-21 23:13:54 +01:00
|
|
|
|
2015-05-23 06:42:12 +02:00
|
|
|
function win32StatPath(path) {
|
2016-01-12 22:04:50 +01:00
|
|
|
const result = splitDeviceRe.exec(path);
|
|
|
|
const device = result[1] || '';
|
|
|
|
const isUnc = !!device && device[1] !== ':';
|
2015-05-23 06:42:12 +02:00
|
|
|
return {
|
|
|
|
device,
|
|
|
|
isUnc,
|
|
|
|
isAbsolute: isUnc || !!result[2], // UNC paths are always absolute
|
|
|
|
tail: result[3]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeUNCRoot(device) {
|
2013-06-11 02:09:54 +02:00
|
|
|
return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\');
|
2015-05-23 06:42:12 +02:00
|
|
|
}
|
2010-11-21 23:13:54 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
// path.resolve([from ...], to)
|
|
|
|
win32.resolve = function() {
|
2016-01-12 22:04:50 +01:00
|
|
|
var resolvedDevice = '';
|
|
|
|
var resolvedTail = '';
|
|
|
|
var resolvedAbsolute = false;
|
2013-06-11 02:09:54 +02:00
|
|
|
|
|
|
|
for (var i = arguments.length - 1; i >= -1; i--) {
|
|
|
|
var path;
|
|
|
|
if (i >= 0) {
|
|
|
|
path = arguments[i];
|
|
|
|
} else if (!resolvedDevice) {
|
|
|
|
path = process.cwd();
|
|
|
|
} else {
|
|
|
|
// Windows has the concept of drive-specific current working
|
|
|
|
// directories. If we've resolved a drive letter but not yet an
|
|
|
|
// absolute path, get cwd for that drive. We're sure the device is not
|
|
|
|
// an unc path at this points, because unc paths are always absolute.
|
|
|
|
path = process.env['=' + resolvedDevice];
|
|
|
|
// Verify that a drive-local cwd was found and that it actually points
|
|
|
|
// to our drive. If not, default to the drive's root.
|
|
|
|
if (!path || path.substr(0, 3).toLowerCase() !==
|
|
|
|
resolvedDevice.toLowerCase() + '\\') {
|
|
|
|
path = resolvedDevice + '\\';
|
2011-01-06 06:39:00 +01:00
|
|
|
}
|
2013-06-11 02:09:54 +02:00
|
|
|
}
|
2010-11-21 23:13:54 +01:00
|
|
|
|
2015-03-14 03:21:47 +01:00
|
|
|
assertPath(path);
|
|
|
|
|
|
|
|
// Skip empty entries
|
|
|
|
if (path === '') {
|
2013-06-11 02:09:54 +02:00
|
|
|
continue;
|
|
|
|
}
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2016-01-12 22:04:50 +01:00
|
|
|
const result = win32StatPath(path);
|
|
|
|
const device = result.device;
|
|
|
|
var isUnc = result.isUnc;
|
|
|
|
const isAbsolute = result.isAbsolute;
|
|
|
|
const tail = result.tail;
|
2013-06-11 02:09:54 +02:00
|
|
|
|
|
|
|
if (device &&
|
|
|
|
resolvedDevice &&
|
|
|
|
device.toLowerCase() !== resolvedDevice.toLowerCase()) {
|
|
|
|
// This path points to another device so it is not applicable
|
|
|
|
continue;
|
2011-01-06 06:39:00 +01:00
|
|
|
}
|
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
if (!resolvedDevice) {
|
|
|
|
resolvedDevice = device;
|
|
|
|
}
|
|
|
|
if (!resolvedAbsolute) {
|
|
|
|
resolvedTail = tail + '\\' + resolvedTail;
|
|
|
|
resolvedAbsolute = isAbsolute;
|
2011-01-06 06:39:00 +01:00
|
|
|
}
|
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
if (resolvedDevice && resolvedAbsolute) {
|
|
|
|
break;
|
2012-11-21 01:00:45 +01:00
|
|
|
}
|
2013-06-11 02:09:54 +02:00
|
|
|
}
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
// Convert slashes to backslashes when `resolvedDevice` points to an UNC
|
|
|
|
// root. Also squash multiple slashes into a single one where appropriate.
|
|
|
|
if (isUnc) {
|
|
|
|
resolvedDevice = normalizeUNCRoot(resolvedDevice);
|
|
|
|
}
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
// 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)
|
2011-01-07 01:06:27 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
// Normalize the tail path
|
2014-11-21 09:22:07 +01:00
|
|
|
resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/),
|
2013-06-11 02:09:54 +02:00
|
|
|
!resolvedAbsolute).join('\\');
|
2014-09-17 12:59:59 +02:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
|
|
|
|
'.';
|
|
|
|
};
|
2010-11-21 23:13:54 +01:00
|
|
|
|
2013-03-18 18:48:13 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
win32.normalize = function(path) {
|
2015-03-14 03:21:47 +01:00
|
|
|
assertPath(path);
|
|
|
|
|
2016-01-12 22:04:50 +01:00
|
|
|
const result = win32StatPath(path);
|
|
|
|
var device = result.device;
|
|
|
|
const isUnc = result.isUnc;
|
|
|
|
const isAbsolute = result.isAbsolute;
|
|
|
|
var tail = result.tail;
|
|
|
|
const trailingSlash = /[\\\/]$/.test(tail);
|
2010-11-21 23:13:54 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
// Normalize the tail path
|
2014-11-21 09:22:07 +01:00
|
|
|
tail = normalizeArray(tail.split(/[\\\/]+/), !isAbsolute).join('\\');
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
if (!tail && !isAbsolute) {
|
|
|
|
tail = '.';
|
|
|
|
}
|
|
|
|
if (tail && trailingSlash) {
|
|
|
|
tail += '\\';
|
|
|
|
}
|
2011-01-07 01:06:27 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
// Convert slashes to backslashes when `device` points to an UNC root.
|
|
|
|
// Also squash multiple slashes into a single one where appropriate.
|
|
|
|
if (isUnc) {
|
|
|
|
device = normalizeUNCRoot(device);
|
|
|
|
}
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
return device + (isAbsolute ? '\\' : '') + tail;
|
|
|
|
};
|
2011-03-06 03:42:33 +01:00
|
|
|
|
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
win32.isAbsolute = function(path) {
|
2015-03-14 03:21:47 +01:00
|
|
|
assertPath(path);
|
2015-05-23 06:42:12 +02:00
|
|
|
return win32StatPath(path).isAbsolute;
|
2013-06-11 02:09:54 +02:00
|
|
|
};
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
win32.join = function() {
|
2015-05-23 06:42:12 +02:00
|
|
|
var paths = [];
|
|
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
|
|
var arg = arguments[i];
|
|
|
|
assertPath(arg);
|
|
|
|
if (arg) {
|
|
|
|
paths.push(arg);
|
2012-11-21 01:00:45 +01:00
|
|
|
}
|
2013-06-11 02:09:54 +02:00
|
|
|
}
|
2012-05-13 03:18:09 +02:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
var joined = paths.join('\\');
|
|
|
|
|
|
|
|
// Make sure that the joined path doesn't start with two slashes, because
|
|
|
|
// normalize() will mistake it for an UNC path then.
|
|
|
|
//
|
|
|
|
// This step is skipped when it is very clear that the user actually
|
|
|
|
// intended to point at an UNC path. This is assumed when the first
|
|
|
|
// non-empty string arguments starts with exactly two slashes followed by
|
|
|
|
// at least one more non-slash character.
|
|
|
|
//
|
|
|
|
// Note that for normalize() to treat a path as an UNC path it needs to
|
|
|
|
// have at least 2 components, so we don't filter for that here.
|
|
|
|
// This means that the user can use join to construct UNC paths from
|
|
|
|
// a server name and a share name; for example:
|
|
|
|
// path.join('//server', 'share') -> '\\\\server\\share\')
|
|
|
|
if (!/^[\\\/]{2}[^\\\/]/.test(paths[0])) {
|
|
|
|
joined = joined.replace(/^[\\\/]{2,}/, '\\');
|
|
|
|
}
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
return win32.normalize(joined);
|
|
|
|
};
|
2013-04-14 21:44:40 +02:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
|
|
|
|
// path.relative(from, to)
|
|
|
|
// it will solve the relative path from 'from' to 'to', for instance:
|
|
|
|
// from = 'C:\\orandea\\test\\aaa'
|
|
|
|
// to = 'C:\\orandea\\impl\\bbb'
|
|
|
|
// The output of the function should be: '..\\..\\impl\\bbb'
|
|
|
|
win32.relative = function(from, to) {
|
2015-03-14 03:21:47 +01:00
|
|
|
assertPath(from);
|
|
|
|
assertPath(to);
|
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
from = win32.resolve(from);
|
|
|
|
to = win32.resolve(to);
|
|
|
|
|
|
|
|
// windows is not case sensitive
|
|
|
|
var lowerFrom = from.toLowerCase();
|
|
|
|
var lowerTo = to.toLowerCase();
|
|
|
|
|
2015-05-23 06:42:12 +02:00
|
|
|
var toParts = trimArray(to.split('\\'));
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2015-05-23 06:42:12 +02:00
|
|
|
var lowerFromParts = trimArray(lowerFrom.split('\\'));
|
|
|
|
var lowerToParts = trimArray(lowerTo.split('\\'));
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
var length = Math.min(lowerFromParts.length, lowerToParts.length);
|
|
|
|
var samePartsLength = length;
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
if (lowerFromParts[i] !== lowerToParts[i]) {
|
|
|
|
samePartsLength = i;
|
|
|
|
break;
|
2011-03-06 03:42:33 +01:00
|
|
|
}
|
2013-06-11 02:09:54 +02:00
|
|
|
}
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2015-08-15 19:07:07 +02:00
|
|
|
if (samePartsLength === 0) {
|
2013-06-11 02:09:54 +02:00
|
|
|
return to;
|
|
|
|
}
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
var outputParts = [];
|
2016-01-30 05:34:13 +01:00
|
|
|
for (var j = samePartsLength; j < lowerFromParts.length; j++) {
|
2013-06-11 02:09:54 +02:00
|
|
|
outputParts.push('..');
|
|
|
|
}
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
outputParts = outputParts.concat(toParts.slice(samePartsLength));
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
return outputParts.join('\\');
|
|
|
|
};
|
2011-03-06 03:42:33 +01:00
|
|
|
|
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
win32._makeLong = function(path) {
|
|
|
|
// Note: this will *probably* throw somewhere.
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof path !== 'string')
|
2013-06-11 02:09:54 +02:00
|
|
|
return path;
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
if (!path) {
|
|
|
|
return '';
|
|
|
|
}
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
var resolvedPath = win32.resolve(path);
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
if (/^[a-zA-Z]\:\\/.test(resolvedPath)) {
|
|
|
|
// path is local filesystem path, which needs to be converted
|
|
|
|
// to long UNC path.
|
|
|
|
return '\\\\?\\' + resolvedPath;
|
|
|
|
} else if (/^\\\\[^?.]/.test(resolvedPath)) {
|
|
|
|
// path is network UNC path, which needs to be converted
|
|
|
|
// to long UNC path.
|
|
|
|
return '\\\\?\\UNC\\' + resolvedPath.substring(2);
|
|
|
|
}
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
return path;
|
|
|
|
};
|
2011-01-06 06:39:00 +01:00
|
|
|
|
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
win32.dirname = function(path) {
|
2016-01-12 22:04:50 +01:00
|
|
|
const result = win32SplitPath(path);
|
|
|
|
const root = result[0];
|
|
|
|
var dir = result[1];
|
2011-02-08 22:56:03 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
if (!root && !dir) {
|
|
|
|
// No dirname whatsoever
|
|
|
|
return '.';
|
|
|
|
}
|
2011-02-08 22:56:03 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
if (dir) {
|
|
|
|
// It has a dirname, strip trailing slash
|
|
|
|
dir = dir.substr(0, dir.length - 1);
|
|
|
|
}
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
return root + dir;
|
|
|
|
};
|
2011-01-06 06:39:00 +01:00
|
|
|
|
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
win32.basename = function(path, ext) {
|
2015-03-14 03:21:47 +01:00
|
|
|
if (ext !== undefined && typeof ext !== 'string')
|
2015-10-14 23:10:25 +02:00
|
|
|
throw new TypeError('"ext" argument must be a string');
|
2015-03-14 03:21:47 +01:00
|
|
|
|
2014-04-08 23:48:24 +02:00
|
|
|
var f = win32SplitPath(path)[2];
|
2013-06-11 02:09:54 +02:00
|
|
|
// TODO: make this comparison case-insensitive on windows?
|
|
|
|
if (ext && f.substr(-1 * ext.length) === ext) {
|
|
|
|
f = f.substr(0, f.length - ext.length);
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
};
|
2011-01-06 06:39:00 +01:00
|
|
|
|
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
win32.extname = function(path) {
|
2014-04-08 23:48:24 +02:00
|
|
|
return win32SplitPath(path)[3];
|
|
|
|
};
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2010-04-21 03:17:54 +02:00
|
|
|
|
2014-04-08 23:48:24 +02:00
|
|
|
win32.format = function(pathObject) {
|
2015-01-29 02:05:53 +01:00
|
|
|
if (pathObject === null || typeof pathObject !== 'object') {
|
2014-04-08 23:48:24 +02:00
|
|
|
throw new TypeError(
|
2015-10-14 23:10:25 +02:00
|
|
|
'Parameter "pathObject" must be an object, not ' + typeof pathObject
|
2014-04-08 23:48:24 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-08-17 05:26:46 +02:00
|
|
|
var dir = pathObject.dir || pathObject.root;
|
|
|
|
var base = pathObject.base ||
|
|
|
|
((pathObject.name || '') + (pathObject.ext || ''));
|
2015-05-23 06:42:12 +02:00
|
|
|
if (!dir) {
|
|
|
|
return base;
|
2014-04-08 23:48:24 +02:00
|
|
|
}
|
2015-08-17 05:26:46 +02:00
|
|
|
if (dir === pathObject.root) {
|
2015-05-23 06:42:12 +02:00
|
|
|
return dir + base;
|
2014-04-08 23:48:24 +02:00
|
|
|
}
|
2015-05-23 06:42:12 +02:00
|
|
|
return dir + win32.sep + base;
|
2014-04-08 23:48:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
win32.parse = function(pathString) {
|
2015-03-14 03:21:47 +01:00
|
|
|
assertPath(pathString);
|
|
|
|
|
2014-04-08 23:48:24 +02:00
|
|
|
var allParts = win32SplitPath(pathString);
|
|
|
|
return {
|
|
|
|
root: allParts[0],
|
2015-05-23 06:42:12 +02:00
|
|
|
dir: allParts[0] + allParts[1].slice(0, -1),
|
2014-04-08 23:48:24 +02:00
|
|
|
base: allParts[2],
|
|
|
|
ext: allParts[3],
|
|
|
|
name: allParts[2].slice(0, allParts[2].length - allParts[3].length)
|
2013-04-14 21:44:40 +02:00
|
|
|
};
|
2013-06-11 02:09:54 +02:00
|
|
|
};
|
2010-11-21 23:13:54 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
|
|
|
|
win32.sep = '\\';
|
|
|
|
win32.delimiter = ';';
|
|
|
|
|
|
|
|
|
|
|
|
// Split a filename into [root, dir, basename, ext], unix version
|
|
|
|
// 'root' is just a slash, or nothing.
|
2015-01-21 17:36:59 +01:00
|
|
|
const splitPathRe =
|
2013-06-11 02:09:54 +02:00
|
|
|
/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
|
|
|
|
var posix = {};
|
|
|
|
|
|
|
|
|
2014-04-08 23:48:24 +02:00
|
|
|
function posixSplitPath(filename) {
|
2015-09-23 23:29:01 +02:00
|
|
|
const out = splitPathRe.exec(filename);
|
|
|
|
out.shift();
|
|
|
|
return out;
|
2014-04-08 23:48:24 +02:00
|
|
|
}
|
2013-06-11 02:09:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
// path.resolve([from ...], to)
|
|
|
|
// posix version
|
|
|
|
posix.resolve = function() {
|
2016-01-12 22:04:50 +01:00
|
|
|
var resolvedPath = '';
|
|
|
|
var resolvedAbsolute = false;
|
2013-06-11 02:09:54 +02:00
|
|
|
|
|
|
|
for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
|
|
|
|
var path = (i >= 0) ? arguments[i] : process.cwd();
|
|
|
|
|
2015-03-14 03:21:47 +01:00
|
|
|
assertPath(path);
|
|
|
|
|
|
|
|
// Skip empty entries
|
|
|
|
if (path === '') {
|
2013-06-11 02:09:54 +02:00
|
|
|
continue;
|
2014-01-21 23:24:58 +01:00
|
|
|
}
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
resolvedPath = path + '/' + resolvedPath;
|
2015-05-23 06:42:12 +02:00
|
|
|
resolvedAbsolute = path[0] === '/';
|
2013-06-11 02:09:54 +02:00
|
|
|
}
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
// 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)
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
// Normalize the path
|
2014-11-21 09:22:07 +01:00
|
|
|
resolvedPath = normalizeArray(resolvedPath.split('/'),
|
|
|
|
!resolvedAbsolute).join('/');
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
|
|
|
|
};
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
// path.normalize(path)
|
|
|
|
// posix version
|
|
|
|
posix.normalize = function(path) {
|
2015-03-14 03:21:47 +01:00
|
|
|
assertPath(path);
|
|
|
|
|
2016-01-12 22:04:50 +01:00
|
|
|
const isAbsolute = posix.isAbsolute(path);
|
|
|
|
const trailingSlash = path && path[path.length - 1] === '/';
|
2013-06-11 02:09:54 +02:00
|
|
|
|
|
|
|
// Normalize the path
|
2014-11-21 09:22:07 +01:00
|
|
|
path = normalizeArray(path.split('/'), !isAbsolute).join('/');
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
if (!path && !isAbsolute) {
|
|
|
|
path = '.';
|
|
|
|
}
|
|
|
|
if (path && trailingSlash) {
|
|
|
|
path += '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (isAbsolute ? '/' : '') + path;
|
|
|
|
};
|
|
|
|
|
|
|
|
// posix version
|
|
|
|
posix.isAbsolute = function(path) {
|
2015-03-14 03:21:47 +01:00
|
|
|
assertPath(path);
|
2015-05-23 06:42:12 +02:00
|
|
|
return !!path && path[0] === '/';
|
2013-06-11 02:09:54 +02:00
|
|
|
};
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
// posix version
|
|
|
|
posix.join = function() {
|
|
|
|
var path = '';
|
|
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
|
|
var segment = arguments[i];
|
2015-07-11 00:13:59 +02:00
|
|
|
assertPath(segment);
|
2013-06-11 02:09:54 +02:00
|
|
|
if (segment) {
|
|
|
|
if (!path) {
|
|
|
|
path += segment;
|
|
|
|
} else {
|
|
|
|
path += '/' + segment;
|
2011-03-06 03:42:33 +01:00
|
|
|
}
|
|
|
|
}
|
2013-06-11 02:09:54 +02:00
|
|
|
}
|
|
|
|
return posix.normalize(path);
|
|
|
|
};
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
|
|
|
|
// path.relative(from, to)
|
|
|
|
// posix version
|
|
|
|
posix.relative = function(from, to) {
|
2015-03-14 03:21:47 +01:00
|
|
|
assertPath(from);
|
|
|
|
assertPath(to);
|
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
from = posix.resolve(from).substr(1);
|
|
|
|
to = posix.resolve(to).substr(1);
|
|
|
|
|
2015-05-23 06:42:12 +02:00
|
|
|
var fromParts = trimArray(from.split('/'));
|
|
|
|
var toParts = trimArray(to.split('/'));
|
2011-03-06 03:42:33 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
var length = Math.min(fromParts.length, toParts.length);
|
|
|
|
var samePartsLength = length;
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
if (fromParts[i] !== toParts[i]) {
|
|
|
|
samePartsLength = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var outputParts = [];
|
2016-01-30 05:34:13 +01:00
|
|
|
for (var j = samePartsLength; j < fromParts.length; j++) {
|
2013-06-11 02:09:54 +02:00
|
|
|
outputParts.push('..');
|
|
|
|
}
|
|
|
|
|
|
|
|
outputParts = outputParts.concat(toParts.slice(samePartsLength));
|
2010-04-21 03:17:54 +02:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
return outputParts.join('/');
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
posix._makeLong = function(path) {
|
|
|
|
return path;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
posix.dirname = function(path) {
|
2016-01-12 22:04:50 +01:00
|
|
|
const result = posixSplitPath(path);
|
|
|
|
const root = result[0];
|
|
|
|
var dir = result[1];
|
2011-09-06 04:46:44 +02:00
|
|
|
|
|
|
|
if (!root && !dir) {
|
|
|
|
// No dirname whatsoever
|
2011-01-07 01:06:27 +01:00
|
|
|
return '.';
|
2010-05-16 21:29:29 +02:00
|
|
|
}
|
2011-09-06 04:46:44 +02:00
|
|
|
|
|
|
|
if (dir) {
|
|
|
|
// It has a dirname, strip trailing slash
|
2012-05-20 21:26:29 +02:00
|
|
|
dir = dir.substr(0, dir.length - 1);
|
2011-09-06 04:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return root + dir;
|
2010-04-21 03:17:54 +02:00
|
|
|
};
|
|
|
|
|
2010-11-21 23:13:54 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
posix.basename = function(path, ext) {
|
2015-03-14 03:21:47 +01:00
|
|
|
if (ext !== undefined && typeof ext !== 'string')
|
2015-10-14 23:10:25 +02:00
|
|
|
throw new TypeError('"ext" argument must be a string');
|
2015-03-14 03:21:47 +01:00
|
|
|
|
2014-04-08 23:48:24 +02:00
|
|
|
var f = posixSplitPath(path)[2];
|
2015-03-14 03:21:47 +01:00
|
|
|
|
2010-04-21 03:17:54 +02:00
|
|
|
if (ext && f.substr(-1 * ext.length) === ext) {
|
|
|
|
f = f.substr(0, f.length - ext.length);
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
};
|
|
|
|
|
2010-11-21 23:13:54 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
posix.extname = function(path) {
|
2014-04-08 23:48:24 +02:00
|
|
|
return posixSplitPath(path)[3];
|
2010-04-21 03:17:54 +02:00
|
|
|
};
|
|
|
|
|
2010-11-21 23:13:54 +01:00
|
|
|
|
2014-04-08 23:48:24 +02:00
|
|
|
posix.format = function(pathObject) {
|
2015-01-29 02:05:53 +01:00
|
|
|
if (pathObject === null || typeof pathObject !== 'object') {
|
2014-04-08 23:48:24 +02:00
|
|
|
throw new TypeError(
|
2015-11-10 00:10:49 +01:00
|
|
|
'Parameter "pathObject" must be an object, not ' + typeof pathObject
|
2014-04-08 23:48:24 +02:00
|
|
|
);
|
|
|
|
}
|
2010-06-16 15:51:19 +02:00
|
|
|
|
2015-08-17 05:26:46 +02:00
|
|
|
var dir = pathObject.dir || pathObject.root;
|
|
|
|
var base = pathObject.base ||
|
|
|
|
((pathObject.name || '') + (pathObject.ext || ''));
|
|
|
|
if (!dir) {
|
|
|
|
return base;
|
2014-04-08 23:48:24 +02:00
|
|
|
}
|
2015-08-17 05:26:46 +02:00
|
|
|
if (dir === pathObject.root) {
|
|
|
|
return dir + base;
|
|
|
|
}
|
|
|
|
return dir + posix.sep + base;
|
2014-04-08 23:48:24 +02:00
|
|
|
};
|
2011-11-25 09:29:06 +01:00
|
|
|
|
2013-02-18 19:46:50 +01:00
|
|
|
|
2014-04-08 23:48:24 +02:00
|
|
|
posix.parse = function(pathString) {
|
2015-03-14 03:21:47 +01:00
|
|
|
assertPath(pathString);
|
|
|
|
|
2014-04-08 23:48:24 +02:00
|
|
|
var allParts = posixSplitPath(pathString);
|
|
|
|
return {
|
|
|
|
root: allParts[0],
|
2015-05-23 06:42:12 +02:00
|
|
|
dir: allParts[0] + allParts[1].slice(0, -1),
|
2014-04-08 23:48:24 +02:00
|
|
|
base: allParts[2],
|
|
|
|
ext: allParts[3],
|
|
|
|
name: allParts[2].slice(0, allParts[2].length - allParts[3].length)
|
|
|
|
};
|
2010-04-21 03:17:54 +02:00
|
|
|
};
|
2012-02-02 16:57:45 +01:00
|
|
|
|
2011-11-25 09:29:06 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
posix.sep = '/';
|
|
|
|
posix.delimiter = ':';
|
2011-11-25 09:29:06 +01:00
|
|
|
|
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
if (isWindows)
|
|
|
|
module.exports = win32;
|
|
|
|
else /* posix */
|
|
|
|
module.exports = posix;
|
2011-11-25 09:29:06 +01:00
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
module.exports.posix = posix;
|
|
|
|
module.exports.win32 = win32;
|