This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
137 lines (116 loc) · 3.22 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var async = require('async');
var fs = require('fs-extra');
var path = require('path');
var versions = require('mongodb-version-list');
var semver = require('semver');
var defaults = require('lodash.defaults');
var config = require('./lib/config');
var activate = require('./lib/activate');
var download = require('./lib/download');
var Model = require('./lib/model');
var resolve = require('./lib/resolve');
// var VERSION = /[0-9]+\.[0-9]+\.[0-9]+([-_\.][a-zA-Z0-9]+)?/;
activate.addToPath(config.CURRENT_BIN_DIRECTORY);
module.exports = exports = function(opts, fn) {
if (typeof opts === 'function') {
fn = opts;
opts = {};
} else if (typeof opts === 'string') {
opts = {
version: opts
};
}
opts.version = opts.version || process.env.MONGODB_VERSION;
exports.use(opts, fn);
};
exports.resolve = function(opts, done) {
resolve(opts, function(err, res) {
if (err) return done(err);
done(null, new Model(res));
});
};
var getVersion = require('get-mongodb-version');
exports.current = function(done) {
var mongod = path.join(config.CURRENT_BIN_DIRECTORY, 'mongod');
fs.exists(mongod, function(exists) {
if (!exists) return done(null, null);
getVersion({path: mongod}, done);
});
};
exports.config = config;
exports.path = function(fn) {
fn(null, config.CURRENT_BIN_DIRECTORY);
};
exports.installed = function(fn) {
fs.readdir(config.ROOT_DIRECTORY, function(err, files) {
files = files || [];
if (err) return fn(null, files);
fn(null, files);
});
};
exports.available = function(opts, fn) {
opts = defaults(opts || {}, {
stable: false,
unstable: false,
rc: false,
range: undefined
});
versions(function(err, res) {
if (err) return fn(err);
res = res.map(function(v) {
return semver.parse(v);
})
.filter(function(v) {
if (opts.range) {
return semver.satisfies(v.version, opts.range);
}
v.stable = v.minor % 2 === 0;
v.unstable = !v.stable;
v.rc = v.prerelease.length > 0;
if (!opts.rc && v.rc) return false;
if (!opts.stable && v.stable) return false;
if (!opts.unstable && v.unstable) return false;
return true;
})
.map(function(v) {
return v.version;
});
fn(null, res);
});
};
exports.is = function(s, done) {
exports.current(function(err, v) {
if (err) return done(err);
done(null, semver.satisfies(v, s));
});
};
exports.install = function(version, done) {
resolve({
version: version
}, function(err, model) {
if (err) return done(err);
download(model, done);
});
};
exports.use = function(opts, done) {
/* eslint no-shadow:0 */
resolve(opts, function(err, model) {
if (err) return done(err);
// @todo (imlucas) Current needs to take into account
// enterprise, debug, platform, bits, etc.
// exports.current(function(err, v) {
// if (err) return done(err);
//
// if (model.version === v) {
// debug('already using ' + v);
// return done(null, model);
// }
async.series([
download.bind(null, model),
activate.bind(null, model)
], function(err) {
if (err) return done(err);
return done(null, model);
});
});
};