From 210c6a9a46b9f83c80438ec6775d81ba8834fb2a Mon Sep 17 00:00:00 2001 From: Gabriel Ratcliff Date: Mon, 1 Jul 2019 17:02:18 -0700 Subject: [PATCH] CLI updates for version API. WIP --- lib/prompts.js | 34 +++++++++++++++++++++----- lib/swagger.js | 31 ++++++++++++------------ lib/versions/create.js | 55 ++++++++++++++++++++++++++++++++++++++++++ lib/versions/delete.js | 35 +++++++++++++++++++++++++++ lib/versions/list.js | 29 ++++++++++++++++++++++ lib/versions/update.js | 0 test/prompts.test.js | 17 +++++++++++++ 7 files changed, 179 insertions(+), 22 deletions(-) create mode 100644 lib/versions/create.js create mode 100644 lib/versions/delete.js create mode 100644 lib/versions/list.js create mode 100644 lib/versions/update.js create mode 100644 test/prompts.test.js diff --git a/lib/prompts.js b/lib/prompts.js index 7eea7aa7c..cc0b8dad4 100644 --- a/lib/prompts.js +++ b/lib/prompts.js @@ -1,23 +1,45 @@ -exports.generatePrompts = (versionList) => [ +exports.generatePrompts = versionList => [ { type: 'select', name: 'option', - message: 'We couldn\'t find a version in ReadMe matching the version in your OAS file. Would you like to use an existing version or create a new one?', + message: + "We couldn't find a version in ReadMe matching the version in your OAS file. Would you like to use an existing version or create a new one?", choices: [ { title: 'Use existing', value: 'update' }, - { title: 'Create a new version', value: 'create' } + { title: 'Create a new version', value: 'create' }, ], }, { - type: prev => prev === 'update' ? 'select' : null, + type: prev => (prev === 'update' ? 'select' : null), name: 'versionSelection', message: 'Select your desired version', choices: versionList.map(v => { return { title: v.version, // eslint-disable-next-line - value: v._id + value: v._id, }; }), - } + }, +]; + +exports.createVersionPrompt = versionList => [ + { + type: 'select', + name: 'fork', + message: 'Which version would you like to fork from?', + choices: versionList.map(v => { + return { + title: v.version, + // eslint-disable-next-line + value: v._id, + }; + }), + }, + { + type: 'select', + name: 'main', + message: 'Would you like to make this version the main version?', + choices: [{ title: 'Yes', value: true }, { title: 'No', value: false }], + }, ]; diff --git a/lib/swagger.js b/lib/swagger.js index e1db84d48..a327fed4f 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -8,19 +8,6 @@ const promptOpts = require('./prompts'); exports.desc = 'Upload your swagger file to ReadMe'; exports.category = 'services'; exports.weight = 2; - -async function versionPrompt(versionList, specPath) { - const promptSelection = promptOpts.generatePrompts(versionList); - const res = await prompts(promptSelection); - - if (res.option === 'create') { - callApi(specPath, res.option); - } else if (res.option === 'update') { - const versionId = res.versionSelection; - callApi(specPath, res.option, versionId); - } -} - exports.run = async function({ args, opts }) { let { key, id } = opts; @@ -36,6 +23,18 @@ exports.run = async function({ args, opts }) { } function callApi(specPath, versionAction, version) { + async function versionPrompt(versionList) { + const promptSelection = promptOpts.generatePrompts(versionList); + const res = await prompts(promptSelection); + + if (res.option === 'create') { + return callApi(specPath, res.option); + } + + const versionId = res.versionSelection; + return callApi(specPath, res.option, versionId); + } + function success(data) { const message = !id ? "You've successfully uploaded a new swagger file to your ReadMe project!" @@ -61,7 +60,7 @@ exports.run = async function({ args, opts }) { try { const errorDesc = JSON.parse(err.error).description; if (typeof errorDesc === 'object') { - return Promise.resolve(versionPrompt(errorDesc, specPath)); + return Promise.resolve(versionPrompt(errorDesc)); } return Promise.reject(new Error(errorDesc)); } catch (e) { @@ -77,8 +76,8 @@ exports.run = async function({ args, opts }) { resolveWithFullResponse: true, headers: { versionAction, - version - } + version, + }, }; // Create diff --git a/lib/versions/create.js b/lib/versions/create.js new file mode 100644 index 000000000..14f08e1b5 --- /dev/null +++ b/lib/versions/create.js @@ -0,0 +1,55 @@ +const request = require('request-promise-native'); +const config = require('config'); +const prompts = require('prompts'); +const promptOpts = require('../prompts'); + +exports.desc = 'Create a new version for your project'; +exports.category = 'services'; +exports.weight = 4; +exports.action = 'versions:create'; + +exports.run = async function({ opts }) { + let { key } = opts; + const { version, codename } = opts; + + if (!key && opts.token) { + console.warn( + 'Using `rdme` with --token has been deprecated. Please use --key and --id instead', + ); + [key] = opts.token.split('-'); + } + + if (!key) { + return Promise.reject(new Error('No api key provided. Please use --key')); + } + + if (!version) { + return Promise.reject( + new Error('No version provided. Please specify a semantic version using --version'), + ); + } + + const versionList = await request + .get(`${config.host}/api/v1/version`, { + json: true, + auth: { user: key }, + }) + .catch(err => Promise.reject(new Error(err))); + + const promptSelection = promptOpts.createVersionPrompt(versionList); + const { fork, main } = await prompts(promptSelection); + + const options = { + json: { + version, + fork, + codename: codename || '', + is_stable: main, + }, + auth: { user: key }, + }; + + return request + .post(`${config.host}/api/v1/version`, options) + .catch(err => Promise.reject(new Error(err))); +}; diff --git a/lib/versions/delete.js b/lib/versions/delete.js new file mode 100644 index 000000000..d2550d9ca --- /dev/null +++ b/lib/versions/delete.js @@ -0,0 +1,35 @@ +const request = require('request-promise-native'); +const config = require('config'); + +exports.desc = 'Delete a version associated with your ReadMe project'; +exports.category = 'services'; +exports.weight = 4; +exports.action = 'versions:delete'; + +exports.run = async function({ opts }) { + let { key } = opts; + const { version } = opts; + + if (!key && opts.token) { + console.warn( + 'Using `rdme` with --token has been deprecated. Please use --key and --id instead', + ); + [key] = opts.token.split('-'); + } + + if (!key) { + return Promise.reject(new Error('No api key provided. Please use --key')); + } + + if (!version) { + return Promise.reject( + new Error('No version provided. Please specify a semantic version using --version'), + ); + } + + return request + .delete(`${config.host}/api/v1/version/${version}`, { + auth: { user: key }, + }) + .catch(err => Promise.reject(new Error(err))); +}; diff --git a/lib/versions/list.js b/lib/versions/list.js new file mode 100644 index 000000000..52fe76128 --- /dev/null +++ b/lib/versions/list.js @@ -0,0 +1,29 @@ +const request = require('request-promise-native'); +const config = require('config'); + +exports.desc = 'List versions available in your project'; +exports.category = 'services'; +exports.weight = 4; +exports.action = 'versions:list'; + +exports.run = function({ opts }) { + let { key } = opts; + + if (!key && opts.token) { + console.warn( + 'Using `rdme` with --token has been deprecated. Please use --key and --id instead', + ); + [key] = opts.token.split('-'); + } + + if (!key) { + return Promise.reject(new Error('No api key provided. Please use --key')); + } + + return request + .get(`${config.host}/api/v1/version`, { + json: true, + auth: { user: key }, + }) + .catch(err => Promise.reject(new Error(err))); +}; diff --git a/lib/versions/update.js b/lib/versions/update.js new file mode 100644 index 000000000..e69de29bb diff --git a/test/prompts.test.js b/test/prompts.test.js new file mode 100644 index 000000000..dca26e022 --- /dev/null +++ b/test/prompts.test.js @@ -0,0 +1,17 @@ +const assert = require('assert'); +const promptHandler = require('../lib/prompts'); + +const versionlist = [ + { + version: '1', + _id: '32', + }, +]; + +describe('generatePrompts()', () => { + it('should create an array of objects based on provided version list', () => { + const res = promptHandler.generatePrompts(versionlist); + console.log(res[1].choices); + assert.equal(res[1].choices[0].title, '1'); + }); +});