Skip to content

Commit

Permalink
Migrated versionId into main version command, cleaned error handling,…
Browse files Browse the repository at this point in the history
… fixed fork parseint
  • Loading branch information
gratcliff committed Jul 17, 2019
1 parent d5997b9 commit a379ba7
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 111 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions lib/versions/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.',
),
);
});
};
14 changes: 7 additions & 7 deletions lib/versions/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
),
);
});
};
24 changes: 14 additions & 10 deletions lib/versions/index.js
Original file line number Diff line number Diff line change
@@ -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.',
),
);
});
};
21 changes: 12 additions & 9 deletions lib/versions/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
),
);
});
};
36 changes: 0 additions & 36 deletions lib/versions/versionId.js

This file was deleted.

2 changes: 1 addition & 1 deletion rdme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
43 changes: 5 additions & 38 deletions test/versions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
});
Expand Down

0 comments on commit a379ba7

Please sign in to comment.