search config file path for resources; use the working directory to find the common path prefix

This commit is contained in:
Jeff Williams 2013-12-01 09:38:49 -08:00
parent 44ff7255e2
commit 9a7b9efe14
2 changed files with 25 additions and 21 deletions

View File

@ -18,8 +18,7 @@ function prefixReducer(previousPath, current) {
return currentPath;
}
// TODO: should probably replace process.cwd() with the CWD before launching JSDoc
currentPath = path.resolve( process.cwd(), path.dirname(current) ).split(path.sep) || [];
currentPath = path.resolve( global.env.pwd, path.dirname(current) ).split(path.sep) || [];
if (previousPath && currentPath.length) {
// remove chunks that exceed the previous path's length
@ -93,8 +92,9 @@ exports._uriToPath = require( runtime.getModulePath('path') ).uriToPath;
/**
* Retrieve the fully qualified path to the requested resource.
*
* If the resource path is specified as a relative path, JSDoc searches for the path in the current
* working directory, then in the JSDoc directory.
* If the resource path is specified as a relative path, JSDoc searches for the path in the
* directory where the JSDoc configuration file is located, then in the current working directory,
* and finally in the JSDoc directory.
*
* If the resource path is specified as a fully qualified path, JSDoc uses the path as-is.
*
@ -105,7 +105,7 @@ exports._uriToPath = require( runtime.getModulePath('path') ).uriToPath;
* Includes the filename if one was provided.
*/
exports.getResourcePath = function(filepath, filename) {
var result;
var result = null;
function pathExists(_path) {
try {
@ -118,16 +118,15 @@ exports.getResourcePath = function(filepath, filename) {
return true;
}
// first, try resolving it relative to the working directory (or just normalize it if it's an
// absolute path)
result = path.resolve(env.pwd, filepath);
if ( !pathExists(result) ) {
// next, try resolving it relative to the JSDoc directory
result = path.resolve(env.dirname, filepath);
if ( !pathExists(result) ) {
result = null;
// absolute paths are normalized by path.resolve on the first pass
[global.env.opts.configure, env.pwd, env.dirname].forEach(function(_path) {
if (!result && _path) {
_path = path.resolve(_path, filepath);
if ( pathExists(_path) ) {
result = _path;
}
}
}
});
if (result) {
result = filename ? path.join(result, filename) : result;

View File

@ -1,4 +1,5 @@
/*global beforeEach: true, describe: true, expect: true, it: true, spyOn: true, xdescribe: true */
/*global afterEach: true, beforeEach: true, describe: true, expect: true, it: true, spyOn: true,
xdescribe: true */
describe('jsdoc/path', function() {
var os = require('os');
@ -30,14 +31,20 @@ describe('jsdoc/path', function() {
});
describe('commonPrefix', function() {
var oldPwd;
var cwd;
beforeEach(function() {
spyOn(process, 'cwd').andCallFake(function() {
return os.platform().match(/^win/) ? 'C:\\Users\\jsdoc' : '/Users/jsdoc';
});
oldPwd = global.env.pwd;
global.env.pwd = os.platform().match(/^win/) ? 'C:\\Users\\jsdoc' : '/Users/jsdoc';
cwd = global.env.pwd.split(path.sep);
});
afterEach(function() {
global.env.pwd = oldPwd;
});
it('finds the correct prefix for a group of relative paths', function() {
var cwd = process.cwd().split(path.sep);
var paths = [
path.join('foo', 'bar', 'baz', 'qux.js'),
path.join('foo', 'bar', 'baz', 'quux.js'),
@ -50,7 +57,6 @@ describe('jsdoc/path', function() {
});
it('finds the correct prefix for a group of absolute paths', function() {
var cwd = process.cwd().split(path.sep);
var paths = [
cwd.concat('foo', 'bar', 'baz', 'qux.js').join(path.sep),
cwd.concat('foo', 'bar', 'baz', 'quux.js').join(path.sep),
@ -64,7 +70,6 @@ describe('jsdoc/path', function() {
it('finds the correct prefix for a group of absolute paths and dotted relative paths',
function() {
var cwd = process.cwd().split(path.sep);
var paths = [
path.join('..', 'jsdoc', 'foo', 'bar', 'baz', 'qux', 'quux', 'test.js'),
cwd.concat('foo', 'bar', 'bazzy.js').join(path.sep),