react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / parents / node_modules / path-platform / path.js
80738 views// Copyright Joyent, Inc. and other Node contributors.1//2// Permission is hereby granted, free of charge, to any person obtaining a3// copy of this software and associated documentation files (the4// "Software"), to deal in the Software without restriction, including5// without limitation the rights to use, copy, modify, merge, publish,6// distribute, sublicense, and/or sell copies of the Software, and to permit7// persons to whom the Software is furnished to do so, subject to the8// following conditions:9//10// The above copyright notice and this permission notice shall be included11// in all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF15// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN16// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,17// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19// USE OR OTHER DEALINGS IN THE SOFTWARE.202122var _path = require('path');2324// we are new enough we already have this from the system, just export the25// system then26if (_path.posix) {27module.exports = _path;28return;29}3031var isWindows = process.platform === 'win32';32var util = require('util');333435// resolves . and .. elements in a path array with directory names there36// must be no slashes, empty elements, or device names (c:\) in the array37// (so also no leading and trailing slashes - it does not distinguish38// relative and absolute paths)39function normalizeArray(parts, allowAboveRoot) {40// if the path tries to go above the root, `up` ends up > 041var up = 0;42for (var i = parts.length - 1; i >= 0; i--) {43var last = parts[i];44if (last === '.') {45parts.splice(i, 1);46} else if (last === '..') {47parts.splice(i, 1);48up++;49} else if (up) {50parts.splice(i, 1);51up--;52}53}5455// if the path is allowed to go above the root, restore leading ..s56if (allowAboveRoot) {57for (; up--; up) {58parts.unshift('..');59}60}6162return parts;63}646566// Regex to split a windows path into three parts: [*, device, slash,67// tail] windows-only68var splitDeviceRe =69/^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/;7071// Regex to split the tail part of the above into [*, dir, basename, ext]72var splitTailRe =73/^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/;7475var normalizeUNCRoot = function(device) {76return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\');77};787980var win32 = {};8182// Function to split a filename into [root, dir, basename, ext]83win32.splitPath = function(filename) {84// Separate device+slash from tail85var result = splitDeviceRe.exec(filename),86device = (result[1] || '') + (result[2] || ''),87tail = result[3] || '';88// Split the tail into dir, basename and extension89var result2 = splitTailRe.exec(tail),90dir = result2[1],91basename = result2[2],92ext = result2[3];93return [device, dir, basename, ext];94};959697// path.resolve([from ...], to)98win32.resolve = function() {99var resolvedDevice = '',100resolvedTail = '',101resolvedAbsolute = false;102103for (var i = arguments.length - 1; i >= -1; i--) {104var path;105if (i >= 0) {106path = arguments[i];107} else if (!resolvedDevice) {108path = process.cwd();109} else {110// Windows has the concept of drive-specific current working111// directories. If we've resolved a drive letter but not yet an112// absolute path, get cwd for that drive. We're sure the device is not113// an unc path at this points, because unc paths are always absolute.114path = process.env['=' + resolvedDevice];115// Verify that a drive-local cwd was found and that it actually points116// to our drive. If not, default to the drive's root.117if (!path || path.substr(0, 3).toLowerCase() !==118resolvedDevice.toLowerCase() + '\\') {119path = resolvedDevice + '\\';120}121}122123// Skip empty and invalid entries124if (typeof path !== 'string') {125throw new TypeError('Arguments to path.resolve must be strings');126} else if (!path) {127continue;128}129130var result = splitDeviceRe.exec(path),131device = result[1] || '',132isUnc = device && device.charAt(1) !== ':',133isAbsolute = win32.isAbsolute(path),134tail = result[3];135136if (device &&137resolvedDevice &&138device.toLowerCase() !== resolvedDevice.toLowerCase()) {139// This path points to another device so it is not applicable140continue;141}142143if (!resolvedDevice) {144resolvedDevice = device;145}146if (!resolvedAbsolute) {147resolvedTail = tail + '\\' + resolvedTail;148resolvedAbsolute = isAbsolute;149}150151if (resolvedDevice && resolvedAbsolute) {152break;153}154}155156// Convert slashes to backslashes when `resolvedDevice` points to an UNC157// root. Also squash multiple slashes into a single one where appropriate.158if (isUnc) {159resolvedDevice = normalizeUNCRoot(resolvedDevice);160}161162// At this point the path should be resolved to a full absolute path,163// but handle relative paths to be safe (might happen when process.cwd()164// fails)165166// Normalize the tail path167168function f(p) {169return !!p;170}171172resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/).filter(f),173!resolvedAbsolute).join('\\');174175return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||176'.';177};178179180win32.normalize = function(path) {181var result = splitDeviceRe.exec(path),182device = result[1] || '',183isUnc = device && device.charAt(1) !== ':',184isAbsolute = win32.isAbsolute(path),185tail = result[3],186trailingSlash = /[\\\/]$/.test(tail);187188// If device is a drive letter, we'll normalize to lower case.189if (device && device.charAt(1) === ':') {190device = device[0].toLowerCase() + device.substr(1);191}192193// Normalize the tail path194tail = normalizeArray(tail.split(/[\\\/]+/).filter(function(p) {195return !!p;196}), !isAbsolute).join('\\');197198if (!tail && !isAbsolute) {199tail = '.';200}201if (tail && trailingSlash) {202tail += '\\';203}204205// Convert slashes to backslashes when `device` points to an UNC root.206// Also squash multiple slashes into a single one where appropriate.207if (isUnc) {208device = normalizeUNCRoot(device);209}210211return device + (isAbsolute ? '\\' : '') + tail;212};213214215win32.isAbsolute = function(path) {216var result = splitDeviceRe.exec(path),217device = result[1] || '',218isUnc = device && device.charAt(1) !== ':';219// UNC paths are always absolute220return !!result[2] || isUnc;221};222223224win32.join = function() {225function f(p) {226if (typeof p !== 'string') {227throw new TypeError('Arguments to path.join must be strings');228}229return p;230}231232var paths = Array.prototype.filter.call(arguments, f);233var joined = paths.join('\\');234235// Make sure that the joined path doesn't start with two slashes, because236// normalize() will mistake it for an UNC path then.237//238// This step is skipped when it is very clear that the user actually239// intended to point at an UNC path. This is assumed when the first240// non-empty string arguments starts with exactly two slashes followed by241// at least one more non-slash character.242//243// Note that for normalize() to treat a path as an UNC path it needs to244// have at least 2 components, so we don't filter for that here.245// This means that the user can use join to construct UNC paths from246// a server name and a share name; for example:247// path.join('//server', 'share') -> '\\\\server\\share\')248if (!/^[\\\/]{2}[^\\\/]/.test(paths[0])) {249joined = joined.replace(/^[\\\/]{2,}/, '\\');250}251252return win32.normalize(joined);253};254255256// path.relative(from, to)257// it will solve the relative path from 'from' to 'to', for instance:258// from = 'C:\\orandea\\test\\aaa'259// to = 'C:\\orandea\\impl\\bbb'260// The output of the function should be: '..\\..\\impl\\bbb'261win32.relative = function(from, to) {262from = win32.resolve(from);263to = win32.resolve(to);264265// windows is not case sensitive266var lowerFrom = from.toLowerCase();267var lowerTo = to.toLowerCase();268269function trim(arr) {270var start = 0;271for (; start < arr.length; start++) {272if (arr[start] !== '') break;273}274275var end = arr.length - 1;276for (; end >= 0; end--) {277if (arr[end] !== '') break;278}279280if (start > end) return [];281return arr.slice(start, end - start + 1);282}283284var toParts = trim(to.split('\\'));285286var lowerFromParts = trim(lowerFrom.split('\\'));287var lowerToParts = trim(lowerTo.split('\\'));288289var length = Math.min(lowerFromParts.length, lowerToParts.length);290var samePartsLength = length;291for (var i = 0; i < length; i++) {292if (lowerFromParts[i] !== lowerToParts[i]) {293samePartsLength = i;294break;295}296}297298if (samePartsLength == 0) {299return to;300}301302var outputParts = [];303for (var i = samePartsLength; i < lowerFromParts.length; i++) {304outputParts.push('..');305}306307outputParts = outputParts.concat(toParts.slice(samePartsLength));308309return outputParts.join('\\');310};311312313win32._makeLong = function(path) {314// Note: this will *probably* throw somewhere.315if (typeof path !== 'string')316return path;317318if (!path) {319return '';320}321322var resolvedPath = win32.resolve(path);323324if (/^[a-zA-Z]\:\\/.test(resolvedPath)) {325// path is local filesystem path, which needs to be converted326// to long UNC path.327return '\\\\?\\' + resolvedPath;328} else if (/^\\\\[^?.]/.test(resolvedPath)) {329// path is network UNC path, which needs to be converted330// to long UNC path.331return '\\\\?\\UNC\\' + resolvedPath.substring(2);332}333334return path;335};336337338win32.dirname = function(path) {339var result = win32.splitPath(path),340root = result[0],341dir = result[1];342343if (!root && !dir) {344// No dirname whatsoever345return '.';346}347348if (dir) {349// It has a dirname, strip trailing slash350dir = dir.substr(0, dir.length - 1);351}352353return root + dir;354};355356357win32.basename = function(path, ext) {358var f = win32.splitPath(path)[2];359// TODO: make this comparison case-insensitive on windows?360if (ext && f.substr(-1 * ext.length) === ext) {361f = f.substr(0, f.length - ext.length);362}363return f;364};365366367win32.extname = function(path) {368return win32.splitPath(path)[3];369};370371372win32.sep = '\\';373win32.delimiter = ';';374375376// Split a filename into [root, dir, basename, ext], unix version377// 'root' is just a slash, or nothing.378var splitPathRe =379/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;380var posix = {};381382383posix.splitPath = function(filename) {384return splitPathRe.exec(filename).slice(1);385};386387388// path.resolve([from ...], to)389// posix version390posix.resolve = function() {391var resolvedPath = '',392resolvedAbsolute = false;393394for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {395var path = (i >= 0) ? arguments[i] : process.cwd();396397// Skip empty and invalid entries398if (typeof path !== 'string') {399throw new TypeError('Arguments to path.resolve must be strings');400} else if (!path) {401continue;402}403404resolvedPath = path + '/' + resolvedPath;405resolvedAbsolute = path.charAt(0) === '/';406}407408// At this point the path should be resolved to a full absolute path, but409// handle relative paths to be safe (might happen when process.cwd() fails)410411// Normalize the path412resolvedPath = normalizeArray(resolvedPath.split('/').filter(function(p) {413return !!p;414}), !resolvedAbsolute).join('/');415416return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';417};418419// path.normalize(path)420// posix version421posix.normalize = function(path) {422var isAbsolute = posix.isAbsolute(path),423trailingSlash = path.substr(-1) === '/';424425// Normalize the path426path = normalizeArray(path.split('/').filter(function(p) {427return !!p;428}), !isAbsolute).join('/');429430if (!path && !isAbsolute) {431path = '.';432}433if (path && trailingSlash) {434path += '/';435}436437return (isAbsolute ? '/' : '') + path;438};439440// posix version441posix.isAbsolute = function(path) {442return path.charAt(0) === '/';443};444445// posix version446posix.join = function() {447var paths = Array.prototype.slice.call(arguments, 0);448return posix.normalize(paths.filter(function(p, index) {449if (typeof p !== 'string') {450throw new TypeError('Arguments to path.join must be strings');451}452return p;453}).join('/'));454};455456457// path.relative(from, to)458// posix version459posix.relative = function(from, to) {460from = posix.resolve(from).substr(1);461to = posix.resolve(to).substr(1);462463function trim(arr) {464var start = 0;465for (; start < arr.length; start++) {466if (arr[start] !== '') break;467}468469var end = arr.length - 1;470for (; end >= 0; end--) {471if (arr[end] !== '') break;472}473474if (start > end) return [];475return arr.slice(start, end - start + 1);476}477478var fromParts = trim(from.split('/'));479var toParts = trim(to.split('/'));480481var length = Math.min(fromParts.length, toParts.length);482var samePartsLength = length;483for (var i = 0; i < length; i++) {484if (fromParts[i] !== toParts[i]) {485samePartsLength = i;486break;487}488}489490var outputParts = [];491for (var i = samePartsLength; i < fromParts.length; i++) {492outputParts.push('..');493}494495outputParts = outputParts.concat(toParts.slice(samePartsLength));496497return outputParts.join('/');498};499500501posix._makeLong = function(path) {502return path;503};504505506posix.dirname = function(path) {507var result = posix.splitPath(path),508root = result[0],509dir = result[1];510511if (!root && !dir) {512// No dirname whatsoever513return '.';514}515516if (dir) {517// It has a dirname, strip trailing slash518dir = dir.substr(0, dir.length - 1);519}520521return root + dir;522};523524525posix.basename = function(path, ext) {526var f = posix.splitPath(path)[2];527// TODO: make this comparison case-insensitive on windows?528if (ext && f.substr(-1 * ext.length) === ext) {529f = f.substr(0, f.length - ext.length);530}531return f;532};533534535posix.extname = function(path) {536return posix.splitPath(path)[3];537};538539540posix.sep = '/';541posix.delimiter = ':';542543544var splitPath;545if (isWindows) {546splitPath = win32.splitPath;547module.exports = win32;548} else /* posix */ {549splitPath = posix.splitPath;550module.exports = posix;551}552553554module.exports.posix = posix;555module.exports.win32 = win32;556557558