1 | 'use strict';
|
2 |
|
3 | var readPackageJson = require('./read-package-json');
|
4 | var getLatestFromRegistry = require('./get-latest-from-registry');
|
5 | var findModulePath = require('./find-module-path');
|
6 | var _ = require('lodash');
|
7 | var semverDiff = require('semver-diff');
|
8 | var pathExists = require('path-exists');
|
9 | var path = require('path');
|
10 | var semver = require('semver');
|
11 | var minimatch = require('minimatch');
|
12 |
|
13 | function createPackageSummary(moduleName, currentState) {
|
14 | var cwdPackageJson = currentState.get('cwdPackageJson');
|
15 |
|
16 | var modulePath = findModulePath(moduleName, currentState);
|
17 | var packageIsInstalled = pathExists.sync(modulePath);
|
18 | var modulePackageJson = readPackageJson(path.join(modulePath, 'package.json'));
|
19 |
|
20 |
|
21 | var isPrivate = Boolean(modulePackageJson.private);
|
22 | if (isPrivate) {
|
23 | return false;
|
24 | }
|
25 |
|
26 |
|
27 | var packageJsonVersion = cwdPackageJson.dependencies[moduleName] || cwdPackageJson.devDependencies[moduleName] || currentState.get('globalPackages')[moduleName];
|
28 |
|
29 | if (packageJsonVersion && !semver.validRange(packageJsonVersion)) {
|
30 | return false;
|
31 | }
|
32 |
|
33 |
|
34 | var ignore = currentState.get('ignore');
|
35 | if (ignore) {
|
36 | var ignoreMatch = Array.isArray(ignore) ? ignore.some(function (ignoredModule) {
|
37 | return minimatch(moduleName, ignoredModule);
|
38 | }) : minimatch(moduleName, ignore);
|
39 | if (ignoreMatch) {
|
40 | return false;
|
41 | }
|
42 | }
|
43 |
|
44 | var unusedDependencies = currentState.get('unusedDependencies');
|
45 | var missingFromPackageJson = currentState.get('missingFromPackageJson');
|
46 |
|
47 | function foundIn(files) {
|
48 | if (!files) {
|
49 | return;
|
50 | }
|
51 |
|
52 | return 'Found in: ' + files.map(function (filepath) {
|
53 | return filepath.replace(currentState.get('cwd'), '');
|
54 | }).join(', ');
|
55 | }
|
56 |
|
57 | return getLatestFromRegistry(moduleName).then(function (fromRegistry) {
|
58 | var installedVersion = modulePackageJson.version;
|
59 |
|
60 | var latest = installedVersion && fromRegistry.latest && fromRegistry.next && semver.gt(installedVersion, fromRegistry.latest) ? fromRegistry.next : fromRegistry.latest;
|
61 | var versions = fromRegistry.versions || [];
|
62 |
|
63 | var versionWanted = semver.maxSatisfying(versions, packageJsonVersion);
|
64 |
|
65 | var versionToUse = installedVersion || versionWanted;
|
66 | var usingNonSemver = semver.valid(latest) && semver.lt(latest, '1.0.0-pre');
|
67 |
|
68 | var bump = semver.valid(latest) && semver.valid(versionToUse) && (usingNonSemver && semverDiff(versionToUse, latest) ? 'nonSemver' : semverDiff(versionToUse, latest));
|
69 |
|
70 | var unused = _.includes(unusedDependencies, moduleName);
|
71 |
|
72 | return {
|
73 |
|
74 | moduleName: moduleName,
|
75 | homepage: fromRegistry.homepage,
|
76 | regError: fromRegistry.error,
|
77 | pkgError: modulePackageJson.error,
|
78 |
|
79 |
|
80 | latest: latest,
|
81 | installed: versionToUse,
|
82 | isInstalled: packageIsInstalled,
|
83 | notInstalled: !packageIsInstalled,
|
84 | packageWanted: versionWanted,
|
85 | packageJson: packageJsonVersion,
|
86 |
|
87 |
|
88 | notInPackageJson: foundIn(missingFromPackageJson[moduleName]),
|
89 |
|
90 |
|
91 | devDependency: _.has(cwdPackageJson.devDependencies, moduleName),
|
92 | usedInScripts: _.findKey(cwdPackageJson.scripts, function (script) {
|
93 | return script.indexOf(moduleName) !== -1;
|
94 | }),
|
95 | mismatch: semver.validRange(packageJsonVersion) && semver.valid(versionToUse) && !semver.satisfies(versionToUse, packageJsonVersion),
|
96 | semverValid: semver.valid(versionToUse),
|
97 | easyUpgrade: semver.validRange(packageJsonVersion) && semver.valid(versionToUse) && semver.satisfies(latest, packageJsonVersion) && bump !== 'major',
|
98 | bump: bump,
|
99 |
|
100 | unused: unused
|
101 | };
|
102 | });
|
103 | }
|
104 |
|
105 | module.exports = createPackageSummary; |
\ | No newline at end of file |