From 13fdc520ccf7e4c11d47d663dae1c32c4d210402 Mon Sep 17 00:00:00 2001 From: John Gee Date: Fri, 15 Sep 2023 19:25:07 +1200 Subject: [PATCH 1/2] Remove default export of global Command instance --- docs/deprecated.md | 1 + index.js | 10 ++--- tests/fixtures-extensions/pm.js | 2 +- tests/fixtures/inspect.js | 2 +- tests/fixtures/pm | 2 +- tests/fixtures/pm-cache.js | 2 +- tests/program.test.js | 71 +++++++++++++++++++++++++++------ 7 files changed, 68 insertions(+), 22 deletions(-) diff --git a/docs/deprecated.md b/docs/deprecated.md index 2ead1ab81..6bbd87a86 100644 --- a/docs/deprecated.md +++ b/docs/deprecated.md @@ -58,6 +58,7 @@ const program = new Command() - Removed from README in Commander v5. - Deprecated from Commander v7. - Removed from TypeScript declarations in Commander v8. +- Removed from CommonJS in Commander v12. Deprecated and gone! ## Callback to .help() and .outputHelp() diff --git a/index.js b/index.js index 7563b1bac..ab63c35d9 100644 --- a/index.js +++ b/index.js @@ -6,13 +6,11 @@ const { Option } = require('./lib/option.js'); // @ts-check -/** - * Expose the root command. - */ +exports.program = new Command(); -exports = module.exports = new Command(); -exports.program = exports; // More explicit access to global command. -// Implicit export of createArgument, createCommand, and createOption. +exports.createCommand = (name) => new Command(name); +exports.createOption = (flags, description) => new Option(flags, description); +exports.createArgument = (name, description) => new Argument(name, description); /** * Expose classes diff --git a/tests/fixtures-extensions/pm.js b/tests/fixtures-extensions/pm.js index 8aff56027..d05bb2a82 100644 --- a/tests/fixtures-extensions/pm.js +++ b/tests/fixtures-extensions/pm.js @@ -1,6 +1,6 @@ #!/usr/bin/env node -const program = require('../../'); +const { program } = require('../../'); program .command('try-ts', 'test file extension lookup') diff --git a/tests/fixtures/inspect.js b/tests/fixtures/inspect.js index 46b99b06d..2ad93ba8a 100644 --- a/tests/fixtures/inspect.js +++ b/tests/fixtures/inspect.js @@ -1,6 +1,6 @@ #!/usr/bin/env node -const program = require('../../'); +const { program } = require('../../'); program .command('sub', 'install one or more packages') diff --git a/tests/fixtures/pm b/tests/fixtures/pm index 40fbd396d..a897597e0 100755 --- a/tests/fixtures/pm +++ b/tests/fixtures/pm @@ -1,6 +1,6 @@ #!/usr/bin/env node -var program = require('../../'); +var { program } = require('../../'); program .version('0.0.1') diff --git a/tests/fixtures/pm-cache.js b/tests/fixtures/pm-cache.js index 121eca247..58346dcf2 100644 --- a/tests/fixtures/pm-cache.js +++ b/tests/fixtures/pm-cache.js @@ -1,4 +1,4 @@ -const program = require('../../'); +const { program } = require('../../'); program .command('clear', 'clear the cache') diff --git a/tests/program.test.js b/tests/program.test.js index 3d5faaf82..51c1b0ee8 100644 --- a/tests/program.test.js +++ b/tests/program.test.js @@ -1,20 +1,67 @@ -const commander = require('../'); +const { + program, + Command, + Option, + Argument, + Help, + CommanderError, + InvalidArgumentError, + InvalidOptionArgumentError, + createCommand, + createOption, + createArgument +} = require('../index.js'); // Do some testing of the default export(s). +// Similar tests to ts-imports.test.ts and esm-imports-test.js. -test('when require commander then is a Command (default export of global)', () => { - // Deprecated global command - const program = commander; - expect(program.constructor.name).toBe('Command'); +/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "checkClass"] }] */ + +function checkClass(obj, name) { + expect(typeof obj).toEqual('object'); + expect(obj.constructor.name).toEqual(name); +} + +test('program', () => { + checkClass(program, 'Command'); +}); + +test('Command', () => { + checkClass(new Command('name'), 'Command'); +}); + +test('Option', () => { + checkClass(new Option('-e, --example', 'description'), 'Option'); +}); + +test('Argument', () => { + checkClass(new Argument('', 'description'), 'Argument'); +}); + +test('Help', () => { + checkClass(new Help(), 'Help'); +}); + +test('CommanderError', () => { + checkClass(new CommanderError(1, 'code', 'failed'), 'CommanderError'); +}); + +test('InvalidArgumentError', () => { + checkClass(new InvalidArgumentError('failed'), 'InvalidArgumentError'); +}); + +test('InvalidOptionArgumentError', () => { // Deprecated + checkClass(new InvalidOptionArgumentError('failed'), 'InvalidArgumentError'); +}); + +test('createCommand', () => { + checkClass(createCommand('foo'), 'Command'); }); -test('when require commander then has program (named export of global)', () => { - // program added in v5 - const program = commander.program; - expect(program.constructor.name).toBe('Command'); +test('createOption', () => { + checkClass(createOption('-e, --example', 'description'), 'Option'); }); -test('when require commander then has newable Command', () => { - const cmd = new commander.Command(); - expect(cmd.constructor.name).toBe('Command'); +test('createArgument', () => { + checkClass(createArgument('', 'description'), 'Argument'); }); From c7895a52548ce8ebdff06cf0c255e57f663d8e32 Mon Sep 17 00:00:00 2001 From: John Gee Date: Fri, 15 Sep 2023 19:30:14 +1200 Subject: [PATCH 2/2] Remove stale code --- tests/ts-imports.test.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/ts-imports.test.ts b/tests/ts-imports.test.ts index 5f6704e2d..e766bcb5f 100644 --- a/tests/ts-imports.test.ts +++ b/tests/ts-imports.test.ts @@ -1,7 +1,5 @@ import { program, Command, Option, CommanderError, InvalidArgumentError, InvalidOptionArgumentError, Help, createCommand } from '../'; -import * as commander from '../'; - // Do some simple checks that expected imports are available at runtime. // Similar tests to esm-imports-test.js @@ -11,10 +9,6 @@ function checkClass(obj: object, name: string): void { expect(obj.constructor.name).toEqual(name); } -test('legacy default export of global Command', () => { - checkClass(commander, 'Command'); -}); - test('program', () => { checkClass(program, 'Command'); });