Skip to content

Commit

Permalink
CLI Versioning WIP:
Browse files Browse the repository at this point in the history
- Added prompts package
- Functionality check versioning against main repo
  • Loading branch information
gratcliff committed Jul 1, 2019
1 parent 4a3a5b8 commit 8f8fe3e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
23 changes: 23 additions & 0 deletions lib/prompts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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?',
choices: [
{ title: 'Use existing', value: 'update' },
{ title: 'Create a new version', value: 'create' }
],
},
{
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
};
}),
}
];
28 changes: 25 additions & 3 deletions lib/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@ const request = require('request-promise-native');
const fs = require('fs');
const path = require('path');
const config = require('config');
const prompts = require('prompts');
const promptOpts = require('./prompts');

exports.desc = 'Upload your swagger file to ReadMe';
exports.category = 'services';
exports.weight = 2;

exports.run = function({ args, opts }) {
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;

if (!key && opts.token) {
Expand All @@ -21,7 +35,7 @@ exports.run = function({ args, opts }) {
return Promise.reject(new Error('No api key provided. Please use --key'));
}

function callApi(specPath) {
function callApi(specPath, versionAction, version) {
function success(data) {
const message = !id
? "You've successfully uploaded a new swagger file to your ReadMe project!"
Expand All @@ -45,7 +59,11 @@ exports.run = function({ args, opts }) {

function error(err) {
try {
return Promise.reject(new Error(JSON.parse(err.error).description));
const errorDesc = JSON.parse(err.error).description;
if (typeof errorDesc === 'object') {
return Promise.resolve(versionPrompt(errorDesc, specPath));
}
return Promise.reject(new Error(errorDesc));
} catch (e) {
return Promise.reject(new Error('There was an error uploading!'));
}
Expand All @@ -57,6 +75,10 @@ exports.run = function({ args, opts }) {
},
auth: { user: key },
resolveWithFullResponse: true,
headers: {
versionAction,
version
}
};

// Create
Expand Down
7 changes: 2 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
"gray-matter": "^4.0.1",
"isemail": "^3.1.3",
"minimist": "^1.2.0",
"oas": "0.8.15",
"opn": "^6.0.0",
"prompts": "^2.1.0",
"read": "^1.0.7",
"oas": "0.8.15",
"request": "^2.88.0",
"request-promise-native": "^1.0.5"
},
Expand Down

0 comments on commit 8f8fe3e

Please sign in to comment.