diff --git a/__tests__/cmds/openapi/index.test.ts b/__tests__/cmds/openapi/index.test.ts index 22915e9e8..86eb35779 100644 --- a/__tests__/cmds/openapi/index.test.ts +++ b/__tests__/cmds/openapi/index.test.ts @@ -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() @@ -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' } }); @@ -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() diff --git a/__tests__/lib/prompts.test.ts b/__tests__/lib/prompts.test.ts index 9fcfc2bae..12f5e0f87 100644 --- a/__tests__/lib/prompts.test.ts +++ b/__tests__/lib/prompts.test.ts @@ -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'; @@ -9,9 +9,11 @@ import promptTerminal from '../../src/lib/promptWrapper'; const versionlist = [ { version: '1', + is_stable: true, }, { version: '2', + is_stable: false, }, ]; @@ -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']); @@ -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]); diff --git a/src/cmds/categories/create.ts b/src/cmds/categories/create.ts index 2019153ca..7c7ee5503 100644 --- a/src/cmds/categories/create.ts +++ b/src/cmds/categories/create.ts @@ -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}`); diff --git a/src/cmds/categories/index.ts b/src/cmds/categories/index.ts index 933f9e444..cb572ed4f 100644 --- a/src/cmds/categories/index.ts +++ b/src/cmds/categories/index.ts @@ -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}`); diff --git a/src/cmds/docs/edit.ts b/src/cmds/docs/edit.ts index 24b66c1df..f2556dbe3 100644 --- a/src/cmds/docs/edit.ts +++ b/src/cmds/docs/edit.ts @@ -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}`); diff --git a/src/cmds/docs/index.ts b/src/cmds/docs/index.ts index 9198b21d0..ba0835075 100644 --- a/src/cmds/docs/index.ts +++ b/src/cmds/docs/index.ts @@ -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}`); diff --git a/src/cmds/docs/single.ts b/src/cmds/docs/single.ts index a13cd1aa2..b4c00442c 100644 --- a/src/cmds/docs/single.ts +++ b/src/cmds/docs/single.ts @@ -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}`); diff --git a/src/cmds/openapi/index.ts b/src/cmds/openapi/index.ts index f6b9ff9ff..caef1f1f4 100644 --- a/src/cmds/openapi/index.ts +++ b/src/cmds/openapi/index.ts @@ -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}`); diff --git a/src/cmds/versions/delete.ts b/src/cmds/versions/delete.ts index fc1dc799d..cb97fecdc 100644 --- a/src/cmds/versions/delete.ts +++ b/src/cmds/versions/delete.ts @@ -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}`); diff --git a/src/cmds/versions/update.ts b/src/cmds/versions/update.ts index 4c0b808f8..e4605250e 100644 --- a/src/cmds/versions/update.ts +++ b/src/cmds/versions/update.ts @@ -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}`); diff --git a/src/lib/prompts.ts b/src/lib/prompts.ts index bb8da6b08..6d93d2021 100644 --- a/src/lib/prompts.ts +++ b/src/lib/prompts.ts @@ -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'; @@ -13,10 +14,6 @@ type SpecList = { title: string; }[]; -type VersionList = { - version: string; -}[]; - type ParsedDocs = { next?: { page: number; @@ -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 { @@ -166,7 +128,7 @@ export function createOasPrompt( } export function createVersionPrompt( - versionList: VersionList, + versionList: Version[], opts: VersionCreateOptions & VersionUpdateOptions, isUpdate?: { is_stable: boolean; diff --git a/src/lib/versionSelect.ts b/src/lib/versionSelect.ts index 3a0b34a58..c1c4e9f6f 100644 --- a/src/lib/versionSelect.ts +++ b/src/lib/versionSelect.ts @@ -1,13 +1,11 @@ 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'; /** @@ -15,11 +13,9 @@ import promptTerminal from './promptWrapper'; * * @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 { +export async function getProjectVersion(versionFlag: string, key: string): Promise { try { if (versionFlag) { return await fetch(`${config.get('host')}/api/v1/version/${versionFlag}`, { @@ -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) {