From cd1bc3f2f9dfafa1236663f29e5c4169a00d5b60 Mon Sep 17 00:00:00 2001 From: Kanad Gupta Date: Thu, 4 Jan 2024 08:16:15 -0600 Subject: [PATCH] feat(oclif): versions:delete --- __tests__/cmds/versions/delete.test.ts | 28 ++++++++++------- src/cmds/versions/delete.ts | 43 +++++++++++--------------- 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/__tests__/cmds/versions/delete.test.ts b/__tests__/cmds/versions/delete.test.ts index ea7f420ee..a0f0fa794 100644 --- a/__tests__/cmds/versions/delete.test.ts +++ b/__tests__/cmds/versions/delete.test.ts @@ -1,37 +1,41 @@ +import type { Config } from '@oclif/core'; + import nock from 'nock'; import prompts from 'prompts'; -import { describe, beforeAll, afterEach, it, expect, vi } from 'vitest'; +import { describe, beforeAll, beforeEach, afterEach, it, expect, vi } from 'vitest'; -import DeleteVersionCommand from '../../../src/cmds/versions/delete.js'; import APIError from '../../../src/lib/apiError.js'; import getAPIMock from '../../helpers/get-api-mock.js'; +import setupOclifConfig from '../../helpers/setup-oclif-config.js'; const key = 'API_KEY'; const version = '1.0.0'; -const deleteVersion = new DeleteVersionCommand(); - describe('rdme versions:delete', () => { + let oclifConfig: Config; + let run: (args?: string[]) => Promise; + beforeAll(() => { nock.disableNetConnect(); }); + beforeEach(async () => { + oclifConfig = await setupOclifConfig(); + run = (args?: string[]) => oclifConfig.runCommand('versions:delete', args); + }); + afterEach(() => nock.cleanAll()); it('should prompt for login if no API key provided', async () => { const consoleInfoSpy = vi.spyOn(console, 'info').mockImplementation(() => {}); prompts.inject(['this-is-not-an-email', 'password', 'subdomain']); - // @ts-expect-error deliberately passing in bad data - await expect(deleteVersion.run({})).rejects.toStrictEqual(new Error('You must provide a valid email address.')); + await expect(run()).rejects.toStrictEqual(new Error('You must provide a valid email address.')); consoleInfoSpy.mockRestore(); }); it('should error in CI if no API key provided', async () => { process.env.TEST_RDME_CI = 'true'; - // @ts-expect-error deliberately passing in bad data - await expect(deleteVersion.run({})).rejects.toStrictEqual( - new Error('No project API key provided. Please use `--key`.'), - ); + await expect(run()).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.')); delete process.env.TEST_RDME_CI; }); @@ -44,7 +48,7 @@ describe('rdme versions:delete', () => { .basicAuth({ user: key }) .reply(200, { version }); - await expect(deleteVersion.run({ key, version })).resolves.toBe('Version 1.0.0 deleted successfully.'); + await expect(run(['--key', key, version])).resolves.toBe('Version 1.0.0 deleted successfully.'); mockRequest.done(); }); @@ -65,7 +69,7 @@ describe('rdme versions:delete', () => { .basicAuth({ user: key }) .reply(200, { version }); - await expect(deleteVersion.run({ key, version })).rejects.toStrictEqual(new APIError(errorResponse)); + await expect(run(['--key', key, version])).rejects.toThrow(new APIError(errorResponse)); mockRequest.done(); }); }); diff --git a/src/cmds/versions/delete.ts b/src/cmds/versions/delete.ts index c6df2e086..17746facd 100644 --- a/src/cmds/versions/delete.ts +++ b/src/cmds/versions/delete.ts @@ -1,37 +1,30 @@ -import type { AuthenticatedCommandOptions } from '../../lib/baseCommand.js'; +import { Args } from '@oclif/core'; -import Command, { CommandCategories } from '../../lib/baseCommand.js'; +import BaseCommand from '../../lib/baseCommandNew.js'; +import { keyFlag } from '../../lib/flags.js'; import readmeAPIFetch, { cleanHeaders, handleRes } from '../../lib/readmeAPIFetch.js'; import { getProjectVersion } from '../../lib/versionSelect.js'; -export default class DeleteVersionCommand extends Command { - constructor() { - super(); - - this.command = 'versions:delete'; - this.usage = 'versions:delete [options]'; - this.description = 'Delete a version associated with your ReadMe project.'; - this.cmdCategory = CommandCategories.VERSIONS; - - this.hiddenArgs = ['version']; - this.args = [ - this.getKeyArg(), - { - name: 'version', - type: String, - defaultOption: true, - }, - ]; - } +export default class DeleteVersionCommand extends BaseCommand { + static description = 'Delete a version associated with your ReadMe project.'; + + static args = { + version: Args.string({ + description: "The version you'd like to delete.", + }), + }; - async run(opts: AuthenticatedCommandOptions) { - await super.run(opts); + static flags = { + key: keyFlag, + }; - const { key, version } = opts; + async run(): Promise { + const { version } = this.args; + const { key } = this.flags; const selectedVersion = await getProjectVersion(version, key); - Command.debug(`selectedVersion: ${selectedVersion}`); + this.debug(`selectedVersion: ${selectedVersion}`); return readmeAPIFetch(`/api/v1/version/${selectedVersion}`, { method: 'delete',