refactor: convert env to a class

Because we now share `env` by passing it around, not by relying on the module cache.
This commit is contained in:
Jeff Williams 2023-12-09 10:18:17 -08:00
parent 577f22be4a
commit 48148e120d
No known key found for this signature in database
5 changed files with 98 additions and 83 deletions

View File

@ -13,6 +13,7 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
/** /**
* Core functionality for JSDoc. * Core functionality for JSDoc.
* *
@ -20,9 +21,9 @@
*/ */
import * as config from './lib/config.js'; import * as config from './lib/config.js';
import Dependencies from './lib/dependencies.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 name from './lib/name.js';
import * as plugins from './lib/plugins.js'; import * as plugins from './lib/plugins.js';
export { config, Dependencies, env, name, plugins }; export { config, Dependencies, Env, name, plugins };
export default { config, Dependencies, env, name, plugins }; export default { config, Dependencies, Env, name, plugins };

View File

@ -13,13 +13,15 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
/** /**
* Data about the environment in which JSDoc is running, including the configuration settings that * Data about the environment in which JSDoc is running, including the configuration settings that
* were used to run JSDoc. * were used to run JSDoc.
* *
* @alias @jsdoc/core.env * @alias @jsdoc/core.Env
*/ */
export default { export default class Env {
constructor() {
/** /**
* The times at which JSDoc started and finished. * The times at which JSDoc started and finished.
* *
@ -27,24 +29,24 @@ export default {
* @property {Date} start - The time at which JSDoc started running. * @property {Date} start - The time at which JSDoc started running.
* @property {Date} finish - The time at which JSDoc finished running. * @property {Date} finish - The time at which JSDoc finished running.
*/ */
run: { this.run = {
start: new Date(), start: new Date(),
finish: null, finish: null,
}, };
/** /**
* The command-line arguments passed to JSDoc. * The command-line arguments passed to JSDoc.
* *
* @type {Array<*>} * @type {Array<*>}
*/ */
args: [], this.args = [];
/** /**
* The data parsed from JSDoc's configuration file. * The data parsed from JSDoc's configuration file.
* *
* @type Object<string, *> * @type Object<string, *>
*/ */
conf: {}, this.conf = {};
/** /**
* The command-line arguments, parsed into a key/value hash. * The command-line arguments, parsed into a key/value hash.
@ -52,15 +54,14 @@ export default {
* @type {Object} * @type {Object}
* @example if (global.env.opts.help) { console.log('Helpful message.'); } * @example if (global.env.opts.help) { console.log('Helpful message.'); }
*/ */
opts: {}, this.opts = {};
/** /**
* The source files that JSDoc will parse. * The source files that JSDoc will parse.
* *
* @type {Array<string>} * @type {Array<string>}
* @memberof env
*/ */
sourceFiles: [], this.sourceFiles = [];
/** /**
* The JSDoc version number and revision date. * The JSDoc version number and revision date.
@ -69,8 +70,9 @@ export default {
* @property {string} number - The JSDoc version number. * @property {string} number - The JSDoc version number.
* @property {string} revision - The JSDoc revision number, expressed as a UTC date string. * @property {string} revision - The JSDoc revision number, expressed as a UTC date string.
*/ */
version: { this.version = {
number: null, number: null,
revision: null, revision: null,
},
}; };
}
}

View File

@ -13,10 +13,11 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import core from '../../index.js'; import core from '../../index.js';
import * as config from '../../lib/config.js'; import * as config from '../../lib/config.js';
import Dependencies from '../../lib/dependencies.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 name from '../../lib/name.js';
import * as plugins from '../../lib/plugins.js'; import * as plugins from '../../lib/plugins.js';
@ -33,9 +34,9 @@ describe('@jsdoc/core', () => {
}); });
}); });
describe('env', () => { describe('Env', () => {
it('is lib/env', () => { it('is lib/env', () => {
expect(core.env).toEqual(env); expect(core.Env).toEqual(Env);
}); });
}); });

View File

@ -13,13 +13,21 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import env from '../../../lib/env.js';
describe('@jsdoc/core.env', () => { import Env from '../../../lib/env.js';
it('exists', () => {
expect(env).toBeObject(); describe('@jsdoc/core.Env', () => {
it('is a constructor', () => {
function factory() {
return new Env();
}
expect(factory).not.toThrow();
}); });
describe('properties', () => {
const env = new Env();
it('has an `args` property', () => { it('has an `args` property', () => {
expect(env.args).toBeArray(); expect(env.args).toBeArray();
}); });
@ -48,3 +56,4 @@ describe('@jsdoc/core.env', () => {
expect(Object.hasOwn(env.version, 'revision')).toBeTrue(); expect(Object.hasOwn(env.version, 'revision')).toBeTrue();
}); });
}); });
});

View File

@ -14,11 +14,13 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import { env } from '@jsdoc/core'; import { Env } from '@jsdoc/core';
import cli from './cli.js'; import cli from './cli.js';
(async () => { (async () => {
const env = new Env();
env.args = process.argv.slice(2); env.args = process.argv.slice(2);
cli.setEnv(env).setVersionInfo(); cli.setEnv(env).setVersionInfo();
await cli.loadConfig(); await cli.loadConfig();