UNPKG

3.18 kBJavaScriptView Raw
1var async = require('async');
2var fs = require('fs-extra');
3var path = require('path');
4var versions = require('mongodb-version-list');
5var semver = require('semver');
6var defaults = require('lodash.defaults');
7var config = require('./lib/config');
8var activate = require('./lib/activate');
9var download = require('./lib/download');
10var Model = require('./lib/model');
11var resolve = require('./lib/resolve');
12
13// var VERSION = /[0-9]+\.[0-9]+\.[0-9]+([-_\.][a-zA-Z0-9]+)?/;
14
15activate.addToPath(config.CURRENT_BIN_DIRECTORY);
16
17module.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
31exports.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
39var getVersion = require('get-mongodb-version');
40exports.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};
48exports.config = config;
49exports.path = function(fn) {
50 fn(null, config.CURRENT_BIN_DIRECTORY);
51};
52
53exports.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
62exports.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
91exports.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
99exports.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
109exports.use = function(opts, done) {
110 /* eslint no-shadow:0 */
111 resolve(opts, function(err, model) {
112 if (err) return done(err);
113
114 // @todo (imlucas) Current needs to take into account
115 // enterprise, debug, platform, bits, etc.
116 // exports.current(function(err, v) {
117 // if (err) return done(err);
118 //
119 // if (model.version === v) {
120 // debug('already using ' + v);
121 // return done(null, model);
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};