Skip to content

Commit c8d88b1

Browse files
committed
Remove .explainSync().. Rewrite project as an ES Module
Replace collect-all with stream-read-all
1 parent d1311fc commit c8d88b1

16 files changed

+151
-1480
lines changed

.github/workflows/node.js.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ jobs:
2626
node-version: ${{ matrix.node-version }}
2727
cache: 'npm'
2828
- run: npm install
29+
- run: npm i -g @75lb/nature@^0.1.1
2930
- run: npm test

.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

README.md

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ A programmatic interface for [jsdoc3](https://github.com/jsdoc3/jsdoc) with a fe
4545

4646
* [jsdoc-api](#module_jsdoc-api)
4747
* _static_
48-
* [.cache](#module_jsdoc-api.cache) : [<code>cache-point</code>](https://github.com/75lb/cache-point)
49-
* [.explainSync([options])](#module_jsdoc-api.explainSync) ⇒ <code>Array.&lt;object&gt;</code>
5048
* [.explain([options])](#module_jsdoc-api.explain) ⇒ <code>Promise</code>
5149
* [.renderSync([options])](#module_jsdoc-api.renderSync)
5250
* _inner_
@@ -66,24 +64,7 @@ A programmatic interface for [jsdoc3](https://github.com/jsdoc3/jsdoc) with a fe
6664
* [.readme](#module_jsdoc-api..JsdocOptions+readme) : <code>string</code>
6765
* [.template](#module_jsdoc-api..JsdocOptions+template) : <code>string</code>
6866
* [.tutorials](#module_jsdoc-api..JsdocOptions+tutorials) : <code>string</code>
69-
70-
<a name="module_jsdoc-api.cache"></a>
71-
72-
### jsdoc.cache : [<code>cache-point</code>](https://github.com/75lb/cache-point)
73-
The [cache-point](https://github.com/75lb/cache-point) instance used when `cache: true` is specified on `.explain()` or `.explainSync()`.
74-
75-
**Kind**: static property of [<code>jsdoc-api</code>](#module_jsdoc-api)
76-
<a name="module_jsdoc-api.explainSync"></a>
77-
78-
### jsdoc.explainSync([options]) ⇒ <code>Array.&lt;object&gt;</code>
79-
Returns jsdoc explain output.
80-
81-
**Kind**: static method of [<code>jsdoc-api</code>](#module_jsdoc-api)
82-
**Prerequisite**: Requires node v0.12 or above
83-
84-
| Param | Type |
85-
| --- | --- |
86-
| [options] | [<code>JsdocOptions</code>](#module_jsdoc-api..JsdocOptions) |
67+
* [~cache](#module_jsdoc-api..cache) : [<code>cache-point</code>](https://github.com/75lb/cache-point)
8768

8869
<a name="module_jsdoc-api.explain"></a>
8970

@@ -227,6 +208,12 @@ The path to the template to use. Default: path/to/jsdoc/templates/default.
227208
Directory in which JSDoc should search for tutorials.
228209

229210
**Kind**: instance property of [<code>JsdocOptions</code>](#module_jsdoc-api..JsdocOptions)
211+
<a name="module_jsdoc-api..cache"></a>
212+
213+
### jsdoc-api~cache : [<code>cache-point</code>](https://github.com/75lb/cache-point)
214+
The [cache-point](https://github.com/75lb/cache-point) instance used when `cache: true` is specified on `.explain()` or `.explainSync()`.
215+
216+
**Kind**: inner constant of [<code>jsdoc-api</code>](#module_jsdoc-api)
230217

231218
* * *
232219

index.js

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
* @module jsdoc-api
33
* @typicalname jsdoc
44
*/
5-
const path = require('path')
6-
const Cache = require('cache-point')
5+
import path from 'path'
6+
import Cache from 'cache-point'
7+
import Explain from './lib/explain.js'
8+
import RenderSync from './lib/render-sync.js'
9+
import arrayify from 'array-back'
10+
import os from 'node:os'
711

812
/**
9-
* Returns jsdoc explain output.
10-
*
11-
* @param [options] {module:jsdoc-api~JsdocOptions}
12-
* @returns {object[]}
13-
* @static
14-
* @prerequisite Requires node v0.12 or above
13+
* @external cache-point
14+
* @see https://github.com/75lb/cache-point
1515
*/
16-
function explainSync (options) {
17-
options = new JsdocOptions(options)
18-
const ExplainSync = require('./lib/explain-sync')
19-
const command = new ExplainSync(options, exports.cache)
20-
return command.execute()
21-
}
16+
17+
/**
18+
* The [cache-point](https://github.com/75lb/cache-point) instance used when `cache: true` is specified on `.explain()` or `.explainSync()`.
19+
* @type {external:cache-point}
20+
*/
21+
const cache = new Cache({ dir: path.join(os.tmpdir(), 'jsdoc-api') })
2222

2323
/**
2424
* Returns a promise for the jsdoc explain output.
@@ -28,10 +28,9 @@ function explainSync (options) {
2828
* @returns {Promise}
2929
* @static
3030
*/
31-
function explain (options) {
31+
async function explain (options) {
3232
options = new JsdocOptions(options)
33-
const Explain = require('./lib/explain')
34-
const command = new Explain(options, exports.cache)
33+
const command = new Explain(options, cache)
3534
return command.execute()
3635
}
3736

@@ -46,7 +45,6 @@ function explain (options) {
4645
*/
4746
function renderSync (options) {
4847
options = new JsdocOptions(options)
49-
const RenderSync = require('./lib/render-sync')
5048
const command = new RenderSync(options)
5149
return command.execute()
5250
}
@@ -58,7 +56,6 @@ function renderSync (options) {
5856
class JsdocOptions {
5957
constructor (options) {
6058
options = options || {}
61-
const arrayify = require('array-back')
6259

6360
/**
6461
* One or more filenames to process. Either this or `source` must be supplied.
@@ -153,16 +150,4 @@ class JsdocOptions {
153150
}
154151
}
155152

156-
/**
157-
* @external cache-point
158-
* @see https://github.com/75lb/cache-point
159-
*/
160-
161-
exports.explainSync = explainSync
162-
exports.explain = explain
163-
exports.renderSync = renderSync
164-
/**
165-
* The [cache-point](https://github.com/75lb/cache-point) instance used when `cache: true` is specified on `.explain()` or `.explainSync()`.
166-
* @type {external:cache-point}
167-
*/
168-
exports.cache = new Cache({ dir: path.join(require('os').tmpdir(), 'jsdoc-api') })
153+
export { explain, renderSync, cache }

lib/explain-sync.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

lib/explain.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
const JsdocCommand = require('./jsdoc-command')
1+
import JsdocCommand from './jsdoc-command.js'
2+
import streamReadAll from 'stream-read-all'
3+
import toSpawnArgs from 'object-to-spawn-args'
4+
import { spawn } from 'child_process'
25

36
/**
47
* @module explain
@@ -18,27 +21,21 @@ class Explain extends JsdocCommand {
1821
}
1922
}
2023

21-
_runJsdoc () {
22-
return new Promise((resolve, reject) => {
23-
const collectAll = require('collect-all')
24+
async _runJsdoc () {
25+
return new Promise(async (resolve, reject) => {
2426
const jsdocOutput = {
2527
stdout: '',
26-
stderr: '',
27-
collectInto (dest) {
28-
return collectAll(data => { this[dest] = data.toString() })
29-
}
28+
stderr: ''
3029
}
3130

32-
const toSpawnArgs = require('object-to-spawn-args')
3331
const jsdocArgs = toSpawnArgs(this.jsdocOptions)
34-
.concat([ '-X' ])
32+
.concat(['-X'])
3533
.concat(this.options.source ? this.tempFile.path : this.inputFileSet.files)
3634
jsdocArgs.unshift(this.jsdocPath)
3735

38-
const spawn = require('child_process').spawn
3936
const handle = spawn('node', jsdocArgs)
40-
handle.stderr.pipe(jsdocOutput.collectInto('stderr'))
41-
handle.stdout.pipe(jsdocOutput.collectInto('stdout'))
37+
jsdocOutput.stdout = (await streamReadAll(handle.stdout)).toString()
38+
jsdocOutput.stderr = (await streamReadAll(handle.stderr)).toString()
4239

4340
handle.on('close', code => {
4441
try {
@@ -56,4 +53,4 @@ class Explain extends JsdocCommand {
5653
}
5754
}
5855

59-
module.exports = Explain
56+
export default Explain

lib/jsdoc-command.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
const arrayify = require('array-back')
2-
const path = require('path')
1+
import arrayify from 'array-back'
2+
import path from 'path'
3+
import TempFile from './temp-file.js'
4+
import FileSet from 'file-set'
5+
import assert from 'assert'
6+
import walkBack from 'walk-back'
7+
import { promises as fs } from 'node:fs'
8+
import currentModulePaths from 'current-module-paths'
9+
10+
const { __dirname } = currentModulePaths(import.meta.url)
311

412
/**
513
* @module jsdoc-command
@@ -16,7 +24,6 @@ class JsdocCommand {
1624

1725
this.cache = cache
1826
this.tempFile = null
19-
const TempFile = require('./temp-file')
2027
if (options.source) this.tempFile = new TempFile(options.source)
2128

2229
const jsdocOptions = Object.assign({}, options)
@@ -27,7 +34,6 @@ class JsdocCommand {
2734
this.options = options
2835
this.jsdocOptions = jsdocOptions
2936

30-
const walkBack = require('walk-back')
3137
this.jsdocPath = walkBack(
3238
path.join(__dirname, '..'),
3339
path.join('node_modules', 'jsdoc', 'jsdoc.js')
@@ -67,7 +73,6 @@ class JsdocCommand {
6773
* Perform pre-execution processing here, e.g. expand input glob patterns.
6874
*/
6975
preExecute () {
70-
const FileSet = require('file-set')
7176
this.inputFileSet = new FileSet(this.options.files)
7277
}
7378

@@ -76,7 +81,6 @@ class JsdocCommand {
7681
* @returns {null|Error}
7782
*/
7883
validate () {
79-
const assert = require('assert')
8084
assert.ok(
8185
this.options.files.length || this.options.source,
8286
'Must set either .files or .source'
@@ -118,7 +122,6 @@ class JsdocCommand {
118122
*/
119123
async readCache () {
120124
if (this.cache) {
121-
const fs = require('fs').promises
122125
const promises = this.inputFileSet.files.map(file => fs.readFile(file, 'utf8'))
123126
const contents = await Promise.all(promises)
124127
this.cacheKey = contents.concat(this.inputFileSet.files)
@@ -127,15 +130,6 @@ class JsdocCommand {
127130
return Promise.reject()
128131
}
129132
}
130-
131-
readCacheSync () {
132-
if (this.cache) {
133-
const fs = require('fs')
134-
const contents = this.inputFileSet.files.map(file => fs.readFileSync(file, 'utf8'))
135-
this.cacheKey = contents.concat(this.inputFileSet.files)
136-
return this.cache.readSync(this.cacheKey)
137-
}
138-
}
139133
}
140134

141-
module.exports = JsdocCommand
135+
export default JsdocCommand

lib/render-sync.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
const JsdocCommand = require('./jsdoc-command')
1+
import JsdocCommand from './jsdoc-command.js'
2+
import toSpawnArgs from 'object-to-spawn-args'
3+
import { spawnSync } from 'child_process'
24

35
/**
46
* @static
57
*/
68
class RenderSync extends JsdocCommand {
79
getOutput (err) {
810
if (err) throw err
9-
const toSpawnArgs = require('object-to-spawn-args')
1011
const jsdocArgs = toSpawnArgs(this.jsdocOptions)
1112
.concat(this.options.source ? this.tempFile.path : this.options.files)
1213

1314
jsdocArgs.unshift(this.jsdocPath)
14-
15-
const spawnSync = require('child_process').spawnSync
1615
spawnSync('node', jsdocArgs)
1716
}
1817
}
1918

20-
module.exports = RenderSync
19+
export default RenderSync

lib/temp-file.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
const fs = require('fs')
2-
const os = require('os')
3-
const crypto = require('crypto')
4-
const path = require('path')
1+
import fs from 'fs'
2+
import os from 'os'
3+
import crypto from 'crypto'
4+
import path from 'path'
55

66
class TempFile {
77
constructor (source) {
88
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'jsdoc-api-'))
99
this.path = path.join(tempDir, crypto.randomBytes(6).toString('hex') + '.js')
1010
fs.writeFileSync(this.path, source)
1111
}
12+
1213
delete () {
1314
try {
1415
fs.unlinkSync(this.path)
@@ -18,4 +19,4 @@ class TempFile {
1819
}
1920
}
2021

21-
module.exports = TempFile
22+
export default TempFile

0 commit comments

Comments
 (0)