From 48148e120d20b63c250ddbe8a048b267c2339d0c Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Sat, 9 Dec 2023 10:18:17 -0800 Subject: [PATCH] refactor: convert `env` to a class Because we now share `env` by passing it around, not by relying on the module cache. --- packages/jsdoc-core/index.js | 7 +- packages/jsdoc-core/lib/env.js | 104 +++++++++++----------- packages/jsdoc-core/test/specs/index.js | 7 +- packages/jsdoc-core/test/specs/lib/env.js | 59 ++++++------ packages/jsdoc/jsdoc.js | 4 +- 5 files changed, 98 insertions(+), 83 deletions(-) diff --git a/packages/jsdoc-core/index.js b/packages/jsdoc-core/index.js index faa15641..f0fda4cb 100644 --- a/packages/jsdoc-core/index.js +++ b/packages/jsdoc-core/index.js @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ + /** * Core functionality for JSDoc. * @@ -20,9 +21,9 @@ */ import * as config from './lib/config.js'; import Dependencies from './lib/dependencies.js'; -import env from './lib/env.js'; +import Env from './lib/env.js'; import * as name from './lib/name.js'; import * as plugins from './lib/plugins.js'; -export { config, Dependencies, env, name, plugins }; -export default { config, Dependencies, env, name, plugins }; +export { config, Dependencies, Env, name, plugins }; +export default { config, Dependencies, Env, name, plugins }; diff --git a/packages/jsdoc-core/lib/env.js b/packages/jsdoc-core/lib/env.js index ac9d223f..892aa83d 100644 --- a/packages/jsdoc-core/lib/env.js +++ b/packages/jsdoc-core/lib/env.js @@ -13,64 +13,66 @@ See the License for the specific language governing permissions and limitations under the License. */ + /** * Data about the environment in which JSDoc is running, including the configuration settings that * were used to run JSDoc. * - * @alias @jsdoc/core.env + * @alias @jsdoc/core.Env */ -export default { - /** - * The times at which JSDoc started and finished. - * - * @type {Object} - * @property {Date} start - The time at which JSDoc started running. - * @property {Date} finish - The time at which JSDoc finished running. - */ - run: { - start: new Date(), - finish: null, - }, +export default class Env { + constructor() { + /** + * The times at which JSDoc started and finished. + * + * @type {Object} + * @property {Date} start - The time at which JSDoc started running. + * @property {Date} finish - The time at which JSDoc finished running. + */ + this.run = { + start: new Date(), + finish: null, + }; - /** - * The command-line arguments passed to JSDoc. - * - * @type {Array<*>} - */ - args: [], + /** + * The command-line arguments passed to JSDoc. + * + * @type {Array<*>} + */ + this.args = []; - /** - * The data parsed from JSDoc's configuration file. - * - * @type Object - */ - conf: {}, + /** + * The data parsed from JSDoc's configuration file. + * + * @type Object + */ + this.conf = {}; - /** - * The command-line arguments, parsed into a key/value hash. - * - * @type {Object} - * @example if (global.env.opts.help) { console.log('Helpful message.'); } - */ - opts: {}, + /** + * The command-line arguments, parsed into a key/value hash. + * + * @type {Object} + * @example if (global.env.opts.help) { console.log('Helpful message.'); } + */ + this.opts = {}; - /** - * The source files that JSDoc will parse. - * - * @type {Array} - * @memberof env - */ - sourceFiles: [], + /** + * The source files that JSDoc will parse. + * + * @type {Array} + */ + this.sourceFiles = []; - /** - * The JSDoc version number and revision date. - * - * @type {Object} - * @property {string} number - The JSDoc version number. - * @property {string} revision - The JSDoc revision number, expressed as a UTC date string. - */ - version: { - number: null, - revision: null, - }, -}; + /** + * The JSDoc version number and revision date. + * + * @type {Object} + * @property {string} number - The JSDoc version number. + * @property {string} revision - The JSDoc revision number, expressed as a UTC date string. + */ + this.version = { + number: null, + revision: null, + }; + } +} diff --git a/packages/jsdoc-core/test/specs/index.js b/packages/jsdoc-core/test/specs/index.js index aed0cdf4..34569650 100644 --- a/packages/jsdoc-core/test/specs/index.js +++ b/packages/jsdoc-core/test/specs/index.js @@ -13,10 +13,11 @@ See the License for the specific language governing permissions and limitations under the License. */ + import core from '../../index.js'; import * as config from '../../lib/config.js'; import Dependencies from '../../lib/dependencies.js'; -import env from '../../lib/env.js'; +import Env from '../../lib/env.js'; import * as name from '../../lib/name.js'; import * as plugins from '../../lib/plugins.js'; @@ -33,9 +34,9 @@ describe('@jsdoc/core', () => { }); }); - describe('env', () => { + describe('Env', () => { it('is lib/env', () => { - expect(core.env).toEqual(env); + expect(core.Env).toEqual(Env); }); }); diff --git a/packages/jsdoc-core/test/specs/lib/env.js b/packages/jsdoc-core/test/specs/lib/env.js index b08f8d71..76044b99 100644 --- a/packages/jsdoc-core/test/specs/lib/env.js +++ b/packages/jsdoc-core/test/specs/lib/env.js @@ -13,38 +13,47 @@ See the License for the specific language governing permissions and limitations under the License. */ -import env from '../../../lib/env.js'; -describe('@jsdoc/core.env', () => { - it('exists', () => { - expect(env).toBeObject(); +import Env from '../../../lib/env.js'; + +describe('@jsdoc/core.Env', () => { + it('is a constructor', () => { + function factory() { + return new Env(); + } + + expect(factory).not.toThrow(); }); - it('has an `args` property', () => { - expect(env.args).toBeArray(); - }); + describe('properties', () => { + const env = new Env(); - it('has a `conf` property', () => { - expect(env.conf).toBeObject(); - }); + it('has an `args` property', () => { + expect(env.args).toBeArray(); + }); - it('has an `opts` property', () => { - expect(env.opts).toBeObject(); - }); + it('has a `conf` property', () => { + expect(env.conf).toBeObject(); + }); - it('has a `run` object with `start` and `finish` properties', () => { - expect(env.run).toBeObject(); - expect(env.run.finish).toBeNull(); - expect(env.run.start).toBeInstanceOf(Date); - }); + it('has an `opts` property', () => { + expect(env.opts).toBeObject(); + }); - it('has a `sourceFiles` property', () => { - expect(env.sourceFiles).toBeArray(); - }); + it('has a `run` object with `start` and `finish` properties', () => { + expect(env.run).toBeObject(); + expect(env.run.finish).toBeNull(); + expect(env.run.start).toBeInstanceOf(Date); + }); - it('has a `version` object with `number` and `revision` properties', () => { - expect(env.version).toBeObject(); - expect(Object.hasOwn(env.version, 'number')).toBeTrue(); - expect(Object.hasOwn(env.version, 'revision')).toBeTrue(); + it('has a `sourceFiles` property', () => { + expect(env.sourceFiles).toBeArray(); + }); + + it('has a `version` object with `number` and `revision` properties', () => { + expect(env.version).toBeObject(); + expect(Object.hasOwn(env.version, 'number')).toBeTrue(); + expect(Object.hasOwn(env.version, 'revision')).toBeTrue(); + }); }); }); diff --git a/packages/jsdoc/jsdoc.js b/packages/jsdoc/jsdoc.js index 3d6837b1..638f27e3 100755 --- a/packages/jsdoc/jsdoc.js +++ b/packages/jsdoc/jsdoc.js @@ -14,11 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { env } from '@jsdoc/core'; +import { Env } from '@jsdoc/core'; import cli from './cli.js'; (async () => { + const env = new Env(); + env.args = process.argv.slice(2); cli.setEnv(env).setVersionInfo(); await cli.loadConfig();