Skip to content

Commit 943e8ff

Browse files
committed
spaces
1 parent 2723e4d commit 943e8ff

File tree

4 files changed

+78
-39
lines changed

4 files changed

+78
-39
lines changed

dist/index.cjs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var currentModulePaths = require('current-module-paths');
1313
var toSpawnArgs = require('object-to-spawn-args');
1414
var cp = require('child_process');
1515
var util = require('node:util');
16+
var streamReadAll = require('stream-read-all');
1617

1718
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
1819
class TempFile {
@@ -100,7 +101,7 @@ class JsdocCommand {
100101
}
101102
}
102103

103-
const exec = util.promisify(cp.exec);
104+
util.promisify(cp.exec);
104105

105106
class Explain extends JsdocCommand {
106107
async getOutput () {
@@ -120,25 +121,38 @@ class Explain extends JsdocCommand {
120121
}
121122

122123
async _runJsdoc () {
123-
const cmd = this.options.source.length
124-
? `node "${this.jsdocPath}" ${toSpawnArgs(this.jsdocOptions).join(' ')} -X ${this.tempFileSet.files.join(' ')}`
125-
: `node "${this.jsdocPath}" ${toSpawnArgs(this.jsdocOptions).join(' ')} -X ${this.inputFileSet.files.join(' ')}`;
126-
127-
let jsdocOutput = { stdout: '', stderr: '' };
128-
try {
129-
jsdocOutput = await exec(cmd, { maxBuffer: 1024 * 1024 * 100 }); /* 100MB */
130-
const explainOutput = JSON.parse(jsdocOutput.stdout);
131-
if (this.options.cache) {
132-
await this.cache.write(this.cacheKey, explainOutput);
133-
}
134-
return explainOutput
135-
} catch (err) {
136-
const firstLineOfStdout = jsdocOutput.stdout.split(/\r?\n/)[0];
137-
const jsdocErr = new Error(jsdocOutput.stderr.trim() || firstLineOfStdout || 'Jsdoc failed.');
138-
jsdocErr.name = 'JSDOC_ERROR';
139-
jsdocErr.cause = err;
140-
throw jsdocErr
141-
}
124+
return new Promise((resolve, reject) => {
125+
const jsdocArgs = [
126+
this.jsdocPath,
127+
...toSpawnArgs(this.jsdocOptions),
128+
'-X',
129+
...(this.options.source.length ? this.tempFileSet.files : this.inputFileSet.files)
130+
];
131+
let jsdocOutput = { stdout: '', stderr: '' };
132+
const handle = cp.spawn('node', jsdocArgs);
133+
streamReadAll.streamReadText(handle.stdout).then(stdout => jsdocOutput.stdout = stdout);
134+
streamReadAll.streamReadText(handle.stderr).then(stderr => jsdocOutput.stderr = stderr);
135+
handle.on('close', (code) => {
136+
try {
137+
if (code > 0) {
138+
throw new Error('jsdoc exited with non-zero code: ' + code)
139+
} else {
140+
const explainOutput = JSON.parse(jsdocOutput.stdout);
141+
if (this.options.cache) {
142+
this.cache.write(this.cacheKey, explainOutput).then(() => resolve(explainOutput));
143+
} else {
144+
resolve(explainOutput);
145+
}
146+
}
147+
} catch (err) {
148+
const firstLineOfStdout = jsdocOutput.stdout.split(/\r?\n/)[0];
149+
const jsdocErr = new Error(jsdocOutput.stderr.trim() || firstLineOfStdout || 'Jsdoc failed.');
150+
jsdocErr.name = 'JSDOC_ERROR';
151+
jsdocErr.cause = err;
152+
reject(jsdocErr);
153+
}
154+
});
155+
})
142156
}
143157

144158
async readCache () {

lib/explain.js

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import toSpawnArgs from 'object-to-spawn-args'
33
import cp from 'child_process'
44
import util from 'node:util'
55
import { promises as fs } from 'node:fs'
6+
import { streamReadText } from 'stream-read-all'
67
const exec = util.promisify(cp.exec)
78

89
class Explain extends JsdocCommand {
@@ -23,25 +24,38 @@ class Explain extends JsdocCommand {
2324
}
2425

2526
async _runJsdoc () {
26-
const cmd = this.options.source.length
27-
? `node "${this.jsdocPath}" ${toSpawnArgs(this.jsdocOptions).join(' ')} -X ${this.tempFileSet.files.join(' ')}`
28-
: `node "${this.jsdocPath}" ${toSpawnArgs(this.jsdocOptions).join(' ')} -X ${this.inputFileSet.files.join(' ')}`
29-
30-
let jsdocOutput = { stdout: '', stderr: '' }
31-
try {
32-
jsdocOutput = await exec(cmd, { maxBuffer: 1024 * 1024 * 100 }) /* 100MB */
33-
const explainOutput = JSON.parse(jsdocOutput.stdout)
34-
if (this.options.cache) {
35-
await this.cache.write(this.cacheKey, explainOutput)
36-
}
37-
return explainOutput
38-
} catch (err) {
39-
const firstLineOfStdout = jsdocOutput.stdout.split(/\r?\n/)[0]
40-
const jsdocErr = new Error(jsdocOutput.stderr.trim() || firstLineOfStdout || 'Jsdoc failed.')
41-
jsdocErr.name = 'JSDOC_ERROR'
42-
jsdocErr.cause = err
43-
throw jsdocErr
44-
}
27+
return new Promise((resolve, reject) => {
28+
const jsdocArgs = [
29+
this.jsdocPath,
30+
...toSpawnArgs(this.jsdocOptions),
31+
'-X',
32+
...(this.options.source.length ? this.tempFileSet.files : this.inputFileSet.files)
33+
]
34+
let jsdocOutput = { stdout: '', stderr: '' }
35+
const handle = cp.spawn('node', jsdocArgs)
36+
streamReadText(handle.stdout).then(stdout => jsdocOutput.stdout = stdout)
37+
streamReadText(handle.stderr).then(stderr => jsdocOutput.stderr = stderr)
38+
handle.on('close', (code) => {
39+
try {
40+
if (code > 0) {
41+
throw new Error('jsdoc exited with non-zero code: ' + code)
42+
} else {
43+
const explainOutput = JSON.parse(jsdocOutput.stdout)
44+
if (this.options.cache) {
45+
this.cache.write(this.cacheKey, explainOutput).then(() => resolve(explainOutput))
46+
} else {
47+
resolve(explainOutput)
48+
}
49+
}
50+
} catch (err) {
51+
const firstLineOfStdout = jsdocOutput.stdout.split(/\r?\n/)[0]
52+
const jsdocErr = new Error(jsdocOutput.stderr.trim() || firstLineOfStdout || 'Jsdoc failed.')
53+
jsdocErr.name = 'JSDOC_ERROR'
54+
jsdocErr.cause = err
55+
reject(jsdocErr)
56+
}
57+
})
58+
})
4559
}
4660

4761
async readCache () {

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"file-set": "^5.2.2",
3838
"jsdoc": "^4.0.4",
3939
"object-to-spawn-args": "^2.0.1",
40+
"stream-read-all": "^5.0.2",
4041
"walk-back": "^5.1.1"
4142
},
4243
"peerDependencies": {

0 commit comments

Comments
 (0)