jsdoc2md / jsdoc-to-markdown

Generate markdown documentation from jsdoc-annotated javascript
MIT License
1.69k stars 151 forks source link

[Bug] jsdoc2md bad call to jsdoc: Path-Problem #307

Open Bizarrus opened 2 hours ago

Bizarrus commented 2 hours ago

Executing jsdoc fails because jsdoc2md does not check or correct paths.

File Structure:

Main Path: E:/ServerManager - Master/
- Bin/Docs.js (this generator, see Sample)
- main.js (Main file)
- Docs/ (later the output)

Sample:

#!/usr/bin/node
import jsdoc2md from 'jsdoc-to-markdown'
import { promises as fs } from 'node:fs'
import path from 'path'

/* input and output paths */
const inputFile = './main.js'

/* get template data */
const templateData = await jsdoc2md.getTemplateData({ files: inputFile })

/* reduce templateData to an array of class names */
const classNames = templateData.filter(i => i.kind === 'class').map(i => i.name)

/* create a documentation file for each class */
for (const className of classNames) {
    const template = `{{#class name="${className}"}}{{>docs}}{{/class}}`
    console.log(`rendering ${className}, template: ${template}`)
    const output = await jsdoc2md.render({ data: templateData, template: template })
    await fs.writeFile(path.resolve(`${className}.md`), output)
}

Output:

PS E:\ServerManager - Master> npm run docs

> master@1.0.0 docs
> node ./Bin/Docs.js

file:///E:/ServerManager%20-%20Master/node_modules/jsdoc-api/lib/explain.js:40
      const jsdocErr = new Error(jsdocOutput.stderr.trim() || firstLineOfStdout || 'Jsdoc failed.')
                       ^

JSDOC_ERROR: Jsdoc failed.
    at Explain._runJsdoc (file:///E:/ServerManager%20-%20Master/node_modules/jsdoc-api/lib/explain.js:40:24)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Explain.execute (file:///E:/ServerManager%20-%20Master/node_modules/jsdoc-api/lib/jsdoc-command.js:57:16)
    at async JsdocToMarkdown.getTemplateData (file:///E:/ServerManager%20-%20Master/node_modules/jsdoc-to-markdown/index.js:68:23)
    at async file:///E:/ServerManager%20-%20Master/Bin/Docs.js:10:22 {
  cause: Error: Command failed: node E:\ServerManager - Master\node_modules\jsdoc\jsdoc.js  -X ./main.js
  node:internal/modules/cjs/loader:1222
    throw err;
    ^

  Error: Cannot find module 'E:\ServerManager'
      at Module._resolveFilename (node:internal/modules/cjs/loader:1219:15)
      at Module._load (node:internal/modules/cjs/loader:1045:27)
      at TracingChannel.traceSync (node:diagnostics_channel:315:14)
      at wrapModuleLoad (node:internal/modules/cjs/loader:215:24)
      at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:158:5)
      at node:internal/main/run_main_module:30:49 {
    code: 'MODULE_NOT_FOUND',
    requireStack: []
  }

  Node.js v22.5.1

      at genericNodeError (node:internal/errors:983:15)
      at wrappedFn (node:internal/errors:537:14)
      at ChildProcess.exithandler (node:child_process:421:12)
      at ChildProcess.emit (node:events:520:28)
      at maybeClose (node:internal/child_process:1105:16)
      at ChildProcess._handle.onexit (node:internal/child_process:305:5) {
    code: 1,
    killed: false,
    signal: null,
    cmd: 'node E:\\ServerManager - Master\\node_modules\\jsdoc\\jsdoc.js  -X ./main.js',
    stdout: '',
    stderr: 'node:internal/modules/cjs/loader:1222\r\n' +
      '  throw err;\r\n' +
      '  ^\r\n' +
      '\r\n' +
      "Error: Cannot find module 'E:\\ServerManager'\r\n" +
      '    at Module._resolveFilename (node:internal/modules/cjs/loader:1219:15)\r\n' +
      '    at Module._load (node:internal/modules/cjs/loader:1045:27)\r\n' +
      '    at TracingChannel.traceSync (node:diagnostics_channel:315:14)\r\n' +
      '    at wrapModuleLoad (node:internal/modules/cjs/loader:215:24)\r\n' +
      '    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:158:5)\r\n' +
      '    at node:internal/main/run_main_module:30:49 {\r\n' +
      "  code: 'MODULE_NOT_FOUND',\r\n" +
      '  requireStack: []\r\n' +
      '}\r\n' +
      '\r\n' +
      'Node.js v22.5.1\r\n'
  }
}

Node.js v22.5.1
75lb commented 2 hours ago

Hi, thanks for the report! Can you confirm which jsdoc2md version you're using? In the meantime, try overriding the JSDOC_PATH with a new path which has the spaces escaped. I can't remember the exact windows syntax, something like:

set JSDOC_PATH="E:\ServerManager%20-%20Master\node_modules\jsdoc\jsdoc.js" && node ./Bin/Docs.js
75lb commented 1 hour ago

Or you could set the env variable in ./Bin/Docs.js, add this line:

process.env.JSDOC_PATH = 'E:\ServerManager%20-%20Master\node_modules\jsdoc\jsdoc.js'

Let me know if that helps. Either way, I will look into reproducing and fixing this bug later today.