Skip to content

Commit

Permalink
feat(breaking): drop version creation question from prompt flows (#635)
Browse files Browse the repository at this point in the history
* feat(breaking): drop version creation question from prompt flows

* chore(openapi): drop prompt

* refactor: cleanup unused version creation code

* test(openapi): reflect new prompt flow

* refactor: simplify prompt, consolidate types

* chore: smol cleanup
  • Loading branch information
kanadgupta authored Oct 13, 2022
1 parent 542307b commit 6223ae5
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 108 deletions.
11 changes: 4 additions & 7 deletions __tests__/cmds/openapi/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ describe('rdme openapi', () => {
});

it('should create a new spec via `--create` flag and ignore `--id`', async () => {
prompts.inject(['update', version]);
prompts.inject([version]);
const registryUUID = getRandomRegistryId();

const mock = getAPIMock()
Expand Down Expand Up @@ -807,17 +807,14 @@ describe('rdme openapi', () => {

it('should request a version list if version is not found', async () => {
const selectedVersion = '1.0.1';
prompts.inject(['create', selectedVersion]);
prompts.inject([selectedVersion]);

const registryUUID = getRandomRegistryId();

const mock = getAPIMock()
.get('/api/v1/version')
.basicAuth({ user: key })
.reply(200, [{ version: '1.0.0' }])
.post('/api/v1/version', { from: '1.0.0', version: '1.0.1', is_stable: false })
.basicAuth({ user: key })
.reply(200, { from: '1.0.0', version: '1.0.1' })
.reply(200, [{ version: '1.0.0' }, { version: '1.0.1' }])
.post('/api/v1/api-registry', body => body.match('form-data; name="spec"'))
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } });

Expand Down Expand Up @@ -1285,7 +1282,7 @@ describe('rdme openapi', () => {
it('should create GHA workflow (--create flag enabled with ignored id opt)', async () => {
expect.assertions(3);
const yamlFileName = 'openapi-file-create-flag-id-opt';
prompts.inject(['update', version, true, 'openapi-branch-create-flag-id-opt', yamlFileName]);
prompts.inject([version, true, 'openapi-branch-create-flag-id-opt', yamlFileName]);
const registryUUID = getRandomRegistryId();

const mock = getAPIMock()
Expand Down
29 changes: 4 additions & 25 deletions __tests__/lib/prompts.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VersionCreateOptions } from '../../src/cmds/versions/create';
import type { VersionUpdateOptions } from '../../src/cmds/versions/update';
import type { Response } from 'node-fetch';

import prompts from 'prompts';
Expand All @@ -9,9 +9,11 @@ import promptTerminal from '../../src/lib/promptWrapper';
const versionlist = [
{
version: '1',
is_stable: true,
},
{
version: '2',
is_stable: false,
},
];

Expand All @@ -38,29 +40,6 @@ const getSpecs = () => {
};

describe('prompt test bed', () => {
describe('generatePrompts()', () => {
it('should return an update option if selected', async () => {
prompts.inject(['update', '2']);

const answer = await promptTerminal(promptHandler.generatePrompts(versionlist));
expect(answer).toStrictEqual({ option: 'update', versionSelection: '2' });
});

it('should return a create option if selected', async () => {
prompts.inject(['create', '1.1']);

const answer = await promptTerminal(promptHandler.generatePrompts(versionlist));
expect(answer).toStrictEqual({ newVersion: '1.1', option: 'create' });
});

it('should return an update option if selectOnly=true', async () => {
prompts.inject(['2']);

const answer = await promptTerminal(promptHandler.generatePrompts(versionlist, true));
expect(answer).toStrictEqual({ versionSelection: '2' });
});
});

describe('createOasPrompt()', () => {
it('should return a create option if selected', async () => {
prompts.inject(['create']);
Expand Down Expand Up @@ -104,7 +83,7 @@ describe('prompt test bed', () => {

describe('createVersionPrompt()', () => {
it('should allow user to choose a fork if flag is not passed (creating version)', async () => {
const opts = { newVersion: '1.2.1' } as VersionCreateOptions;
const opts: VersionUpdateOptions = { newVersion: '1.2.1' };

prompts.inject(['1', true, true]);

Expand Down
2 changes: 1 addition & 1 deletion src/cmds/categories/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class CategoriesCreateCommand extends Command {
return Promise.reject(new Error('`categoryType` must be `guide` or `reference`.'));
}

const selectedVersion = await getProjectVersion(version, key, false);
const selectedVersion = await getProjectVersion(version, key);

Command.debug(`selectedVersion: ${selectedVersion}`);

Expand Down
2 changes: 1 addition & 1 deletion src/cmds/categories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class CategoriesCommand extends Command {

const { key, version } = opts;

const selectedVersion = await getProjectVersion(version, key, true);
const selectedVersion = await getProjectVersion(version, key);

Command.debug(`selectedVersion: ${selectedVersion}`);

Expand Down
2 changes: 1 addition & 1 deletion src/cmds/docs/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class EditDocsCommand extends Command {
return Promise.reject(new Error(`No slug provided. Usage \`${config.get('cli')} ${this.usage}\`.`));
}

const selectedVersion = await getProjectVersion(version, key, true);
const selectedVersion = await getProjectVersion(version, key);

Command.debug(`selectedVersion: ${selectedVersion}`);

Expand Down
2 changes: 1 addition & 1 deletion src/cmds/docs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class DocsCommand extends Command {
// TODO: should we allow version selection at all here?
// Let's revisit this once we re-evaluate our category logic in the API.
// Ideally we should ignore this parameter entirely if the category is included.
const selectedVersion = await getProjectVersion(version, key, false);
const selectedVersion = await getProjectVersion(version, key);

Command.debug(`selectedVersion: ${selectedVersion}`);

Expand Down
2 changes: 1 addition & 1 deletion src/cmds/docs/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class SingleDocCommand extends Command {
// TODO: should we allow version selection at all here?
// Let's revisit this once we re-evaluate our category logic in the API.
// Ideally we should ignore this parameter entirely if the category is included.
const selectedVersion = await getProjectVersion(version, key, false);
const selectedVersion = await getProjectVersion(version, key);

Command.debug(`selectedVersion: ${selectedVersion}`);

Expand Down
2 changes: 1 addition & 1 deletion src/cmds/openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export default class OpenAPICommand extends Command {
}

if (create || !id) {
selectedVersion = await getProjectVersion(selectedVersion, key, true);
selectedVersion = await getProjectVersion(selectedVersion, key);
}

Command.debug(`selectedVersion: ${selectedVersion}`);
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/versions/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class DeleteVersionCommand extends Command {

const { key, version } = opts;

const selectedVersion = await getProjectVersion(version, key, false);
const selectedVersion = await getProjectVersion(version, key);

Command.debug(`selectedVersion: ${selectedVersion}`);

Expand Down
2 changes: 1 addition & 1 deletion src/cmds/versions/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class UpdateVersionCommand extends Command {

const { key, version, newVersion, codename, main, beta, isPublic, deprecated } = opts;

const selectedVersion = await getProjectVersion(version, key, false);
const selectedVersion = await getProjectVersion(version, key);

Command.debug(`selectedVersion: ${selectedVersion}`);

Expand Down
42 changes: 2 additions & 40 deletions src/lib/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Version } from '../cmds/versions';
import type { VersionCreateOptions } from 'cmds/versions/create';
import type { VersionUpdateOptions } from 'cmds/versions/update';
import type { Response } from 'node-fetch';
Expand All @@ -13,10 +14,6 @@ type SpecList = {
title: string;
}[];

type VersionList = {
version: string;
}[];

type ParsedDocs = {
next?: {
page: number;
Expand All @@ -28,41 +25,6 @@ type ParsedDocs = {
};
};

export function generatePrompts(versionList: VersionList, selectOnly = false): PromptObject[] {
return [
{
type: selectOnly ? null : 'select',
name: 'option',
message: 'Would you like to use an existing project version or create a new one?',
choices: [
{ title: 'Use existing', value: 'update' },
{ title: 'Create a new version', value: 'create' },
],
},
{
type: (prev, values) => {
return (selectOnly ? false : values.option !== 'update') ? null : 'select';
},
name: 'versionSelection',
message: 'Select your desired version',
choices: versionList.map(v => {
return {
title: v.version,
value: v.version,
};
}),
},
{
type: (prev, values) => {
return (selectOnly ? true : values.option === 'update') ? null : 'text';
},
name: 'newVersion',
message: "What's your new version?",
hint: '1.0.0',
},
];
}

function specOptions(specList: SpecList, parsedDocs: ParsedDocs, currPage: number, totalPages: number): Choice[] {
const specs = specList.map(s => {
return {
Expand Down Expand Up @@ -166,7 +128,7 @@ export function createOasPrompt(
}

export function createVersionPrompt(
versionList: VersionList,
versionList: Version[],
opts: VersionCreateOptions & VersionUpdateOptions,
isUpdate?: {
is_stable: boolean;
Expand Down
40 changes: 12 additions & 28 deletions src/lib/versionSelect.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import type { Version } from '../cmds/versions';

import config from 'config';
import { Headers } from 'node-fetch';

import APIError from './apiError';
import fetch, { cleanHeaders, handleRes } from './fetch';
import isCI from './isCI';
import { warn } from './logger';
import * as promptHandler from './prompts';
import promptTerminal from './promptWrapper';

/**
* Validates and returns a project version.
*
* @param versionFlag version input parameter
* @param key project API key
* @param allowNewVersion if true, goes through prompt flow
* for creating a new project version
* @returns a cleaned up project version
*/
export async function getProjectVersion(versionFlag: string, key: string, allowNewVersion: boolean): Promise<string> {
export async function getProjectVersion(versionFlag: string, key: string): Promise<string> {
try {
if (versionFlag) {
return await fetch(`${config.get('host')}/api/v1/version/${versionFlag}`, {
Expand All @@ -40,29 +36,17 @@ export async function getProjectVersion(versionFlag: string, key: string, allowN
headers: cleanHeaders(key),
}).then(res => handleRes(res));

if (allowNewVersion) {
const { option, versionSelection, newVersion } = await promptTerminal(promptHandler.generatePrompts(versionList));

if (option === 'update') return versionSelection;

const body: Version = {
from: versionList[0].version,
version: newVersion,
is_stable: false,
};

const newVersionFromApi = await fetch(`${config.get('host')}/api/v1/version`, {
method: 'post',
headers: cleanHeaders(key, new Headers({ 'Content-Type': 'application/json' })),
body: JSON.stringify(body),
})
.then(res => handleRes(res))
.then((res: Version) => res.version);

return newVersionFromApi;
}

const { versionSelection } = await promptTerminal(promptHandler.generatePrompts(versionList, true));
const { versionSelection } = await promptTerminal({
type: 'select',
name: 'versionSelection',
message: 'Select your desired version',
choices: versionList.map(v => {
return {
title: v.version,
value: v.version,
};
}),
});

return versionSelection;
} catch (err) {
Expand Down

0 comments on commit 6223ae5

Please sign in to comment.