Skip to content

Commit a30f678

Browse files
committed
Merge pull request mrjoelkemp#9 from nicksay/locate-jsdoc-command
Locate the jsdoc executable command in any node_modules directory
2 parents 5e938c9 + d621aec commit a30f678

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

Diff for: index.js

+43-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,55 @@
11
var execFile = require('child_process').execFile,
2-
path = require('path'),
3-
win32 = require('os').platform() === 'win32';
2+
fs = require('fs'),
3+
os = require('os'),
4+
path = require('path');
5+
6+
/**
7+
* Locates the JSDoc executable command. Since a module is not provided,
8+
* `require.resolve('jsdoc')` does not work and must be done manually.
9+
*
10+
* @param {String} dir The starting directory to search.
11+
* @return {String} The executable path, or null if not found.
12+
*/
13+
function locateJSDocCommand(dir) {
14+
var executable = os.platform() === 'win32' ? 'jsdoc.cmd' : 'jsdoc',
15+
cmd;
16+
dir = path.resolve(dir);
17+
while (dir) {
18+
try {
19+
cmd = path.join(dir, 'node_modules', '.bin', executable);
20+
// End the search if the command is found.
21+
// If not found, an exception is thrown.
22+
fs.statSync(cmd);
23+
break;
24+
} catch (ex) {
25+
cmd = null;
26+
// Otherwise, iterate to the parent directory, if possible.
27+
if (path.dirname(dir) == dir) {
28+
dir = null;
29+
} else {
30+
dir = path.resolve(path.dirname(dir));
31+
}
32+
}
33+
}
34+
return cmd;
35+
}
436

537
/**
638
* Parses the given file using JSDoc's parser.
7-
* Since JSDoc doesn't isn't require-able, we need to get the parse info from the command line.
39+
* Since JSDoc doesn't isn't require-able, we need to get the parse info from
40+
* the command line.
841
*
942
* @param {String} filename
1043
* @param {Function} cb ({Object}) -> null - Executed with the AST
1144
*/
1245
function jsdocParser(filename, cb) {
13-
// We can't require.resolve('jsdoc')
14-
// since jsdoc exposes jsdoc & jsdoc.cmd to .bin folder we can use them on any platform
15-
var cmd = path.join(__dirname, 'node_modules', '.bin', win32 ? 'jsdoc.cmd' : 'jsdoc');
16-
17-
execFile(cmd, ['-X', filename], { maxBuffer: 5120 * 1024 }, jsdocParser._onComplete.bind(null, cb));
46+
var cmd = locateJSDocCommand(__dirname);
47+
if (!cmd) {
48+
cb(new Error('Could not find jsdoc command.'), null);
49+
return;
50+
}
51+
execFile(cmd, ['-X', filename], {maxBuffer: 5120 * 1024},
52+
jsdocParser._onComplete.bind(null, cb));
1853
}
1954

2055
/**

0 commit comments

Comments
 (0)