-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
104 lines (89 loc) · 2.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*!
* load-plugins <https://github.com/jonschlinkert/load-plugins>
*
* Copyright (c) 2014 Jon Schlinkert, contributors.
* Licensed under the MIT License
*/
'use strict';
var path = require('path');
var appname = require('app-name');
var resolve = require('resolve-dep');
var extend = require('extend-shallow');
var utils = require('./lib/utils');
var registry = require('./lib/registry')
/**
* Expose `plugins`
*/
module.exports = plugins;
/**
* Load plugins from `node_modules` or a local directory.
*
* ```js
* var plugins = require('load-plugins')('gulp-*');
* var jshint = plugins['gulp-jshint'];
* var mocha = plugins['gulp-mocha'];
* ```
*
* @param {String} `patterns` Glob pattern to use.
* @param {String} `options`
* @return {String}
*/
function plugins(patterns, options) {
var opts = extend({
strict: true,
camelize: true,
strip: registry.strip
}, options);
var files = resolve(patterns, opts);
return files.reduce(function (cache, fp) {
var key = rename(fp, opts);
cache[key] = req(fp, opts);
return cache;
}, {});
}
/**
* Default `require` function. Pass a custom function to
* `options.require` to override.
*
* @param {String} filepath
* @param {String} `options`
* @return {String}
*/
function req(filepath, options) {
if (options.require) {
return options.require(filepath, options);
}
var fp = path.resolve(filepath);
return require(fp);
}
/**
* Rename function. Pass a custom function to `options.name` to change behavior.
*
* - Detect if the path is in node_modules, if so use the name of the module.
* - If not, use the name of file.
* - If the file name is `index`, use the last directory segment.
*
* Of course, these are best guesses. If you use a wierd directory
* structure for your plugins, it's probably a good idea to use a
* custom renaming function.
*
* @param {String} `filepath`
* @return {String}
*/
function rename(filepath, options) {
var name;
filepath = utils.relative(filepath);
if (/node_modules/.test(filepath)) {
name = utils.segments(filepath.replace(/.*\/(?=node_modules)/,''), 1, 2);
} else {
name = utils.basename(filepath);
}
if (name === 'index') {
name = utils.segments(filepath, -2)[0];
}
var str = appname(name, options.strip);
if (options.camelize) {
return utils.camelize(str);
}
return str;
}