Skip to content

Commit

Permalink
CLI updates for version API. WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gratcliff committed Jul 2, 2019
1 parent 8f8fe3e commit 210c6a9
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 22 deletions.
34 changes: 28 additions & 6 deletions lib/prompts.js
Original file line number Diff line number Diff line change
@@ -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 }],
},
];
31 changes: 15 additions & 16 deletions lib/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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!"
Expand All @@ -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) {
Expand All @@ -77,8 +76,8 @@ exports.run = async function({ args, opts }) {
resolveWithFullResponse: true,
headers: {
versionAction,
version
}
version,
},
};

// Create
Expand Down
55 changes: 55 additions & 0 deletions lib/versions/create.js
Original file line number Diff line number Diff line change
@@ -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)));
};
35 changes: 35 additions & 0 deletions lib/versions/delete.js
Original file line number Diff line number Diff line change
@@ -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)));
};
29 changes: 29 additions & 0 deletions lib/versions/list.js
Original file line number Diff line number Diff line change
@@ -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)));
};
Empty file added lib/versions/update.js
Empty file.
17 changes: 17 additions & 0 deletions test/prompts.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});

0 comments on commit 210c6a9

Please sign in to comment.