1 | 'use strict';
|
2 |
|
3 | var _keys = require('babel-runtime/core-js/object/keys');
|
4 |
|
5 | var _keys2 = _interopRequireDefault(_keys);
|
6 |
|
7 | var _extends2 = require('babel-runtime/helpers/extends');
|
8 |
|
9 | var _extends3 = _interopRequireDefault(_extends2);
|
10 |
|
11 | var _promise = require('babel-runtime/core-js/promise');
|
12 |
|
13 | var _promise2 = _interopRequireDefault(_promise);
|
14 |
|
15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
16 |
|
17 | var depcheck = require('depcheck');
|
18 | var ora = require('ora');
|
19 | var _ = require('lodash');
|
20 |
|
21 | var _require = require('rc-config-loader'),
|
22 | rcFile = _require.rcFile;
|
23 |
|
24 | function skipUnused(currentState) {
|
25 | return currentState.get('skipUnused') ||
|
26 | currentState.get('global') ||
|
27 | currentState.get('update') ||
|
28 | !currentState.get('cwdPackageJson').name;
|
29 | }
|
30 |
|
31 | function loadRcFile(rcFileName) {
|
32 | try {
|
33 | var results = rcFile(rcFileName);
|
34 |
|
35 | if (!results) {
|
36 | return {};
|
37 | }
|
38 | return results.config;
|
39 | } catch (error) {
|
40 | console.error('Error parsing rc file; skipping it; error: ' + error.message);
|
41 | return {};
|
42 | }
|
43 | }
|
44 |
|
45 | function getSpecialParsers(currentState) {
|
46 | var specialsInput = currentState.get('specials');
|
47 | if (!specialsInput) return;
|
48 | return specialsInput.split(',').map(function (special) {
|
49 | return depcheck.special[special];
|
50 | }).filter(Boolean);
|
51 | }
|
52 |
|
53 | function checkUnused(currentState) {
|
54 | var spinner = ora('Checking for unused packages. --skip-unused if you don\'t want this.');
|
55 | spinner.enabled = spinner.enabled && currentState.get('spinner');
|
56 | spinner.start();
|
57 |
|
58 | return new _promise2.default(function (resolve) {
|
59 | if (skipUnused(currentState)) {
|
60 | resolve(currentState);
|
61 | return;
|
62 | }
|
63 |
|
64 | var depcheckDefaults = {
|
65 | ignoreDirs: ['sandbox', 'dist', 'generated', '.generated', 'build', 'fixtures', 'jspm_packages'],
|
66 | ignoreMatches: ['gulp-*', 'grunt-*', 'karma-*', 'angular-*', 'babel-*', 'metalsmith-*', 'eslint-plugin-*', '@types/*', 'grunt', 'mocha', 'ava'],
|
67 | specials: getSpecialParsers(currentState)
|
68 | };
|
69 |
|
70 | var npmCheckRc = loadRcFile('npmcheck');
|
71 |
|
72 | var depcheckOptions = (0, _extends3.default)({}, depcheckDefaults, npmCheckRc.depcheck);
|
73 |
|
74 | depcheck(currentState.get('cwd'), depcheckOptions, resolve);
|
75 | }).then(function (depCheckResults) {
|
76 | spinner.stop();
|
77 | var unusedDependencies = [].concat(depCheckResults.dependencies, depCheckResults.devDependencies);
|
78 | currentState.set('unusedDependencies', unusedDependencies);
|
79 |
|
80 | var cwdPackageJson = currentState.get('cwdPackageJson');
|
81 |
|
82 |
|
83 | var missingFromPackageJson = _.omit(depCheckResults.missing || {}, (0, _keys2.default)(cwdPackageJson.dependencies), (0, _keys2.default)(cwdPackageJson.devDependencies));
|
84 | currentState.set('missingFromPackageJson', missingFromPackageJson);
|
85 | return currentState;
|
86 | });
|
87 | }
|
88 |
|
89 | module.exports = checkUnused; |
\ | No newline at end of file |