From 3b42f4003bba1922054581bd5e1adb5856c2d46c Mon Sep 17 00:00:00 2001 From: Gabriel Ratcliff Date: Tue, 17 Sep 2019 13:54:55 -0700 Subject: [PATCH 1/2] fixed help bug, and added support for semver validation in the cli --- cmds/versions/create.js | 7 ++++--- cmds/versions/delete.js | 7 ++++--- cmds/versions/update.js | 7 ++++--- lib/prompts.js | 7 +++++++ package.json | 1 + test/cmds/versions.test.js | 6 +++--- test/lib/prompts.test.js | 5 +++++ 7 files changed, 28 insertions(+), 12 deletions(-) diff --git a/cmds/versions/create.js b/cmds/versions/create.js index 3b9c5af5d..401cdf240 100644 --- a/cmds/versions/create.js +++ b/cmds/versions/create.js @@ -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={project-version} [options]'; +exports.usage = 'versions:create --version= [options]'; exports.description = 'Create a new version for your project.'; exports.category = 'versions'; exports.position = 2; @@ -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.`, ), ); } diff --git a/cmds/versions/delete.js b/cmds/versions/delete.js index eddbe2bde..9b9adfa04 100644 --- a/cmds/versions/delete.js +++ b/cmds/versions/delete.js @@ -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= [options]'; exports.description = 'Delete a version associated with your ReadMe project.'; exports.category = 'versions'; exports.position = 4; @@ -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.`, ), ); } diff --git a/cmds/versions/update.js b/cmds/versions/update.js index 7fefddfb3..d9a215c02 100644 --- a/cmds/versions/update.js +++ b/cmds/versions/update.js @@ -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= [options]'; exports.description = 'Update an existing version for your project.'; exports.category = 'versions'; exports.position = 3; @@ -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.`, ), ); } diff --git a/lib/prompts.js b/lib/prompts.js index bcd47051a..79fd3f9a9 100644 --- a/lib/prompts.js +++ b/lib/prompts.js @@ -1,3 +1,5 @@ +const semver = require('semver'); + exports.generatePrompts = versionList => [ { type: 'select', @@ -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 as your value.'); + }, }, { type: 'confirm', diff --git a/package.json b/package.json index 416d8e905..767e698bd 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/cmds/versions.test.js b/test/cmds/versions.test.js index 5d7ec2603..97a32f748 100644 --- a/test/cmds/versions.test.js +++ b/test/cmds/versions.test.js @@ -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.', ); }); }); @@ -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.', ); }); }); @@ -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.', ); }); }); diff --git a/test/lib/prompts.test.js b/test/lib/prompts.test.js index 7ecdc8b8c..f6f1cfe30 100644 --- a/test/lib/prompts.test.js +++ b/test/lib/prompts.test.js @@ -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), From fba44326ca7d707f0bee2dd078330704cf2d8f6a Mon Sep 17 00:00:00 2001 From: Gabriel Ratcliff Date: Tue, 17 Sep 2019 14:00:56 -0700 Subject: [PATCH 2/2] Update lib/prompts.js Co-Authored-By: Jon Ursenbach --- lib/prompts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/prompts.js b/lib/prompts.js index 79fd3f9a9..05b685e2f 100644 --- a/lib/prompts.js +++ b/lib/prompts.js @@ -62,7 +62,7 @@ exports.createVersionPrompt = (versionList, opts, isUpdate) => [ validate(val) { return semver.valid(semver.coerce(val)) ? true - : this.styles.danger('Please specify a semantic version as your value.'); + : this.styles.danger('Please specify a semantic version.'); }, }, {