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..05b685e2f 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.'); + }, }, { type: 'confirm', diff --git a/package.json b/package.json index f8ce7a529..dd80787a6 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),