2012-10-11 20:26:23 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2013-11-29 15:56:55 +00:00
|
|
|
// Based on https://github.com/joyent/node/blob/41e53e557992a7d552a8e23de035f9463da25c99/lib/path.js
|
2012-10-11 20:26:23 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
// resolves . and .. elements in a path array with directory names there
|
|
|
|
// must be no slashes, empty elements, 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, allowAboveRoot) {
|
|
|
|
// if the path tries to go above the root, `up` ends up > 0
|
|
|
|
var up = 0;
|
|
|
|
for (var i = parts.length - 1; i >= 0; i--) {
|
|
|
|
var last = parts[i];
|
|
|
|
if (last === '.') {
|
|
|
|
parts.splice(i, 1);
|
|
|
|
} else if (last === '..') {
|
|
|
|
parts.splice(i, 1);
|
|
|
|
up++;
|
|
|
|
} else if (up) {
|
|
|
|
parts.splice(i, 1);
|
|
|
|
up--;
|
2012-10-11 20:26:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
// if the path is allowed to go above the root, restore leading ..s
|
|
|
|
if (allowAboveRoot) {
|
|
|
|
for (; up--; up) {
|
|
|
|
parts.unshift('..');
|
2013-11-29 15:56:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split a filename into [root, dir, basename, ext], unix version
|
|
|
|
// 'root' is just a slash, or nothing.
|
|
|
|
var splitPathRe =
|
|
|
|
/^(\/?)([\s\S]+\/(?!$)|\/)?((?:\.{1,2}$|[\s\S]+?)?(\.[^.\/]*)?)$/;
|
|
|
|
var splitPath = function(filename) {
|
|
|
|
var result = splitPathRe.exec(filename);
|
|
|
|
return [result[1] || '', result[2] || '', result[3] || '', result[4] || ''];
|
|
|
|
};
|
|
|
|
|
|
|
|
// path.resolve([from ...], to)
|
|
|
|
function resolve() {
|
|
|
|
var resolvedPath = '',
|
|
|
|
resolvedAbsolute = false;
|
|
|
|
|
|
|
|
for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
|
2014-11-28 17:48:10 +00:00
|
|
|
// XXXfiler: we don't have process.cwd() so we use '/' as a fallback
|
2014-05-23 18:14:06 +00:00
|
|
|
var path = (i >= 0) ? arguments[i] : '/';
|
|
|
|
|
|
|
|
// Skip empty and invalid entries
|
|
|
|
if (typeof path !== 'string' || !path) {
|
|
|
|
continue;
|
2012-10-11 20:26:23 +00:00
|
|
|
}
|
2013-11-29 15:56:55 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
resolvedPath = path + '/' + resolvedPath;
|
|
|
|
resolvedAbsolute = path.charAt(0) === '/';
|
2013-11-29 15:56:55 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 18:14:06 +00: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)
|
2013-11-29 15:56:55 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
// Normalize the path
|
|
|
|
resolvedPath = normalizeArray(resolvedPath.split('/').filter(function(p) {
|
|
|
|
return !!p;
|
|
|
|
}), !resolvedAbsolute).join('/');
|
2013-11-29 15:56:55 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
|
|
|
|
}
|
2013-11-29 15:56:55 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
// path.normalize(path)
|
|
|
|
function normalize(path) {
|
|
|
|
var isAbsolute = path.charAt(0) === '/',
|
|
|
|
trailingSlash = path.substr(-1) === '/';
|
2013-11-29 15:56:55 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
// Normalize the path
|
|
|
|
path = normalizeArray(path.split('/').filter(function(p) {
|
|
|
|
return !!p;
|
|
|
|
}), !isAbsolute).join('/');
|
2013-11-29 15:56:55 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
if (!path && !isAbsolute) {
|
|
|
|
path = '.';
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
if (path && trailingSlash) {
|
|
|
|
path += '/';
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (isAbsolute ? '/' : '') + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
function join() {
|
|
|
|
var paths = Array.prototype.slice.call(arguments, 0);
|
|
|
|
return normalize(paths.filter(function(p, index) {
|
|
|
|
return p && typeof p === 'string';
|
|
|
|
}).join('/'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// path.relative(from, to)
|
|
|
|
function relative(from, to) {
|
|
|
|
from = exports.resolve(from).substr(1);
|
|
|
|
to = exports.resolve(to).substr(1);
|
|
|
|
|
|
|
|
function trim(arr) {
|
|
|
|
var start = 0;
|
|
|
|
for (; start < arr.length; start++) {
|
|
|
|
if (arr[start] !== '') break;
|
2013-11-29 15:56:55 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
var end = arr.length - 1;
|
|
|
|
for (; end >= 0; end--) {
|
|
|
|
if (arr[end] !== '') break;
|
2013-11-29 15:56:55 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
if (start > end) return [];
|
|
|
|
return arr.slice(start, end - start + 1);
|
2013-11-29 15:56:55 +00:00
|
|
|
}
|
2012-10-11 20:26:23 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
var fromParts = trim(from.split('/'));
|
|
|
|
var toParts = trim(to.split('/'));
|
2012-10-11 20:26:23 +00:00
|
|
|
|
2014-05-23 18:14:06 +00: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;
|
2012-10-11 20:26:23 +00:00
|
|
|
}
|
2014-05-23 18:14:06 +00:00
|
|
|
}
|
2012-10-11 20:26:23 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
var outputParts = [];
|
|
|
|
for (var i = samePartsLength; i < fromParts.length; i++) {
|
|
|
|
outputParts.push('..');
|
2013-11-29 15:56:55 +00:00
|
|
|
}
|
2012-10-11 20:26:23 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
outputParts = outputParts.concat(toParts.slice(samePartsLength));
|
|
|
|
|
|
|
|
return outputParts.join('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
function dirname(path) {
|
|
|
|
var result = splitPath(path),
|
|
|
|
root = result[0],
|
|
|
|
dir = result[1];
|
|
|
|
|
|
|
|
if (!root && !dir) {
|
|
|
|
// No dirname whatsoever
|
|
|
|
return '.';
|
2013-11-29 15:56:55 +00:00
|
|
|
}
|
2012-10-11 20:26:23 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
if (dir) {
|
|
|
|
// It has a dirname, strip trailing slash
|
|
|
|
dir = dir.substr(0, dir.length - 1);
|
2012-11-09 16:10:55 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
return root + dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
function basename(path, ext) {
|
|
|
|
var f = splitPath(path)[2];
|
|
|
|
// TODO: make this comparison case-insensitive on windows?
|
|
|
|
if (ext && f.substr(-1 * ext.length) === ext) {
|
|
|
|
f = f.substr(0, f.length - ext.length);
|
2014-02-21 17:39:33 +00:00
|
|
|
}
|
2014-11-28 17:48:10 +00:00
|
|
|
// XXXfiler: node.js just does `return f`
|
2014-05-23 18:14:06 +00:00
|
|
|
return f === "" ? "/" : f;
|
|
|
|
}
|
2014-02-21 17:39:33 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
function extname(path) {
|
|
|
|
return splitPath(path)[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
function isAbsolute(path) {
|
|
|
|
if(path.charAt(0) === '/') {
|
|
|
|
return true;
|
2014-02-21 17:39:33 +00:00
|
|
|
}
|
2014-05-23 18:14:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-21 17:39:33 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
function isNull(path) {
|
|
|
|
if (('' + path).indexOf('\u0000') !== -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-28 17:48:10 +00:00
|
|
|
// Make sure we don't double-add a trailing slash (e.g., '/' -> '//')
|
|
|
|
function addTrailing(path) {
|
|
|
|
return path.replace(/\/*$/, '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deal with multiple slashes at the end, one, or none
|
|
|
|
// and make sure we don't return the empty string.
|
|
|
|
function removeTrailing(path) {
|
|
|
|
path = path.replace(/\/*$/, '');
|
|
|
|
return path === '' ? '/' : path;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXXfiler: we don't support path.exists() or path.existsSync(), which
|
2014-05-23 18:14:06 +00:00
|
|
|
// are deprecated, and need a FileSystem instance to work. Use fs.stat().
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
normalize: normalize,
|
|
|
|
resolve: resolve,
|
|
|
|
join: join,
|
|
|
|
relative: relative,
|
|
|
|
sep: '/',
|
|
|
|
delimiter: ':',
|
|
|
|
dirname: dirname,
|
|
|
|
basename: basename,
|
|
|
|
extname: extname,
|
|
|
|
isAbsolute: isAbsolute,
|
2014-11-28 17:48:10 +00:00
|
|
|
isNull: isNull,
|
|
|
|
// Non-node but useful...
|
|
|
|
addTrailing: addTrailing,
|
|
|
|
removeTrailing: removeTrailing
|
2014-05-23 18:14:06 +00:00
|
|
|
};
|