Bob Kerns dc2ec62e70 Use the java facilities to handle path manipulations portably.
This was causing it to mis-calculate the paths, and end up losing the
output directory entirely, and overwrite the template static files
with themselves, resulting in truncation to zero-length. Of course,
they were also missing in the output.

It was expecting to be able to split paths on
System.getProperty("path.separator"), but that describes how to put a
path together, not how to take one apart, which is more complicated.

In particular, in windows, / is as valid a separator as \ in all but a
few UI contexts.

And since we need to pass / in paths that may get turned into URI's,
it's important to handle this correctly.

Java already provides a File facility to handle these sorts of operations.
This patch makes use of java.io.File to:

1) Find the parent of a File for dirname.
2) Find the name of a File for basename.

It also now makes use of substring for removing the ext for basename,
rather than Array.prototype.slice + join(""). I'm not sure what that
was all about...  It does have the effect of ensuring it's a Javascript
String, but calling String() has the same benefit and is much more clear.

You can put that back if there was a reason for it, but it looks just
confused to me.
2012-09-11 22:57:50 -07:00

154 lines
4.2 KiB
JavaScript

var isWindows = java.lang.System.getProperty("os.name").toLowerCase().contains("windows");
var fileSeparator = exports.sep = java.lang.System.getProperty("file.separator");
/**
* Returns everything on a path except for the last item
* e.g. if the path was 'path/to/something', the return value would be 'path/to'
*/
exports.dirname = function(_path) {
var f = new java.io.File(_path);
return String(f.getParent());
};
/**
* Returns the last item on a path
*/
exports.basename = function(_path, ext) {
var f = new java.io.File(_path);
var p = f.getParentFile();
var base = String(f.getName());
if (p != null) {
var idx = ext ? base.indexOf(ext) : -1;
if (idx !== -1) {
base = base.substring(0, base.length - ext.length);
}
}
return base;
};
exports.existsSync = function(_path) {
var f = new java.io.File(_path);
if (f.isDirectory()){
return true;
}
if (!f.exists()){
return false;
}
if (!f.canRead()){
return false;
}
return true;
};
//Code below taken from node
//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--;
}
}
// if the path is allowed to go above the root, restore leading ..s
if (allowAboveRoot) {
for (; up--; up) {
parts.unshift('..');
}
}
return parts;
}
if (isWindows) {
// Regex to split a windows path into three parts: [*, device, slash,
// tail] windows-only
var splitDeviceRe =
/^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?([\\\/])?([\s\S]*?)$/;
// windows version
exports.normalize = function(_path) {
var result = splitDeviceRe.exec(_path),
device = result[1] || '',
isUnc = device && device.charAt(1) !== ':',
isAbsolute = !!result[2] || isUnc, // UNC paths are always absolute
tail = result[3],
trailingSlash = /[\\\/]$/.test(tail);
// Normalize the tail path
tail = normalizeArray(tail.split(/[\\\/]+/).filter(function(p) {
return !!p;
}), !isAbsolute).join('\\');
if (!tail && !isAbsolute) {
tail = '.';
}
if (tail && trailingSlash) {
tail += '\\';
}
return device + (isAbsolute ? '\\' : '') + tail;
};
//windows version
exports.join = function() {
function f(p) {
return p && typeof p === 'string';
}
var _paths = Array.prototype.slice.call(arguments, 0).filter(f);
var joined = _paths.join('\\');
// Make sure that the joined path doesn't start with two slashes
// - it will be mistaken for an unc path by normalize() -
// unless the _paths[0] also starts with two slashes
if (/^[\\\/]{2}/.test(joined) && !/^[\\\/]{2}/.test(_paths[0])) {
joined = joined.slice(1);
}
return exports.normalize(joined);
};
} else {
// path.normalize(_path)
// posix version
exports.normalize = function(_path) {
var isAbsolute = _path.charAt(0) === '/',
trailingSlash = _path.slice(-1) === '/';
// Normalize the path
_path = normalizeArray(_path.split('/').filter(function(p) {
return !!p;
}), !isAbsolute).join('/');
if (!_path && !isAbsolute) {
_path = '.';
}
if (_path && trailingSlash) {
_path += '/';
}
return (isAbsolute ? '/' : '') + _path;
};
// posix version
exports.join = function() {
var _paths = Array.prototype.slice.call(arguments, 0);
return exports.normalize(_paths.filter(function(p, index) {
return p && typeof p === 'string';
}).join('/'));
};
}