forked from jaybekster/package-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (123 loc) · 4.5 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
138
'use strict';
var semver = require('semver'),
objectAssign = require('object-assign'),
textTable = require('text-table'),
chalk = require('chalk'),
version = require('./package.json').version,
CacheToFile = require('./lib/cache-to-file.js'),
readPackageJson = require('./lib/read-package-json'),
getCurrentNpmLs = require('./lib/get-current-npm-ls');
var textTableObj = {
header: [
['Name', 'Current', 'Wanted', 'Problem']
],
options: {
align: ['l', 'l', 'l', 'l']
}
};
var SETTINGS = {
path: process.cwd() + '/package.json',
directory: process.cwd(),
prod: true,
dev: true,
hashFile: false
};
var gitHubPublicRe = /^https:\/\/github.com\/([^\/])+\/([^\/]+\.git)#([0-9]+\.[0-9]+\.[0-9]+)$/;
/**
* comparePackages compare data from <npm ls> command and <package.json>
* @param {Object} options
* @return {Promise}
*/
function comparePackages(options) {
var output = {};
return Promise.all([
readPackageJson(options),
getCurrentNpmLs(options)
]).then(function(values) {
var packageName,
currentPackages = values[1],
actualPackages = values[0],
packageVersion,
tableBody = [],
packageJsonVersion;
for (packageName in actualPackages) {
output[packageName] = {};
output[packageName].actualVersion = actualPackages[packageName];
if (!currentPackages[packageName] || currentPackages[packageName].missing) {
tableBody.push([packageName, '', actualPackages[packageName], 'is missing']);
continue;
}
packageJsonVersion = actualPackages[packageName];
if (currentPackages[packageName].version) {
packageVersion = currentPackages[packageName].version;
} else if (currentPackages[packageName].required && currentPackages[packageName].required.version) {
packageVersion = currentPackages[packageName].required.version;
}
if (gitHubPublicRe.test(packageJsonVersion)) { // if public github url
packageJsonVersion = packageJsonVersion.match(gitHubPublicRe)[3];
}
if (semver.validRange(packageJsonVersion)
&& packageVersion !== packageJsonVersion
&& !semver.satisfies(packageVersion, packageJsonVersion)
) {
output[packageName].actualVersion = packageJsonVersion;
output[packageName].relevantVersion = packageVersion;
tableBody.push([packageName, packageVersion, packageJsonVersion, 'invalid version']);
}
}
return {
success: !tableBody.length,
output: output,
text: textTable([].concat(textTableObj.header, tableBody), textTableObj.options)
};
});
}
/**
* packageChecker itself
* @param {Object} options
* @param {Function} callback
*/
function packageChecker(options, callback) {
var callback = (typeof options === 'object' ? callback : options) || function() {},
options = objectAssign(SETTINGS, (typeof options === 'object' ? options : callback) || {});
if (typeof callback !== 'function') {
throw new Error('Invalid argument: callback');
}
Promise.resolve(
options.hashFile ? CacheToFile.checkHash(options.hashFile, options.path) : {
success: false
}
).then(function(response) {
if (!response.success) {
return comparePackages(options).then(function(compareResponse) {
return objectAssign(response, compareResponse);
});
}
return response;
}).then(function(response) {
if (options.hashFile && response.success) {
return response.cacheToFile.saveNewHash().then(function() {
return response;
});
}
return response;
}).then(function(response) {
if (response.success) {
console.log(chalk.green('No differences were found'));
callback(null, response);
} else {
if (response.text) {
console.log(response.text);
}
if (options.hashFile) {
response.cacheToFile.removeNewHash();
}
throw new Error('Differences were found');
}
}).catch(function(err) {
console.log(chalk.red(err));
callback(null, response);
});
}
packageChecker.version = version;
module.exports = packageChecker;