Skip to content

Commit

Permalink
feat(oclif): versions:delete
Browse files Browse the repository at this point in the history
  • Loading branch information
kanadgupta committed Jan 4, 2024
1 parent b6c432b commit cd1bc3f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
28 changes: 16 additions & 12 deletions __tests__/cmds/versions/delete.test.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>;

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;
});

Expand All @@ -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();
});

Expand All @@ -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();
});
});
43 changes: 18 additions & 25 deletions src/cmds/versions/delete.ts
Original file line number Diff line number Diff line change
@@ -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 <version> [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<typeof DeleteVersionCommand> {
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<string> {
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',
Expand Down

0 comments on commit cd1bc3f

Please sign in to comment.