1 | var async = require('async');
|
2 | var fs = require('fs-extra');
|
3 | var path = require('path');
|
4 | var versions = require('mongodb-version-list');
|
5 | var semver = require('semver');
|
6 | var defaults = require('lodash.defaults');
|
7 | var config = require('./lib/config');
|
8 | var activate = require('./lib/activate');
|
9 | var download = require('./lib/download');
|
10 | var Model = require('./lib/model');
|
11 | var resolve = require('./lib/resolve');
|
12 |
|
13 |
|
14 |
|
15 | activate.addToPath(config.CURRENT_BIN_DIRECTORY);
|
16 |
|
17 | module.exports = exports = function(opts, fn) {
|
18 | if (typeof opts === 'function') {
|
19 | fn = opts;
|
20 | opts = {};
|
21 | } else if (typeof opts === 'string') {
|
22 | opts = {
|
23 | version: opts
|
24 | };
|
25 | }
|
26 | opts.version = opts.version || process.env.MONGODB_VERSION;
|
27 |
|
28 | exports.use(opts, fn);
|
29 | };
|
30 |
|
31 | exports.resolve = function(opts, done) {
|
32 | resolve(opts, function(err, res) {
|
33 | if (err) return done(err);
|
34 |
|
35 | done(null, new Model(res));
|
36 | });
|
37 | };
|
38 |
|
39 | var getVersion = require('get-mongodb-version');
|
40 | exports.current = function(done) {
|
41 | var mongod = path.join(config.CURRENT_BIN_DIRECTORY, 'mongod');
|
42 | fs.exists(mongod, function(exists) {
|
43 | if (!exists) return done(null, null);
|
44 |
|
45 | getVersion({path: mongod}, done);
|
46 | });
|
47 | };
|
48 | exports.config = config;
|
49 | exports.path = function(fn) {
|
50 | fn(null, config.CURRENT_BIN_DIRECTORY);
|
51 | };
|
52 |
|
53 | exports.installed = function(fn) {
|
54 | fs.readdir(config.ROOT_DIRECTORY, function(err, files) {
|
55 | files = files || [];
|
56 | if (err) return fn(null, files);
|
57 |
|
58 | fn(null, files);
|
59 | });
|
60 | };
|
61 |
|
62 | exports.available = function(opts, fn) {
|
63 | opts = defaults(opts || {}, {
|
64 | stable: false,
|
65 | unstable: false,
|
66 | rc: false
|
67 | });
|
68 |
|
69 | versions(function(err, res) {
|
70 | if (err) return fn(err);
|
71 | res = res.map(function(v) {
|
72 | return semver.parse(v);
|
73 | })
|
74 | .filter(function(v) {
|
75 | v.stable = v.minor % 2 === 0;
|
76 | v.unstable = !v.stable;
|
77 | v.rc = v.prerelease.length > 0;
|
78 |
|
79 | if (!opts.rc && v.rc) return false;
|
80 | if (!opts.stable && v.stable) return false;
|
81 | if (!opts.unstable && v.unstable) return false;
|
82 | return true;
|
83 | })
|
84 | .map(function(v) {
|
85 | return v.version;
|
86 | });
|
87 | fn(null, res);
|
88 | });
|
89 | };
|
90 |
|
91 | exports.is = function(s, done) {
|
92 | exports.current(function(err, v) {
|
93 | if (err) return done(err);
|
94 |
|
95 | done(null, semver.satisfies(v, s));
|
96 | });
|
97 | };
|
98 |
|
99 | exports.install = function(version, done) {
|
100 | resolve({
|
101 | version: version
|
102 | }, function(err, model) {
|
103 | if (err) return done(err);
|
104 |
|
105 | download(model, done);
|
106 | });
|
107 | };
|
108 |
|
109 | exports.use = function(opts, done) {
|
110 |
|
111 | resolve(opts, function(err, model) {
|
112 | if (err) return done(err);
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | async.series([
|
125 | download.bind(null, model),
|
126 | activate.bind(null, model)
|
127 | ], function(err) {
|
128 | if (err) return done(err);
|
129 | return done(null, model);
|
130 | });
|
131 | });
|
132 | };
|