UNPKG

1.59 kBJavaScriptView Raw
1/*
2 * matchdep
3 * https://github.com/tkellen/node-matchdep
4 *
5 * Copyright (c) 2012 Tyler Kellen
6 * Licensed under the MIT license.
7 */
8
9'use strict';
10
11var minimatch = require('minimatch');
12var path = require('path');
13
14// export object
15var matchdep = module.exports = {};
16
17// Ensure configuration has devDep and dependencies keys
18function loadConfig(config, props) {
19 // if no config defined, assume package.json in cwd
20 if (config == null) {
21 config = path.resolve(process.cwd(), 'package.json');
22 }
23
24 // if package is a string, try to require it
25 if (typeof config === 'string') {
26 config = require(config);
27 }
28
29 // if config is not an object yet, something is amiss
30 if (typeof config !== 'object') {
31 throw new Error('Invalid configuration specified.');
32 }
33
34 // For all specified props, populate result object.
35 var result = {};
36 props.forEach(function(prop) {
37 result[prop] = config[prop] ? Object.keys(config[prop]) : [];
38 });
39 return result;
40}
41
42// What config properties should each method search?
43var methods = {
44 filter: ['dependencies'],
45 filterDev: ['devDependencies'],
46 filterPeer: ['peerDependencies'],
47 filterAll: ['dependencies', 'devDependencies', 'peerDependencies'],
48};
49
50// Dynamically generate methods.
51Object.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});