123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- var process = Object.create(require('../system'));
- process.cwd = process.pathFor.bind(process, 'CurProcD');
- var isWindows = process.platform.indexOf('win') === 0;
- function normalizeArray(parts, allowAboveRoot) {
-
- 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--;
- }
- }
-
- if (allowAboveRoot) {
- for (; up--; up) {
- parts.unshift('..');
- }
- }
- return parts;
- }
- if (isWindows) {
-
-
- var splitDeviceRe =
- /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/;
-
- var splitTailRe =
- /^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/;
-
-
- var splitPath = function(filename) {
-
- var result = splitDeviceRe.exec(filename),
- device = (result[1] || '') + (result[2] || ''),
- tail = result[3] || '';
-
- var result2 = splitTailRe.exec(tail),
- dir = result2[1],
- basename = result2[2],
- ext = result2[3];
- return [device, dir, basename, ext];
- };
- var normalizeUNCRoot = function(device) {
- return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\');
- };
-
-
- exports.resolve = function() {
- var resolvedDevice = '',
- resolvedTail = '',
- resolvedAbsolute = false;
- for (var i = arguments.length - 1; i >= -1; i--) {
- var path;
- if (i >= 0) {
- path = arguments[i];
- } else if (!resolvedDevice) {
- path = process.cwd();
- } else {
-
-
-
-
- path = process.env['=' + resolvedDevice];
-
-
- if (!path || path.substr(0, 3).toLowerCase() !==
- resolvedDevice.toLowerCase() + '\\') {
- path = resolvedDevice + '\\';
- }
- }
-
- if (typeof path !== 'string') {
- throw new TypeError('Arguments to path.resolve must be strings');
- } else if (!path) {
- continue;
- }
- var result = splitDeviceRe.exec(path),
- device = result[1] || '',
- isUnc = device && device.charAt(1) !== ':',
- isAbsolute = exports.isAbsolute(path),
- tail = result[3];
- if (device &&
- resolvedDevice &&
- device.toLowerCase() !== resolvedDevice.toLowerCase()) {
-
- continue;
- }
- if (!resolvedDevice) {
- resolvedDevice = device;
- }
- if (!resolvedAbsolute) {
- resolvedTail = tail + '\\' + resolvedTail;
- resolvedAbsolute = isAbsolute;
- }
- if (resolvedDevice && resolvedAbsolute) {
- break;
- }
- }
-
-
- if (isUnc) {
- resolvedDevice = normalizeUNCRoot(resolvedDevice);
- }
-
-
-
-
- function f(p) {
- return !!p;
- }
- resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/).filter(f),
- !resolvedAbsolute).join('\\');
- return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
- '.';
- };
-
- exports.normalize = function(path) {
- var result = splitDeviceRe.exec(path),
- device = result[1] || '',
- isUnc = device && device.charAt(1) !== ':',
- isAbsolute = exports.isAbsolute(path),
- tail = result[3],
- trailingSlash = /[\\\/]$/.test(tail);
-
- if (device && device.charAt(1) === ':') {
- device = device[0].toLowerCase() + device.substr(1);
- }
-
- tail = normalizeArray(tail.split(/[\\\/]+/).filter(function(p) {
- return !!p;
- }), !isAbsolute).join('\\');
- if (!tail && !isAbsolute) {
- tail = '.';
- }
- if (tail && trailingSlash) {
- tail += '\\';
- }
-
-
- if (isUnc) {
- device = normalizeUNCRoot(device);
- }
- return device + (isAbsolute ? '\\' : '') + tail;
- };
-
- exports.isAbsolute = function(path) {
- var result = splitDeviceRe.exec(path),
- device = result[1] || '',
- isUnc = device && device.charAt(1) !== ':';
-
- return !!result[2] || isUnc;
- };
-
- exports.join = function() {
- function f(p) {
- if (typeof p !== 'string') {
- throw new TypeError('Arguments to path.join must be strings');
- }
- return p;
- }
- var paths = Array.prototype.filter.call(arguments, f);
- var joined = paths.join('\\');
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (!/^[\\\/]{2}[^\\\/]/.test(paths[0])) {
- joined = joined.replace(/^[\\\/]{2,}/, '\\');
- }
- return exports.normalize(joined);
- };
-
-
-
-
-
-
- exports.relative = function(from, to) {
- from = exports.resolve(from);
- to = exports.resolve(to);
-
- var lowerFrom = from.toLowerCase();
- var lowerTo = to.toLowerCase();
- function trim(arr) {
- var start = 0;
- for (; start < arr.length; start++) {
- if (arr[start] !== '') break;
- }
- var end = arr.length - 1;
- for (; end >= 0; end--) {
- if (arr[end] !== '') break;
- }
- if (start > end) return [];
- return arr.slice(start, end - start + 1);
- }
- var toParts = trim(to.split('\\'));
- var lowerFromParts = trim(lowerFrom.split('\\'));
- var lowerToParts = trim(lowerTo.split('\\'));
- 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;
- }
- }
- if (samePartsLength == 0) {
- return to;
- }
- var outputParts = [];
- for (var i = samePartsLength; i < lowerFromParts.length; i++) {
- outputParts.push('..');
- }
- outputParts = outputParts.concat(toParts.slice(samePartsLength));
- return outputParts.join('\\');
- };
- exports.sep = '\\';
- exports.delimiter = ';';
- } else {
-
-
- var splitPathRe =
- /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
- var splitPath = function(filename) {
- return splitPathRe.exec(filename).slice(1);
- };
-
-
- exports.resolve = function() {
- var resolvedPath = '',
- resolvedAbsolute = false;
- for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
- var path = (i >= 0) ? arguments[i] : process.cwd();
-
- if (typeof path !== 'string') {
- throw new TypeError('Arguments to path.resolve must be strings');
- } else if (!path) {
- continue;
- }
- resolvedPath = path + '/' + resolvedPath;
- resolvedAbsolute = path.charAt(0) === '/';
- }
-
-
-
- resolvedPath = normalizeArray(resolvedPath.split('/').filter(function(p) {
- return !!p;
- }), !resolvedAbsolute).join('/');
- return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
- };
-
-
- exports.normalize = function(path) {
- var isAbsolute = exports.isAbsolute(path),
- trailingSlash = path.substr(-1) === '/';
-
- path = normalizeArray(path.split('/').filter(function(p) {
- return !!p;
- }), !isAbsolute).join('/');
- if (!path && !isAbsolute) {
- path = '.';
- }
- if (path && trailingSlash) {
- path += '/';
- }
- return (isAbsolute ? '/' : '') + path;
- };
-
- exports.isAbsolute = function(path) {
- return path.charAt(0) === '/';
- };
-
- exports.join = function() {
- var paths = Array.prototype.slice.call(arguments, 0);
- return exports.normalize(paths.filter(function(p, index) {
- if (typeof p !== 'string') {
- throw new TypeError('Arguments to path.join must be strings');
- }
- return p;
- }).join('/'));
- };
-
-
- exports.relative = function(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;
- }
- var end = arr.length - 1;
- for (; end >= 0; end--) {
- if (arr[end] !== '') break;
- }
- if (start > end) return [];
- return arr.slice(start, end - start + 1);
- }
- var fromParts = trim(from.split('/'));
- var toParts = trim(to.split('/'));
- 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 = [];
- for (var i = samePartsLength; i < fromParts.length; i++) {
- outputParts.push('..');
- }
- outputParts = outputParts.concat(toParts.slice(samePartsLength));
- return outputParts.join('/');
- };
- exports.sep = '/';
- exports.delimiter = ':';
- }
- exports.dirname = function(path) {
- var result = splitPath(path),
- root = result[0],
- dir = result[1];
- if (!root && !dir) {
-
- return '.';
- }
- if (dir) {
-
- dir = dir.substr(0, dir.length - 1);
- }
- return root + dir;
- };
- exports.basename = function(path, ext) {
- var f = splitPath(path)[2];
-
- if (ext && f.substr(-1 * ext.length) === ext) {
- f = f.substr(0, f.length - ext.length);
- }
- return f;
- };
- exports.extname = function(path) {
- return splitPath(path)[3];
- };
- if (isWindows) {
- exports._makeLong = function(path) {
-
- if (typeof path !== 'string')
- return path;
- if (!path) {
- return '';
- }
- var resolvedPath = exports.resolve(path);
- if (/^[a-zA-Z]\:\\/.test(resolvedPath)) {
-
-
- return '\\\\?\\' + resolvedPath;
- } else if (/^\\\\[^?.]/.test(resolvedPath)) {
-
-
- return '\\\\?\\UNC\\' + resolvedPath.substring(2);
- }
- return path;
- };
- } else {
- exports._makeLong = function(path) {
- return path;
- };
- }
|