Skip to content

Commit a379ba7

Browse files
committed
Migrated versionId into main version command, cleaned error handling, fixed fork parseint
1 parent d5997b9 commit a379ba7

File tree

8 files changed

+48
-111
lines changed

8 files changed

+48
-111
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ rdme versions --key={api-key}
9393

9494
#### Get all information about a particular version
9595
```sh
96-
rdme versions:versionId --key={api-key} --version={project-version}
96+
rdme versions --key={api-key} --version={project-version}
9797
```
9898

9999
#### Create a new version using flags

lib/versions/create.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ exports.run = async function({ opts }) {
3131
.catch(err => Promise.reject(new Error(err)));
3232
}
3333

34-
const promptResponse = await prompt(promptOpts.createVersionPrompt(versionList, opts));
34+
const promptResponse = await prompt(promptOpts.createVersionPrompt(versionList || [{}], opts));
3535
const options = {
3636
json: {
3737
version,
@@ -48,13 +48,12 @@ exports.run = async function({ opts }) {
4848
.post(`${config.host}/api/v1/version`, options)
4949
.then(() => Promise.resolve(`Version ${version} created successfully`))
5050
.catch(err => {
51-
let errorDesc;
52-
try {
53-
errorDesc =
54-
typeof err.error === 'string' ? JSON.parse(err.error).description : err.error.description;
55-
} catch (e) {
56-
errorDesc = 'Failed to create a new version using your specified parameters.';
57-
}
58-
return Promise.reject(new Error(errorDesc));
51+
return Promise.reject(
52+
new Error(
53+
err.error && err.error.description
54+
? err.error.description
55+
: 'Failed to create a new version using your specified parameters.',
56+
),
57+
);
5958
});
6059
};

lib/versions/delete.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ exports.run = async function({ opts }) {
2525
})
2626
.then(() => Promise.resolve(`Version ${version} deleted successfully`))
2727
.catch(err => {
28-
let errorDesc;
29-
try {
30-
errorDesc = JSON.parse(err.error).description;
31-
} catch (e) {
32-
errorDesc = 'Failed to delete target version.';
33-
}
34-
return Promise.reject(new Error(errorDesc));
28+
return Promise.reject(
29+
new Error(
30+
err.error && err.error.description
31+
? err.error.description
32+
: 'Failed to delete target version.',
33+
),
34+
);
3535
});
3636
};

lib/versions/index.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
const request = require('request-promise-native');
22
const config = require('config');
33

4-
exports.desc = 'List versions available in your project';
4+
exports.desc = 'List versions available in your project or get version by semver';
55
exports.category = 'services';
66
exports.weight = 3;
77
exports.action = 'versions';
88

99
exports.run = function({ opts }) {
10-
const { key } = opts;
10+
const { key, version } = opts;
1111

1212
if (!key) {
1313
return Promise.reject(new Error('No api key provided. Please use --key'));
1414
}
1515

16+
const uri = version
17+
? `${config.host}/api/v1/version/${version}`
18+
: `${config.host}/api/v1/version`;
19+
1620
return request
17-
.get(`${config.host}/api/v1/version`, {
21+
.get(uri, {
1822
json: true,
1923
auth: { user: key },
2024
})
2125
.catch(err => {
22-
let errorDesc;
23-
try {
24-
errorDesc = JSON.parse(err.error).description;
25-
} catch (e) {
26-
errorDesc = 'Failed to get versions attached to the provided key.';
27-
}
28-
return Promise.reject(new Error(errorDesc));
26+
return Promise.reject(
27+
new Error(
28+
err.error && err.error.description
29+
? err.error.description
30+
: 'Failed to get version(s) attached to the provided key.',
31+
),
32+
);
2933
});
3034
};

lib/versions/update.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ exports.run = async function({ opts }) {
4444
auth: { user: key },
4545
};
4646

47-
return request.put(`${config.host}/api/v1/version/${version}`, options).catch(err => {
48-
let errorDesc;
49-
try {
50-
errorDesc = JSON.parse(err.error).description;
51-
} catch (e) {
52-
errorDesc = 'Failed to update version using your specified parameters.';
53-
}
54-
return Promise.reject(new Error(errorDesc));
55-
});
47+
return request
48+
.put(`${config.host}/api/v1/version/${version}`, options)
49+
.then(() => Promise.resolve(`Version ${version} updated successfully`))
50+
.catch(err => {
51+
return Promise.reject(
52+
new Error(
53+
err.error && err.error.description
54+
? err.error.description
55+
: 'Failed to update version using your specified parameters.',
56+
),
57+
);
58+
});
5659
};

lib/versions/versionId.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

rdme.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require('colors');
33

44
const parseArgs = require('minimist')(process.argv.slice(2), {
5-
string: 'version',
5+
string: ['version', 'fork'],
66
alias: {
77
// Allows --version, -v, -V
88
v: 'version',

test/versions.test.js

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const versions = require('../cli').bind(null, 'versions');
77
const createVersion = require('../cli').bind(null, 'versions:create');
88
const deleteVersion = require('../cli').bind(null, 'versions:delete');
99
const updateVersion = require('../cli').bind(null, 'versions:update');
10-
const versionById = require('../cli').bind(null, 'versions:versionId');
1110

1211
jest.mock('../lib/prompts');
1312
const key = 'Xmw4bGctRVIQz7R7dQXqH9nQe5d0SPQs';
@@ -35,56 +34,24 @@ describe('Versions CLI Commands', () => {
3534
mockRequest.done();
3635
});
3736

38-
it('should catch any request errors', async () => {
39-
const mockRequest = nock(config.host)
40-
.get('/api/v1/version')
41-
.basicAuth({ user: key })
42-
.reply(400);
43-
44-
await versions([], { key }).catch(err => {
45-
assert.equal(err.message, 'Failed to get versions attached to the provided key.');
46-
});
47-
mockRequest.done();
48-
});
49-
});
50-
51-
describe('get version by id', () => {
52-
it('should error if no api key provided', () => {
53-
versionById([], {}).catch(err => {
54-
assert.equal(err.message, 'No api key provided. Please use --key');
55-
});
56-
});
57-
58-
it('should error if no version provided', () => {
59-
versionById([], { key }).catch(err => {
60-
assert.equal(
61-
err.message,
62-
'No version provided. Please specify a semantic version using --version',
63-
);
64-
});
65-
});
66-
67-
it('should get a specific version object', async () => {
37+
it('should get a specific version object if version flag provided', async () => {
6838
const mockRequest = nock(config.host)
6939
.get(`/api/v1/version/${version}`)
7040
.basicAuth({ user: key })
7141
.reply(200, { version });
7242

73-
await versionById([], { key, version });
43+
await versions([], { key, version });
7444
mockRequest.done();
7545
});
7646

7747
it('should catch any request errors', async () => {
7848
const mockRequest = nock(config.host)
79-
.get(`/api/v1/version/${version}`)
49+
.get('/api/v1/version')
8050
.basicAuth({ user: key })
8151
.reply(400);
8252

83-
await versionById([], { key, version }).catch(err => {
84-
assert.equal(
85-
err.message,
86-
'Failed to get specific version using provided identifier and key.',
87-
);
53+
await versions([], { key }).catch(err => {
54+
assert.equal(err.message, 'Failed to get version(s) attached to the provided key.');
8855
});
8956
mockRequest.done();
9057
});

0 commit comments

Comments
 (0)