|
1 | 1 | 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 | +} |
4 | 36 |
|
5 | 37 | /**
|
6 | 38 | * 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. |
8 | 41 | *
|
9 | 42 | * @param {String} filename
|
10 | 43 | * @param {Function} cb ({Object}) -> null - Executed with the AST
|
11 | 44 | */
|
12 | 45 | 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)); |
18 | 53 | }
|
19 | 54 |
|
20 | 55 | /**
|
|
0 commit comments