-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathversion.js
78 lines (54 loc) · 1.87 KB
/
version.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
77
78
var fs = require('fs');
var exec = require('sync-exec');
var EOL = require('os').EOL;
Version = {}
Version.latest = {}
// Get the latest version of a given package
Version.getLatest = function(packageName) {
// Check to see if we've cached this result
if (Version.latest[packageName]) {
return Version.latest[packageName];
}
// Rather than reimplement Meteor's entire package database mechanism in order
// to get the latest versions, we simply call out to Meteor's command line.
var result = exec('meteor show ' + packageName);
if (result.status != 0) {
throw(result.stderr);
}
else {
var lines = result.stdout.split(EOL);
var idx = 0;
var latest = null;
// Find the list of recent versions
while (idx < lines.length && lines[idx++] !== 'Recent versions:') ;
// Get the last line in this list
while (idx < lines.length && lines[idx] !== '') {
latest = lines[idx];
idx++;
}
// If we have a line, get the version and test
if (latest !== null) {
latestVersion = latest.split(' ').filter(function(item) { return item != ''; })[0];
Version.latest[packageName] = latestVersion;
return latestVersion;
}
}
}
// Get the version of the given package that's actually used by the project
Version.getVersionUsed = function(packageName) {
// Cached the used version info if we haven't already done so
if (typeof Version.used === 'undefined') {
Version.used = {};
var versions = fs.readFileSync(".meteor/versions", { encoding: 'utf-8' });
var lines = versions.split(EOL);
lines.forEach(function(line) {
var splitString = line.split('@');
var version = (splitString.length > 1) ? splitString[1] : null;
var name = splitString[0];
if (version)
Version.used[name] = version
});
}
return Version.used[packageName];
}
module.exports = Version;