From 9a7b9efe142aede9c128b8a3d026a902acf4ced9 Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Sun, 1 Dec 2013 09:38:49 -0800 Subject: [PATCH] search config file path for resources; use the working directory to find the common path prefix --- lib/jsdoc/path.js | 27 +++++++++++++-------------- test/specs/jsdoc/path.js | 19 ++++++++++++------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/jsdoc/path.js b/lib/jsdoc/path.js index 16681126..56613a4a 100644 --- a/lib/jsdoc/path.js +++ b/lib/jsdoc/path.js @@ -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; diff --git a/test/specs/jsdoc/path.js b/test/specs/jsdoc/path.js index 90952b3b..3da7b11b 100644 --- a/test/specs/jsdoc/path.js +++ b/test/specs/jsdoc/path.js @@ -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),