Skip to content

Commit

Permalink
spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Nov 1, 2024
1 parent 2723e4d commit 943e8ff
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 39 deletions.
54 changes: 34 additions & 20 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var currentModulePaths = require('current-module-paths');
var toSpawnArgs = require('object-to-spawn-args');
var cp = require('child_process');
var util = require('node:util');
var streamReadAll = require('stream-read-all');

var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
class TempFile {
Expand Down Expand Up @@ -100,7 +101,7 @@ class JsdocCommand {
}
}

const exec = util.promisify(cp.exec);
util.promisify(cp.exec);

class Explain extends JsdocCommand {
async getOutput () {
Expand All @@ -120,25 +121,38 @@ class Explain extends JsdocCommand {
}

async _runJsdoc () {
const cmd = this.options.source.length
? `node "${this.jsdocPath}" ${toSpawnArgs(this.jsdocOptions).join(' ')} -X ${this.tempFileSet.files.join(' ')}`
: `node "${this.jsdocPath}" ${toSpawnArgs(this.jsdocOptions).join(' ')} -X ${this.inputFileSet.files.join(' ')}`;

let jsdocOutput = { stdout: '', stderr: '' };
try {
jsdocOutput = await exec(cmd, { maxBuffer: 1024 * 1024 * 100 }); /* 100MB */
const explainOutput = JSON.parse(jsdocOutput.stdout);
if (this.options.cache) {
await this.cache.write(this.cacheKey, explainOutput);
}
return explainOutput
} catch (err) {
const firstLineOfStdout = jsdocOutput.stdout.split(/\r?\n/)[0];
const jsdocErr = new Error(jsdocOutput.stderr.trim() || firstLineOfStdout || 'Jsdoc failed.');
jsdocErr.name = 'JSDOC_ERROR';
jsdocErr.cause = err;
throw jsdocErr
}
return new Promise((resolve, reject) => {
const jsdocArgs = [
this.jsdocPath,
...toSpawnArgs(this.jsdocOptions),
'-X',
...(this.options.source.length ? this.tempFileSet.files : this.inputFileSet.files)
];
let jsdocOutput = { stdout: '', stderr: '' };
const handle = cp.spawn('node', jsdocArgs);
streamReadAll.streamReadText(handle.stdout).then(stdout => jsdocOutput.stdout = stdout);
streamReadAll.streamReadText(handle.stderr).then(stderr => jsdocOutput.stderr = stderr);
handle.on('close', (code) => {
try {
if (code > 0) {
throw new Error('jsdoc exited with non-zero code: ' + code)
} else {
const explainOutput = JSON.parse(jsdocOutput.stdout);
if (this.options.cache) {
this.cache.write(this.cacheKey, explainOutput).then(() => resolve(explainOutput));
} else {
resolve(explainOutput);
}
}
} catch (err) {
const firstLineOfStdout = jsdocOutput.stdout.split(/\r?\n/)[0];
const jsdocErr = new Error(jsdocOutput.stderr.trim() || firstLineOfStdout || 'Jsdoc failed.');
jsdocErr.name = 'JSDOC_ERROR';
jsdocErr.cause = err;
reject(jsdocErr);
}
});
})
}

async readCache () {
Expand Down
52 changes: 33 additions & 19 deletions lib/explain.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import toSpawnArgs from 'object-to-spawn-args'
import cp from 'child_process'
import util from 'node:util'
import { promises as fs } from 'node:fs'
import { streamReadText } from 'stream-read-all'
const exec = util.promisify(cp.exec)

class Explain extends JsdocCommand {
Expand All @@ -23,25 +24,38 @@ class Explain extends JsdocCommand {
}

async _runJsdoc () {
const cmd = this.options.source.length
? `node "${this.jsdocPath}" ${toSpawnArgs(this.jsdocOptions).join(' ')} -X ${this.tempFileSet.files.join(' ')}`
: `node "${this.jsdocPath}" ${toSpawnArgs(this.jsdocOptions).join(' ')} -X ${this.inputFileSet.files.join(' ')}`

let jsdocOutput = { stdout: '', stderr: '' }
try {
jsdocOutput = await exec(cmd, { maxBuffer: 1024 * 1024 * 100 }) /* 100MB */
const explainOutput = JSON.parse(jsdocOutput.stdout)
if (this.options.cache) {
await this.cache.write(this.cacheKey, explainOutput)
}
return explainOutput
} catch (err) {
const firstLineOfStdout = jsdocOutput.stdout.split(/\r?\n/)[0]
const jsdocErr = new Error(jsdocOutput.stderr.trim() || firstLineOfStdout || 'Jsdoc failed.')
jsdocErr.name = 'JSDOC_ERROR'
jsdocErr.cause = err
throw jsdocErr
}
return new Promise((resolve, reject) => {
const jsdocArgs = [
this.jsdocPath,
...toSpawnArgs(this.jsdocOptions),
'-X',
...(this.options.source.length ? this.tempFileSet.files : this.inputFileSet.files)
]
let jsdocOutput = { stdout: '', stderr: '' }
const handle = cp.spawn('node', jsdocArgs)
streamReadText(handle.stdout).then(stdout => jsdocOutput.stdout = stdout)
streamReadText(handle.stderr).then(stderr => jsdocOutput.stderr = stderr)
handle.on('close', (code) => {
try {
if (code > 0) {
throw new Error('jsdoc exited with non-zero code: ' + code)
} else {
const explainOutput = JSON.parse(jsdocOutput.stdout)
if (this.options.cache) {
this.cache.write(this.cacheKey, explainOutput).then(() => resolve(explainOutput))
} else {
resolve(explainOutput)
}
}
} catch (err) {
const firstLineOfStdout = jsdocOutput.stdout.split(/\r?\n/)[0]
const jsdocErr = new Error(jsdocOutput.stderr.trim() || firstLineOfStdout || 'Jsdoc failed.')
jsdocErr.name = 'JSDOC_ERROR'
jsdocErr.cause = err
reject(jsdocErr)
}
})
})
}

async readCache () {
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"file-set": "^5.2.2",
"jsdoc": "^4.0.4",
"object-to-spawn-args": "^2.0.1",
"stream-read-all": "^5.0.2",
"walk-back": "^5.1.1"
},
"peerDependencies": {
Expand Down

0 comments on commit 943e8ff

Please sign in to comment.