From e77ef10057303ff475b698291c56f4154b954208 Mon Sep 17 00:00:00 2001 From: mbehzad Date: Fri, 10 Nov 2017 13:11:07 +0100 Subject: [PATCH 1/3] allows `options.source` also to be an array of strings generates a temp source file for each code string in the options.source, adds unit test, updates / generates readme, minor fix in the documentation for `options.source` regarding using either `source` or `files`. --- README.md | 8 ++++---- lib/explain-sync.js | 3 ++- lib/explain.js | 3 ++- lib/jsdoc-api.js | 4 ++-- lib/jsdoc-command.js | 13 +++++++++---- lib/render-sync.js | 2 +- test/render-sync.js | 30 ++++++++++++++++++++++++++++++ 7 files changed, 50 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 634ae1c..a09e4d8 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ A programmatic interface for [jsdoc3](https://github.com/jsdoc3/jsdoc) with a fe * _inner_ * [~JsdocOptions](#module_jsdoc-api..JsdocOptions) * [.files](#module_jsdoc-api..JsdocOptions.JsdocOptions+files) : string \| Array.<string> - * [.source](#module_jsdoc-api..JsdocOptions.JsdocOptions+source) : string + * [.source](#module_jsdoc-api..JsdocOptions.JsdocOptions+source) : string \| Array.<string> * [.cache](#module_jsdoc-api..JsdocOptions.JsdocOptions+cache) : boolean * [.access](#module_jsdoc-api..JsdocOptions.JsdocOptions+access) : string * [.configure](#module_jsdoc-api..JsdocOptions.JsdocOptions+configure) : string @@ -123,7 +123,7 @@ The jsdoc options, common for all operations. * [~JsdocOptions](#module_jsdoc-api..JsdocOptions) * [.files](#module_jsdoc-api..JsdocOptions.JsdocOptions+files) : string \| Array.<string> - * [.source](#module_jsdoc-api..JsdocOptions.JsdocOptions+source) : string + * [.source](#module_jsdoc-api..JsdocOptions.JsdocOptions+source) : string \| Array.<string> * [.cache](#module_jsdoc-api..JsdocOptions.JsdocOptions+cache) : boolean * [.access](#module_jsdoc-api..JsdocOptions.JsdocOptions+access) : string * [.configure](#module_jsdoc-api..JsdocOptions.JsdocOptions+configure) : string @@ -146,8 +146,8 @@ One or more filenames to process. Either this or `source` must be supplied. **Kind**: instance property of [JsdocOptions](#module_jsdoc-api..JsdocOptions) -#### options.source : string -A string containing source code to process. Either this or `source` must be supplied. +#### options.source : string \| Array.<string> +A string or array of strings containing source code to process. Either this or `files` must be supplied. **Kind**: instance property of [JsdocOptions](#module_jsdoc-api..JsdocOptions) diff --git a/lib/explain-sync.js b/lib/explain-sync.js index 8e0077e..e14d1e7 100644 --- a/lib/explain-sync.js +++ b/lib/explain-sync.js @@ -1,5 +1,6 @@ 'use strict' const JsdocCommand = require('./jsdoc-command') +const FileSet = require('file-set') /** * @static @@ -23,7 +24,7 @@ class ExplainSync extends JsdocCommand { const toSpawnArgs = require('object-to-spawn-args') const jsdocArgs = toSpawnArgs(this.jsdocOptions) .concat([ '-X' ]) - .concat(this.options.source ? this.tempFile.path : this.inputFileSet.files) + .concat(this.options.source ? new FileSet(this.tempFiles.map(tempFile => tempFile.path)).files : this.inputFileSet.files) jsdocArgs.unshift(this.jsdocPath) diff --git a/lib/explain.js b/lib/explain.js index 316ae54..da24cce 100644 --- a/lib/explain.js +++ b/lib/explain.js @@ -1,5 +1,6 @@ 'use strict' const JsdocCommand = require('./jsdoc-command') +const FileSet = require('file-set') /** * @module explain @@ -35,7 +36,7 @@ class Explain extends JsdocCommand { const toSpawnArgs = require('object-to-spawn-args') const jsdocArgs = toSpawnArgs(this.jsdocOptions) .concat([ '-X' ]) - .concat(this.options.source ? this.tempFile.path : this.inputFileSet.files) + .concat(this.options.source ? new FileSet(this.tempFiles.map(tempFile => tempFile.path)).files : this.inputFileSet.files) jsdocArgs.unshift(this.jsdocPath) const spawn = require('child_process').spawn diff --git a/lib/jsdoc-api.js b/lib/jsdoc-api.js index 60a2094..4be851f 100644 --- a/lib/jsdoc-api.js +++ b/lib/jsdoc-api.js @@ -78,8 +78,8 @@ class JsdocOptions { this.files = arrayify(options.files) /** - * A string containing source code to process. Either this or `source` must be supplied. - * @type {string} + * A string or array of strings containing source code to process. Either this or `files` must be supplied. + * @type {string|string[]} */ this.source = options.source diff --git a/lib/jsdoc-command.js b/lib/jsdoc-command.js index 3db6b53..f6f8fcc 100644 --- a/lib/jsdoc-command.js +++ b/lib/jsdoc-command.js @@ -16,9 +16,14 @@ class JsdocCommand { options.files = arrayify(options.files) this.cache = cache - this.tempFile = null + this.tempFiles = null const TempFile = require('./temp-file') - if (options.source) this.tempFile = new TempFile(options.source) + if (options.source) { + // make sure `options.source` is an array + if (!Array.isArray(options.source)) options.source = [options.source] + + this.tempFiles = options.source.map(source => new TempFile(source)) + } const jsdocOptions = Object.assign({}, options) delete jsdocOptions.files @@ -94,8 +99,8 @@ class JsdocCommand { * perform post-execution cleanup */ postExecute () { - if (this.tempFile) { - this.tempFile.delete() + if (this.tempFiles) { + this.tempFiles.forEach(tempFile => tempFile.delete()) } } diff --git a/lib/render-sync.js b/lib/render-sync.js index d179919..5a62c83 100644 --- a/lib/render-sync.js +++ b/lib/render-sync.js @@ -9,7 +9,7 @@ class RenderSync extends JsdocCommand { if (err) throw err const toSpawnArgs = require('object-to-spawn-args') const jsdocArgs = toSpawnArgs(this.jsdocOptions) - .concat(this.options.source ? this.tempFile.path : this.options.files) + .concat(this.options.source ? this.tempFiles.map(tempFile => tempFile.path) : this.options.files) jsdocArgs.unshift(this.jsdocPath) diff --git a/test/render-sync.js b/test/render-sync.js index b932bc5..aa338b7 100644 --- a/test/render-sync.js +++ b/test/render-sync.js @@ -25,3 +25,33 @@ runner.test('.renderSync({ source, destination })', function () { fs.statSync('./tmp/out/index.html') }) }) + +runner.test('.renderSync({ source[], destination })', function () { + Fixture.createTmpFolder('tmp') + const sources = [ + `import Foo from "foo" + /** + * FooPrime is some child class + * @class + * @param {Object} - an input + * @extends Foo + */ + function FooPrime() {} + export default FooPrime + `, + `import Foo from "foo" + /** + * FooSecond is some other child class + * @class + * @param {Object} - an input + * @extends Foo + */ + function FooSecond() {} + export default FooSecond + `] + jsdoc.renderSync({ source: sources, destination: 'tmp/out' }) + a.doesNotThrow(function () { + fs.statSync('./tmp/out/FooPrime.html') + fs.statSync('./tmp/out/FooSecond.html') + }) +}) From 024672dc99e9b8750ef89cbc76e1ab7dac7dd8dd Mon Sep 17 00:00:00 2001 From: mbehzad Date: Tue, 13 Feb 2018 15:27:53 +0100 Subject: [PATCH 2/3] updates new unit tests according to the changes from "fix test race issues [fd1d7be]" --- test/render-sync.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/render-sync.js b/test/render-sync.js index 80b28b8..82014a6 100644 --- a/test/render-sync.js +++ b/test/render-sync.js @@ -49,9 +49,9 @@ runner.test('.renderSync({ source[], destination })', function () { function FooSecond() {} export default FooSecond `] - jsdoc.renderSync({ source: sources, destination: 'tmp/out' }) + jsdoc.renderSync({ source: sources, destination: 'tmp/renderSync/out' }) a.doesNotThrow(function () { - fs.statSync('./tmp/out/FooPrime.html') - fs.statSync('./tmp/out/FooSecond.html') + fs.statSync('./tmp/renderSync/out/FooPrime.html') + fs.statSync('./tmp/renderSync/out/FooSecond.html') }) }) From e975bb5a7e1b9c462d6fa2600014d6d6a0e88db0 Mon Sep 17 00:00:00 2001 From: mbehzad Date: Sun, 24 Jun 2018 13:38:07 +0200 Subject: [PATCH 3/3] updates new unit tests according to the changes from "fix test race issues [fd1d7be]" --- test/render-sync.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/render-sync.js b/test/render-sync.js index 82014a6..7524b34 100644 --- a/test/render-sync.js +++ b/test/render-sync.js @@ -27,7 +27,7 @@ runner.test('.renderSync({ source, destination })', function () { }) runner.test('.renderSync({ source[], destination })', function () { - Fixture.createTmpFolder('tmp') + Fixture.createTmpFolder('tmp/renderSync') const sources = [ `import Foo from "foo" /**