UNPKG

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