From a379ba74b0c520dcfa90315e16faaf7b3341decd Mon Sep 17 00:00:00 2001 From: Gabriel Ratcliff Date: Wed, 17 Jul 2019 09:59:52 -0700 Subject: [PATCH] Migrated versionId into main version command, cleaned error handling, fixed fork parseint --- README.md | 2 +- lib/versions/create.js | 17 ++++++++-------- lib/versions/delete.js | 14 ++++++------- lib/versions/index.js | 24 +++++++++++++--------- lib/versions/update.js | 21 +++++++++++-------- lib/versions/versionId.js | 36 -------------------------------- rdme.js | 2 +- test/versions.test.js | 43 +++++---------------------------------- 8 files changed, 48 insertions(+), 111 deletions(-) delete mode 100644 lib/versions/versionId.js diff --git a/README.md b/README.md index 603f27cf9..e13157caa 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ rdme versions --key={api-key} #### Get all information about a particular version ```sh -rdme versions:versionId --key={api-key} --version={project-version} +rdme versions --key={api-key} --version={project-version} ``` #### Create a new version using flags diff --git a/lib/versions/create.js b/lib/versions/create.js index 9b19d2ee5..225e5a5a0 100644 --- a/lib/versions/create.js +++ b/lib/versions/create.js @@ -31,7 +31,7 @@ exports.run = async function({ opts }) { .catch(err => Promise.reject(new Error(err))); } - const promptResponse = await prompt(promptOpts.createVersionPrompt(versionList, opts)); + const promptResponse = await prompt(promptOpts.createVersionPrompt(versionList || [{}], opts)); const options = { json: { version, @@ -48,13 +48,12 @@ exports.run = async function({ opts }) { .post(`${config.host}/api/v1/version`, options) .then(() => Promise.resolve(`Version ${version} created successfully`)) .catch(err => { - let errorDesc; - try { - errorDesc = - typeof err.error === 'string' ? JSON.parse(err.error).description : err.error.description; - } catch (e) { - errorDesc = 'Failed to create a new version using your specified parameters.'; - } - return Promise.reject(new Error(errorDesc)); + return Promise.reject( + new Error( + err.error && err.error.description + ? err.error.description + : 'Failed to create a new version using your specified parameters.', + ), + ); }); }; diff --git a/lib/versions/delete.js b/lib/versions/delete.js index 0888c9bba..31e009b81 100644 --- a/lib/versions/delete.js +++ b/lib/versions/delete.js @@ -25,12 +25,12 @@ exports.run = async function({ opts }) { }) .then(() => Promise.resolve(`Version ${version} deleted successfully`)) .catch(err => { - let errorDesc; - try { - errorDesc = JSON.parse(err.error).description; - } catch (e) { - errorDesc = 'Failed to delete target version.'; - } - return Promise.reject(new Error(errorDesc)); + return Promise.reject( + new Error( + err.error && err.error.description + ? err.error.description + : 'Failed to delete target version.', + ), + ); }); }; diff --git a/lib/versions/index.js b/lib/versions/index.js index 19ef67ea0..8a461329e 100644 --- a/lib/versions/index.js +++ b/lib/versions/index.js @@ -1,30 +1,34 @@ const request = require('request-promise-native'); const config = require('config'); -exports.desc = 'List versions available in your project'; +exports.desc = 'List versions available in your project or get version by semver'; exports.category = 'services'; exports.weight = 3; exports.action = 'versions'; exports.run = function({ opts }) { - const { key } = opts; + const { key, version } = opts; if (!key) { return Promise.reject(new Error('No api key provided. Please use --key')); } + const uri = version + ? `${config.host}/api/v1/version/${version}` + : `${config.host}/api/v1/version`; + return request - .get(`${config.host}/api/v1/version`, { + .get(uri, { json: true, auth: { user: key }, }) .catch(err => { - let errorDesc; - try { - errorDesc = JSON.parse(err.error).description; - } catch (e) { - errorDesc = 'Failed to get versions attached to the provided key.'; - } - return Promise.reject(new Error(errorDesc)); + return Promise.reject( + new Error( + err.error && err.error.description + ? err.error.description + : 'Failed to get version(s) attached to the provided key.', + ), + ); }); }; diff --git a/lib/versions/update.js b/lib/versions/update.js index 0b3deee42..ab9d29acf 100644 --- a/lib/versions/update.js +++ b/lib/versions/update.js @@ -44,13 +44,16 @@ exports.run = async function({ opts }) { auth: { user: key }, }; - return request.put(`${config.host}/api/v1/version/${version}`, options).catch(err => { - let errorDesc; - try { - errorDesc = JSON.parse(err.error).description; - } catch (e) { - errorDesc = 'Failed to update version using your specified parameters.'; - } - return Promise.reject(new Error(errorDesc)); - }); + return request + .put(`${config.host}/api/v1/version/${version}`, options) + .then(() => Promise.resolve(`Version ${version} updated successfully`)) + .catch(err => { + return Promise.reject( + new Error( + err.error && err.error.description + ? err.error.description + : 'Failed to update version using your specified parameters.', + ), + ); + }); }; diff --git a/lib/versions/versionId.js b/lib/versions/versionId.js deleted file mode 100644 index 4371f9b46..000000000 --- a/lib/versions/versionId.js +++ /dev/null @@ -1,36 +0,0 @@ -const request = require('request-promise-native'); -const config = require('config'); - -exports.desc = 'Get a specific version from your project'; -exports.category = 'services'; -exports.weight = 4; -exports.action = 'versions:versionId'; - -exports.run = function({ opts }) { - const { key, version } = opts; - - 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 - .get(`${config.host}/api/v1/version/${version}`, { - json: true, - auth: { user: key }, - }) - .catch(err => { - let errorDesc; - try { - errorDesc = JSON.parse(err.error).description; - } catch (e) { - errorDesc = 'Failed to get specific version using provided identifier and key.'; - } - return Promise.reject(new Error(errorDesc)); - }); -}; diff --git a/rdme.js b/rdme.js index 00fab7fcb..7a3d6525c 100755 --- a/rdme.js +++ b/rdme.js @@ -2,7 +2,7 @@ require('colors'); const parseArgs = require('minimist')(process.argv.slice(2), { - string: 'version', + string: ['version', 'fork'], alias: { // Allows --version, -v, -V v: 'version', diff --git a/test/versions.test.js b/test/versions.test.js index 8a3a1485c..6b2fd4033 100644 --- a/test/versions.test.js +++ b/test/versions.test.js @@ -7,7 +7,6 @@ const versions = require('../cli').bind(null, 'versions'); const createVersion = require('../cli').bind(null, 'versions:create'); const deleteVersion = require('../cli').bind(null, 'versions:delete'); const updateVersion = require('../cli').bind(null, 'versions:update'); -const versionById = require('../cli').bind(null, 'versions:versionId'); jest.mock('../lib/prompts'); const key = 'Xmw4bGctRVIQz7R7dQXqH9nQe5d0SPQs'; @@ -35,56 +34,24 @@ describe('Versions CLI Commands', () => { mockRequest.done(); }); - it('should catch any request errors', async () => { - const mockRequest = nock(config.host) - .get('/api/v1/version') - .basicAuth({ user: key }) - .reply(400); - - await versions([], { key }).catch(err => { - assert.equal(err.message, 'Failed to get versions attached to the provided key.'); - }); - mockRequest.done(); - }); - }); - - describe('get version by id', () => { - it('should error if no api key provided', () => { - versionById([], {}).catch(err => { - assert.equal(err.message, 'No api key provided. Please use --key'); - }); - }); - - it('should error if no version provided', () => { - versionById([], { key }).catch(err => { - assert.equal( - err.message, - 'No version provided. Please specify a semantic version using --version', - ); - }); - }); - - it('should get a specific version object', async () => { + it('should get a specific version object if version flag provided', async () => { const mockRequest = nock(config.host) .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); - await versionById([], { key, version }); + await versions([], { key, version }); mockRequest.done(); }); it('should catch any request errors', async () => { const mockRequest = nock(config.host) - .get(`/api/v1/version/${version}`) + .get('/api/v1/version') .basicAuth({ user: key }) .reply(400); - await versionById([], { key, version }).catch(err => { - assert.equal( - err.message, - 'Failed to get specific version using provided identifier and key.', - ); + await versions([], { key }).catch(err => { + assert.equal(err.message, 'Failed to get version(s) attached to the provided key.'); }); mockRequest.done(); });