Merge pull request #165 from hegemonic/fs-path-fixes

Make 'fs' and 'path' modules closer to their Node.js equivalents
This commit is contained in:
Jeff Williams 2012-08-19 16:56:01 -07:00
commit 2bf4af851d
8 changed files with 145 additions and 91 deletions

View File

@ -21,6 +21,12 @@ env = {
finish: null
},
/**
The type of VM that is executing jsdoc.
@type string
*/
vm: '',
/**
The command line arguments passed into jsdoc.
@type Array
@ -120,6 +126,23 @@ function exit(n) {
java.lang.System.exit(n);
}
/**
Detect the type of VM running jsdoc.
**Note**: Rhino is the only VM that is currently supported.
@return {string} rhino|node
*/
function detectVm() {
if (typeof Packages === "object" &&
Object.prototype.toString.call(Packages) === "[object JavaPackage]") {
return "rhino";
} else if ( require && require.main && module && (require.main === module) ) {
return "node";
} else {
// unknown VM
return;
}
}
function installPlugins(plugins, p) {
var dictionary = require('jsdoc/tag/dictionary'),
parser = p || app.jsdoc.parser;
@ -196,6 +219,8 @@ function main() {
env.opts = jsdoc.opts.parser.parse(env.args);
env.vm = detectVm();
try {
env.conf = new Config( fs.readFileSync( env.opts.configure || env.dirname + '/conf.json' ) ).get();
}

View File

@ -1,12 +1,17 @@
/*global Packages: true */
var path = require('path');
exports.readFileSync = function(filename, encoding) {
encoding = encoding || 'utf-8';
return readFile(filename, encoding);
};
var stat = exports.stat = exports.statSync = function(path, encoding) {
var f = new java.io.File(path);
// in node 0.8, path.exists() and path.existsSync() moved to the "fs" module
exports.existsSync = path.existsSync;
var statSync = exports.statSync = function(_path) {
var f = new java.io.File(_path);
return {
isFile: function() {
return f.isFile();
@ -15,14 +20,13 @@ var stat = exports.stat = exports.statSync = function(path, encoding) {
return f.isDirectory();
}
};
};
var readdirSync = exports.readdirSync = function(path) {
var readdirSync = exports.readdirSync = function(_path) {
var dir,
files;
dir = new java.io.File(path);
dir = new java.io.File(_path);
if (!dir.directory) { return [String(dir)]; }
files = dir.list();
@ -35,6 +39,8 @@ var readdirSync = exports.readdirSync = function(path) {
return files;
};
// TODO: not part of node's "fs" module
// for node, could use wrench.readdirSyncRecursive(), although it doesn't take a 'recurse' param
var ls = exports.ls = function(dir, recurse, _allFiles, _path) {
var files,
file;
@ -47,7 +53,7 @@ var ls = exports.ls = function(dir, recurse, _allFiles, _path) {
if (_path.length === 0) { return _allFiles; }
if (typeof recurse === 'undefined') { recurse = 1; }
if ( stat(dir).isFile(dir) ) {
if ( statSync(dir).isFile(dir) ) {
files = [dir];
}
else {
@ -77,57 +83,33 @@ var ls = exports.ls = function(dir, recurse, _allFiles, _path) {
return _allFiles;
};
var toDir = exports.toDir = function(path) {
var f = new java.io.File(path);
// TODO: not part of node's "fs" module
var toDir = exports.toDir = function(_path) {
var f = new java.io.File(_path);
if (f.isDirectory()){
return path;
}
var parts = path.split(/[\\\/]/);
parts.pop();
return parts.join('/');
};
function makeDir(/**string*/ path) {
var dirPath = toDir(path);
(new java.io.File(dirPath)).mkdir();
}
function exists(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;
}
exports.mkPath = function(/**Array*/ path) {
if (path.constructor != Array){path = path.split(/[\\\/]/);}
var make = "";
for (var i = 0, l = path.length; i < l; i++) {
make += path[i] + '/';
if (! exists(make)) {
makeDir(make);
}
return _path;
} else {
return path.dirname(_path);
}
};
var toFile = exports.toFile = function(path) {
var parts = path.split(/[\\\/]/);
return parts.pop();
var mkdirSync = exports.mkdirSync = function(/**string*/ _path) {
var dir_path = toDir(_path);
(new java.io.File(dir_path)).mkdir();
};
exports.copyFile = function(inFile, outDir, fileName) {
if (fileName == null){fileName = toFile(inFile);}
// TODO: not part of node's "fs" module
// for node, could use: https://github.com/substack/node-mkdirp
exports.mkPath = function(/**Array*/ _path) {
if (_path.constructor == Array) { _path = _path.join(""); }
(new java.io.File(_path)).mkdirs();
};
// TODO: not part of node's "fs" module
exports.copyFileSync = function(inFile, outDir, fileName) {
if (fileName == null){fileName = path.basename(inFile);}
outDir = toDir(outDir);

View File

@ -37,7 +37,7 @@ exports.Scanner.prototype.scan = function(searchPaths, depth, filter) {
searchPaths.forEach(function($) {
var filepath = decodeURIComponent($);
if ( fs.stat(filepath).isFile() ) {
if ( fs.statSync(filepath).isFile() ) {
filePaths.push(filepath);
}
else {

View File

@ -10,6 +10,7 @@
var tutorial = require('jsdoc/tutorial'),
fs = require('fs'),
path = require('path'),
hasOwnProp = Object.prototype.hasOwnProperty,
conf = {},
tutorials = {},
@ -39,15 +40,15 @@ exports.root.getByName = function(name) {
};
/** Load tutorials from given path.
@param {string} path - Tutorials directory.
@param {string} _path - Tutorials directory.
*/
exports.load = function(path) {
exports.load = function(_path) {
var match,
type,
name,
content,
current,
files = fs.ls(path);
files = fs.ls(_path);
// tutorials handling
files.forEach(function(file) {
@ -55,7 +56,7 @@ exports.load = function(path) {
// any filetype that can apply to tutorials
if (match) {
name = fs.toFile(match[1]);
name = path.basename(match[1]);
content = fs.readFileSync(file);
switch (match[2].toLowerCase()) {

View File

@ -1,37 +1,48 @@
var isWindows = java.lang.System.getProperty("os.name").toLowerCase().contains("windows");
var fileSeparator = java.lang.System.getProperty("file.separator");
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.basename = function(path) {
var parts = path.split(fileSeparator);
exports.dirname = function(_path) {
var parts = _path.split(fileSeparator);
parts.pop();
path = parts.join(fileSeparator);
return path;
_path = parts.join(fileSeparator);
return _path;
};
/**
* Returns the last item on a path
*/
exports.filename = function(path) {
var parts = path.split(fileSeparator);
exports.basename = function(_path, ext) {
var base,
idx,
parts = _path.split(fileSeparator);
if (parts.length > 0) {
return parts.pop();
base = parts.pop();
idx = ext ? base.indexOf(ext) : -1;
if (idx !== -1) {
base = Array.prototype.slice.call(base, 0, base.length - ext.length).join("");
}
}
return null;
return base;
};
exports.existsSync = function(path) {
var file = new java.io.File(path);
exports.existsSync = function(_path) {
var f = new java.io.File(_path);
if (file.isFile()) {
return true;
if (f.isDirectory()){
return true;
}
return false;
if (!f.exists()){
return false;
}
if (!f.canRead()){
return false;
}
return true;
};
//Code below taken from node
@ -73,8 +84,8 @@ if (isWindows) {
/^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?([\\\/])?([\s\S]*?)$/;
// windows version
exports.normalize = function(path) {
var result = splitDeviceRe.exec(path),
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
@ -102,44 +113,44 @@ if (isWindows) {
return p && typeof p === 'string';
}
var paths = Array.prototype.slice.call(arguments, 0).filter(f);
var joined = paths.join('\\');
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])) {
// 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)
// path.normalize(_path)
// posix version
exports.normalize = function(path) {
var isAbsolute = path.charAt(0) === '/',
trailingSlash = path.slice(-1) === '/';
exports.normalize = function(_path) {
var isAbsolute = _path.charAt(0) === '/',
trailingSlash = _path.slice(-1) === '/';
// Normalize the path
path = normalizeArray(path.split('/').filter(function(p) {
_path = normalizeArray(_path.split('/').filter(function(p) {
return !!p;
}), !isAbsolute).join('/');
if (!path && !isAbsolute) {
path = '.';
if (!_path && !isAbsolute) {
_path = '.';
}
if (path && trailingSlash) {
path += '/';
if (_path && trailingSlash) {
_path += '/';
}
return (isAbsolute ? '/' : '') + 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) {
var _paths = Array.prototype.slice.call(arguments, 0);
return exports.normalize(_paths.filter(function(p, index) {
return p && typeof p === 'string';
}).join('/'));
};

View File

@ -175,7 +175,7 @@
mixins = find({kind: 'mixin'}),
namespaces = find({kind: 'namespace'});
var outdir = opts.destination;
var outdir = env.dirname + '/' + opts.destination;
if (packageInfo && packageInfo.name) {
outdir += '/' + packageInfo.name + '/' + packageInfo.version + '/';
}
@ -188,7 +188,7 @@
staticFiles.forEach(function(fileName) {
var toDir = fs.toDir(fileName.replace(fromDir, outdir));
fs.mkPath(toDir);
fs.copyFile(fileName, toDir);
fs.copyFileSync(fileName, toDir);
});
function linkto(longname, linktext) {

View File

@ -36,7 +36,7 @@ exports.load = function(loadpath, matcher, clear) {
var file = path.join(env.dirname, loadpath, wannaBeSpecs[i]);
try {
if (fs.statSync(file).isFile()) {
if (!/.*nodejs_modules.*/.test(file) && matcher.test(path.filename(file))) {
if (!/.*nodejs_modules.*/.test(file) && matcher.test(path.basename(file))) {
specs.push(createSpecObj(file));
}
}

35
test/specs/path.js Normal file
View File

@ -0,0 +1,35 @@
/*global describe: true, expect: true, it: true */
describe("path", function() {
// TODO: more tests
var path = require('path');
var pathChunks = [
"foo",
"bar",
"baz",
"qux.html"
];
var joinedPath = path.join.apply(this, pathChunks);
describe("basename", function() {
it("should exist", function() {
expect(path.basename).toBeDefined();
});
it("should be a function", function() {
expect(typeof path.basename).toEqual("function");
});
it("should work correctly without an 'ext' parameter", function() {
expect( path.basename(joinedPath) ).toEqual( pathChunks[pathChunks.length - 1] );
});
it("should work correctly with an 'ext' parameter", function() {
var fn = pathChunks[pathChunks.length - 1],
ext = Array.prototype.slice.call( fn, fn.indexOf(".") ).join("");
bn = Array.prototype.slice.call( fn, 0, fn.indexOf(".") ).join("");
expect( path.basename(joinedPath, ext) ).toEqual(bn);
});
});
});