/
fetch-info.js
76 lines (51 loc) · 1.74 KB
/
fetch-info.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module.exports = {
friendlyName: 'Fetch info',
description: 'Look up information about the latest version of the specified NPM package.',
extendedDescription: 'This fetches the package.json string for the specified package directly from the NPM registry.',
cacheable: true,
inputs: {
packageName: {
description: 'The unique name of the NPM package.',
example: 'browserify',
required: true
}
},
exits: {
success: {
outputFriendlyName: 'NPM package info',
outputDescription: 'Information about the specified NPM package, pulled directly from its package.json info in the NPM registry.',
example: {}
},
packageNotFound: {
description: 'No package exists on the public NPM registry with the specified name.'
},
couldNotParse: {
description: 'The package.json string for the specified package could not be parsed as JSON.'
}
},
fn: function (inputs,exits) {
var util = require('util');
var _ = require('lodash');
var Http = require('machinepack-http');
if (inputs.packageName.match(/^@/)){
return exits.error(new Error('This machine does not currently support scoped packages (e.g. @mikermcneil/foobar)'));
}
Http.sendHttpRequest({
baseUrl: 'http://registry.npmjs.org',
url: util.format('/%s', inputs.packageName)
}).exec({
error: function (err) {
return exits.error(err);
},
notFound: function (response) {
return exits.packageNotFound(response);
},
success: function (response) {
var parsed;
try { parsed = JSON.parse(response.body); }
catch (e) { return exits.couldNotParse(e); }
return exits.success(parsed);
}
});
}
};