You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.1 KiB

function validPathPart(p) {
return typeof p === 'string' && p;
}
14 years ago
exports.join = function() {
var args = Array.prototype.slice.call(arguments);
return exports.normalizeArray(args).join('/');
};
exports.split = function(path) {
// split based on / and \, but only if that / is not at the start or end.
return exports.normalizeArray(path.split(/^|[\\\/](?!$)/));
};
function cleanArray(parts) {
var i = 0;
var l = parts.length - 1;
var stripped = false;
// strip leading empty args
while (i < l && !validPathPart(parts[i])) {
stripped = true;
14 years ago
i++;
}
// strip tailing empty args
while (l >= i && !validPathPart(parts[l])) {
stripped = true;
14 years ago
l--;
}
if (stripped) {
// if l chopped all the way back to i, then this is empty
parts = Array.prototype.slice.call(parts, i, l + 1);
}
return parts.filter(function(p) { return validPathPart(p) })
.join('/')
.split(/^|[\\\/](?!$)/);
}
exports.normalizeArray = function(original) {
var parts = cleanArray(original);
14 years ago
if (!parts.length || (parts.length === 1 && !parts[0])) return ['.'];
// now we're fully ready to rock.
// leading/trailing invalids have been stripped off.
// if it comes in starting with a slash, or ending with a slash,
14 years ago
var leadingSlash = (parts[0].charAt(0) === '/');
if (leadingSlash) parts[0] = parts[0].substr(1);
var last = parts.slice(-1)[0];
14 years ago
var tailingSlash = (last.substr(-1) === '/');
if (tailingSlash) parts[parts.length - 1] = last.slice(0, -1);
var directories = [];
var prev;
14 years ago
for (var i = 0, l = parts.length - 1; i <= l; i++) {
var directory = parts[i];
// if it's blank, and we're not keeping blanks, then skip it.
if (directory === '') continue;
// if it's a dot, then skip it
14 years ago
if (directory === '.' && (directories.length ||
(i === 0 && !(tailingSlash && i === l)) ||
(i === 0 && leadingSlash))) continue;
// if we're dealing with an absolute path, then discard ..s that go
// above that the base.
14 years ago
if (leadingSlash && directories.length === 0 && directory === '..') {
continue;
}
// trying to go up a dir
14 years ago
if (directory === '..' && directories.length && prev !== '..' &&
prev !== undefined) {
directories.pop();
prev = directories.slice(-1)[0];
} else {
directories.push(directory);
prev = directory;
}
}
if (!directories.length) {
14 years ago
directories = [leadingSlash || tailingSlash ? '' : '.'];
}
var last = directories.slice(-1)[0];
14 years ago
if (tailingSlash && last.substr(-1) !== '/') {
directories[directories.length - 1] += '/';
}
14 years ago
if (leadingSlash && directories[0].charAt(0) !== '/') {
if (directories[0] === '.') directories[0] = '';
directories[0] = '/' + directories[0];
}
return directories;
};
exports.normalize = function(path) {
return exports.join(path);
};
14 years ago
exports.dirname = function(path) {
if (path.length > 1 && '\\/'.indexOf(path[path.length-1]) != -1) {
path = path.replace(/\/+$/, '');
}
var lastSlash = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
switch (lastSlash) {
case -1:
return '.';
case 0:
return '/';
default:
return path.substring(0, lastSlash);
}
};
14 years ago
exports.basename = function(path, ext) {
var f = path.substr(path.lastIndexOf('/') + 1);
if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length);
}
return f;
};
14 years ago
exports.extname = function(path) {
var dot = path.lastIndexOf('.'),
slash = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
// The last dot must be in the last path component, and it (the last dot) must
// not start the last path component (i.e. be a dot that signifies a hidden
// file in UNIX).
return dot <= slash + 1 ? '' : path.substring(dot);
};
14 years ago
exports.exists = function(path, callback) {
process.binding('fs').stat(path, function(err, stats) {
if (callback) callback(err ? false : true);
});
};
14 years ago
exports.existsSync = function(path) {
try {
process.binding('fs').stat(path);
return true;
14 years ago
} catch (e) {
return false;
}
};