Skip to content

Commit

Permalink
CLI functionality for rdme version CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
gratcliff committed Jul 2, 2019
1 parent 210c6a9 commit c786949
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 60 deletions.
129 changes: 85 additions & 44 deletions lib/prompts.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,86 @@
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,
};
}),
},
];
const { prompt } = require('enquirer');

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 }],
},
];
exports.generatePrompts = async versionList =>
prompt([
{
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: [
{ message: 'Use existing', value: 'update' },
{ message: 'Create a new version', value: 'create' },
],
},
{
type: 'select',
name: 'versionSelection',
message: 'Select your desired version',
skip() {
return this.enquirer.answers.option !== 'update';
},
choices: versionList.map(v => {
return {
message: v.version,
// eslint-disable-next-line
value: v._id,
};
}),
},
]);

exports.createVersionPrompt = async (versionList, opts, isUpdate) =>
prompt([
{
type: 'select',
name: 'from',
message: 'Which version would you like to fork from?',
skip() {
return opts.fork || isUpdate;
},
choices: versionList.map(v => {
return {
message: v.version,
// eslint-disable-next-line
value: v.version,
};
}),
},
{
type: 'input',
name: 'newVersion',
message: "What's your new version?",
skip() {
return opts.newVersion || !isUpdate;
},
hint: '1.0.0',
},
{
type: 'confirm',
name: 'is_stable',
message: 'Would you like to make this version the main version for this project?',
skip: () => opts.main,
},
{
type: 'confirm',
name: 'is_beta',
message: 'Should this version be in beta?',
skip: () => opts.beta,
},
{
type: 'confirm',
name: 'is_hidden',
message: 'Would you like to make this version public?',
skip() {
return opts.isPublic || opts.main || this.enquirer.answers.is_stable;
},
},
{
type: 'confirm',
name: 'is_deprecated',
message: 'Would you like to deprecate this version?',
skip() {
return opts.deprecated || opts.main || !isUpdate || this.enquirer.answers.is_stable;
},
},
]);
28 changes: 15 additions & 13 deletions lib/versions/create.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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';
Expand All @@ -10,7 +9,8 @@ exports.action = 'versions:create';

exports.run = async function({ opts }) {
let { key } = opts;
const { version, codename } = opts;
let versionList;
const { version, codename, fork, main, beta, isPublic } = opts;

if (!key && opts.token) {
console.warn(
Expand All @@ -29,22 +29,24 @@ exports.run = async function({ opts }) {
);
}

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);
if (!fork) {
versionList = await request
.get(`${config.host}/api/v1/version`, {
json: true,
auth: { user: key },
})
.catch(err => Promise.reject(new Error(err)));
}

const promptResponse = await promptOpts.createVersionPrompt(versionList, opts);
const options = {
json: {
version,
fork,
codename: codename || '',
is_stable: main,
is_stable: main || promptResponse.is_stable,
is_beta: beta || promptResponse.is_beta,
from: fork || promptResponse.from,
is_hidden: promptResponse.is_stable ? false : !(isPublic || promptResponse.is_hidden),
},
auth: { user: key },
};
Expand Down
57 changes: 57 additions & 0 deletions lib/versions/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const request = require('request-promise-native');
const config = require('config');
const promptOpts = require('../prompts');

exports.desc = 'Update an existing version for your project';
exports.category = 'services';
exports.weight = 4;
exports.action = 'versions:update';

exports.run = async function({ opts }) {
let { key } = opts;
let versionList;
const { version, codename, fork, newVersion, main, beta, isPublic, deprecated } = 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'),
);
}

if (!fork) {
versionList = await request
.get(`${config.host}/api/v1/version`, {
json: true,
auth: { user: key },
})
.catch(err => Promise.reject(new Error(err)));
}

const promptResponse = await promptOpts.createVersionPrompt(versionList, opts, true);
const options = {
json: {
codename: codename || '',
version: newVersion || promptResponse.newVersion,
is_stable: main || promptResponse.is_stable,
is_beta: beta || promptResponse.is_beta,
is_deprecated: deprecated || promptResponse.is_deprecated,
is_hidden: promptResponse.is_stable ? false : !(isPublic || promptResponse.is_hidden),
},
auth: { user: key },
};

return request
.put(`${config.host}/api/v1/version/${version}`, options)
.catch(err => Promise.reject(new Error(err)));
};
20 changes: 18 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
"config": "^3.1.0",
"configstore": "^5.0.0",
"editor": "^1.0.0",
"enquirer": "^2.3.0",
"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",
"request": "^2.88.0",
"request-promise-native": "^1.0.5"
Expand Down

0 comments on commit c786949

Please sign in to comment.