UNPKG

3.2 kBJavaScriptView Raw
1'use strict';
2
3var _keys = require('babel-runtime/core-js/object/keys');
4
5var _keys2 = _interopRequireDefault(_keys);
6
7var _extends2 = require('babel-runtime/helpers/extends');
8
9var _extends3 = _interopRequireDefault(_extends2);
10
11var _promise = require('babel-runtime/core-js/promise');
12
13var _promise2 = _interopRequireDefault(_promise);
14
15function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
17var depcheck = require('depcheck');
18var ora = require('ora');
19var _ = require('lodash');
20
21var _require = require('rc-config-loader'),
22 rcFile = _require.rcFile;
23
24function skipUnused(currentState) {
25 return currentState.get('skipUnused') || // manual option to ignore this
26 currentState.get('global') || // global modules
27 currentState.get('update') || // in the process of doing an update
28 !currentState.get('cwdPackageJson').name; // there's no package.json
29}
30
31function loadRcFile(rcFileName) {
32 try {
33 var results = rcFile(rcFileName);
34 // Not Found
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 {}; // default value
42 }
43}
44
45function 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
53function 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 // currently missing will return devDependencies that aren't really missing
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
89module.exports = checkUnused;
\No newline at end of file