1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var minimatch = require('minimatch');
|
12 | var path = require('path');
|
13 |
|
14 |
|
15 | var matchdep = module.exports = {};
|
16 |
|
17 |
|
18 | function loadConfig(config, props) {
|
19 |
|
20 | if (config == null) {
|
21 | config = path.resolve(process.cwd(), 'package.json');
|
22 | }
|
23 |
|
24 |
|
25 | if (typeof config === 'string') {
|
26 | config = require(config);
|
27 | }
|
28 |
|
29 |
|
30 | if (typeof config !== 'object') {
|
31 | throw new Error('Invalid configuration specified.');
|
32 | }
|
33 |
|
34 |
|
35 | var result = {};
|
36 | props.forEach(function(prop) {
|
37 | result[prop] = config[prop] ? Object.keys(config[prop]) : [];
|
38 | });
|
39 | return result;
|
40 | }
|
41 |
|
42 |
|
43 | var methods = {
|
44 | filter: ['dependencies'],
|
45 | filterDev: ['devDependencies'],
|
46 | filterPeer: ['peerDependencies'],
|
47 | filterAll: ['dependencies', 'devDependencies', 'peerDependencies'],
|
48 | };
|
49 |
|
50 |
|
51 | Object.keys(methods).forEach(function(method) {
|
52 | var props = methods[method];
|
53 | matchdep[method] = function(pattern, config) {
|
54 | config = loadConfig(config, props);
|
55 | var search = props.reduce(function(result, prop) {
|
56 | return result.concat(config[prop]);
|
57 | }, []);
|
58 | return minimatch.match(search, pattern, {});
|
59 | };
|
60 | });
|