Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/help-semver-validation #78

Merged
merged 2 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cmds/versions/create.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const request = require('request-promise-native');
const config = require('config');
const semver = require('semver');
const { prompt } = require('enquirer');
const promptOpts = require('../../lib/prompts');

exports.command = 'versions:create';
exports.usage = 'versions:create <version> | --version={project-version} [options]';
exports.usage = 'versions:create --version=<version> [options]';
exports.description = 'Create a new version for your project.';
exports.category = 'versions';
exports.position = 2;
Expand Down Expand Up @@ -56,10 +57,10 @@ exports.run = async function(opts) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

if (!version) {
if (!version || !semver.valid(semver.coerce(version))) {
return Promise.reject(
new Error(
`No version provided. Please specify a semantic version. See \`${config.cli} help ${exports.command}\` for help.`,
`Please specify a semantic version. See \`${config.cli} help ${exports.command}\` for help.`,
),
);
}
Expand Down
7 changes: 4 additions & 3 deletions cmds/versions/delete.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const request = require('request-promise-native');
const config = require('config');
const semver = require('semver');

exports.command = 'versions:delete';
exports.usage = 'versions:delete --version={project-version} [options]';
exports.usage = 'versions:delete --version=<version> [options]';
exports.description = 'Delete a version associated with your ReadMe project.';
exports.category = 'versions';
exports.position = 4;
Expand All @@ -28,10 +29,10 @@ exports.run = async function(opts) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

if (!version) {
if (!version || !semver.valid(semver.coerce(version))) {
return Promise.reject(
new Error(
`No version provided. Please specify a semantic version. See \`${config.cli} help ${exports.command}\` for help.`,
`Please specify a semantic version. See \`${config.cli} help ${exports.command}\` for help.`,
),
);
}
Expand Down
7 changes: 4 additions & 3 deletions cmds/versions/update.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const request = require('request-promise-native');
const config = require('config');
const semver = require('semver');
const { prompt } = require('enquirer');
const promptOpts = require('../../lib/prompts');

exports.command = 'versions:update';
exports.usage = 'versions:update --version={project-version} [options]';
exports.usage = 'versions:update --version=<version> [options]';
exports.description = 'Update an existing version for your project.';
exports.category = 'versions';
exports.position = 3;
Expand Down Expand Up @@ -49,10 +50,10 @@ exports.run = async function(opts) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

if (!version) {
if (!version || !semver.valid(semver.coerce(version))) {
return Promise.reject(
new Error(
`No version provided. Please specify a semantic version. See \`${config.cli} help ${exports.command}\` for help.`,
`Please specify a semantic version. See \`${config.cli} help ${exports.command}\` for help.`,
),
);
}
Expand Down
7 changes: 7 additions & 0 deletions lib/prompts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const semver = require('semver');

exports.generatePrompts = versionList => [
{
type: 'select',
Expand Down Expand Up @@ -57,6 +59,11 @@ exports.createVersionPrompt = (versionList, opts, isUpdate) => [
return opts.newVersion || !isUpdate;
},
hint: '1.0.0',
validate(val) {
return semver.valid(semver.coerce(val))
? true
: this.styles.danger('Please specify a semantic version.');
},
},
{
type: 'confirm',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"read": "^1.0.7",
"request": "^2.88.0",
"request-promise-native": "^1.0.5",
"semver": "^6.3.0",
"table-layout": "^1.0.0"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions test/cmds/versions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('rdme versions*', () => {
createVersion.run({ key }).catch(err => {
assert.equal(
err.message,
'No version provided. Please specify a semantic version. See `rdme help versions:create` for help.',
'Please specify a semantic version. See `rdme help versions:create` for help.',
);
});
});
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('rdme versions*', () => {
deleteVersion.run({ key }).catch(err => {
assert.equal(
err.message,
'No version provided. Please specify a semantic version. See `rdme help versions:delete` for help.',
'Please specify a semantic version. See `rdme help versions:delete` for help.',
);
});
});
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('rdme versions*', () => {
updateVersion.run({ key }).catch(err => {
assert.equal(
err.message,
'No version provided. Please specify a semantic version. See `rdme help versions:update` for help.',
'Please specify a semantic version. See `rdme help versions:update` for help.',
);
});
});
Expand Down
5 changes: 5 additions & 0 deletions test/lib/prompts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ describe('prompt test bed', () => {
await prompt.keypress(null, { name: 'down' });
await prompt.keypress(null, { name: 'up' });
await prompt.submit();
if (prompt.name === 'newVersion') {
// eslint-disable-next-line
prompt.value = '1.2.1';
await prompt.submit();
}
});
const answer = await enquirer.prompt(
promptHandler.createVersionPrompt(versionlist, opts, false),
Expand Down