UNPKG

4.7 kBJavaScriptView Raw
1require('lazy-ass');
2var check = require('check-more-types');
3require('console.json');
4
5var Q = require('q');
6Q.longStackSupport = true;
7var check = require('check-types');
8var verify = check.verify;
9var depsOk = require('deps-ok');
10var _ = require('lodash');
11
12var nameVersionParser = require('./moduleName');
13var registry = require('./registry');
14var nextVersions = registry.nextVersions;
15var testVersions = require('./test-module-version').testModulesVersions;
16var runTest = require('./test-module-version').testPromise;
17var getDependenciesToCheck = require('./dependencies');
18var reportAvailable = require('./report-available');
19
20var boundConsoleLog = console.log.bind(console);
21
22// returns a promise
23function available(moduleName, options) {
24 var toCheck = getDependenciesToCheck(options, moduleName);
25 var nextVersionsPromise = nextVersions(options, toCheck);
26 nextVersionsPromise.then(function (info) {
27 toCheck = _.zipObject(toCheck);
28 return reportAvailable(info, toCheck, options);
29 }, function (error) {
30 console.error('Could not fetch available modules\n', error);
31 });
32}
33
34function checkDependenciesInstalled() {
35 var defer = Q.defer();
36 process.nextTick(function () {
37 if (depsOk(process.cwd())) {
38 defer.resolve();
39 } else {
40 var msg = 'Current installation is not complete. Please run `npm install` or `bower install` first';
41 defer.reject(new Error(msg));
42 }
43 });
44 return defer.promise;
45}
46
47function checkCurrentInstall(options) {
48 options = options || {};
49 var log = options.tldr ? _.noop : boundConsoleLog;
50 log('checking if the current state works');
51
52 return checkDependenciesInstalled()
53 .then(function () {
54 return runTest(options, options.testCommand)();
55 })
56 .then(function () {
57 console.log('> tests are passing at the start');
58 });
59}
60
61var isOnline = Q.denodeify(require('is-online'));
62
63// returns promise
64function checkAllUpdates(options) {
65 options = options || {};
66 var moduleName = options.names;
67 var checkLatestOnly = !!options.latest;
68 var checkCommand = options.testCommand;
69 if (checkCommand) {
70 verify.unemptyString(checkCommand, 'invalid test command ' + checkCommand);
71 }
72 var all = options.all;
73 if (all) {
74 checkLatestOnly = true;
75 console.log('will check only latest versions because testing all');
76 }
77
78 if (check.string(moduleName)) {
79 moduleName = [moduleName];
80 }
81 checkLatestOnly = !!checkLatestOnly;
82 if (checkCommand) {
83 check.verify.string(checkCommand, 'expected string test command');
84 }
85 var toCheck = getDependenciesToCheck(options, moduleName);
86 check.verify.array(toCheck, 'dependencies to check should be an array');
87
88 var testVersionsBound = testVersions.bind(null, {
89 modules: toCheck,
90 command: checkCommand,
91 all: all,
92 color: options.color,
93 keep: options.keep,
94 allowed: options.allow || options.allowed,
95 tldr: options.tldr,
96 type: options.type
97 });
98
99 return isOnline()
100 .then(function (online) {
101 if (!online) {
102 throw new Error('Need to be online to check new modules');
103 }
104 }).then(function () {
105 if (isSingleSpecificVersion(moduleName)) {
106 var nv = nameVersionParser(moduleName[0]);
107 console.log('checking only specific:', nv.name, nv.version);
108 var list = [{
109 name: nv.name,
110 versions: [nv.version]
111 }];
112 return testVersionsBound(list);
113 } else {
114 var nextVersionsPromise = nextVersions(options, toCheck, checkLatestOnly);
115 return nextVersionsPromise.then(testVersionsBound);
116 }
117 });
118
119}
120
121function isSingleSpecificVersion(moduleNames) {
122 if (!moduleNames) {
123 return false;
124 }
125 var name = moduleNames;
126 if (Array.isArray(moduleNames)) {
127 if (moduleNames.length !== 1) {
128 return false;
129 }
130 name = moduleNames[0];
131 }
132 check.verify.string(name, 'expected module name string, not ' +
133 JSON.stringify(name));
134 var parsed = nameVersionParser(name);
135 if (check.object(parsed)) {
136 return false;
137 }
138 return check.string(parsed.version);
139}
140
141module.exports = {
142 checkCurrentInstall: checkCurrentInstall,
143 checkAllUpdates: checkAllUpdates,
144 available: available
145};