-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathindex.js
286 lines (251 loc) · 6.29 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
'use strict';
var assert = require('assert');
var sinon = require('sinon');
var capture = require('capture-stream');
var path = require('path');
var gulpLoadPlugins = (function() {
var wrapInFunc = function(value) {
return function() {
return value;
};
};
var proxyquire = require('proxyquire').noCallThru();
return proxyquire('../', {
'gulp-foo': wrapInFunc({ name: 'foo' }),
'gulp-bar': wrapInFunc({ name: 'bar' }),
'bar': wrapInFunc({ name: 'bar' }),
'gulp-foo-bar': wrapInFunc({ name: 'foo-bar' }),
'jack-foo': wrapInFunc({ name: 'jack-foo' }),
'gulp-insert': {
'append': wrapInFunc({ name: 'insert.append' }),
'wrap': wrapInFunc({ name: 'insert.wrap' })
},
'gulp.baz': wrapInFunc({ name: 'baz' }),
'findup-sync': function() { return null; },
'@myco/gulp-test-plugin': wrapInFunc({ name: 'test' })
});
})();
describe('configuration', function() {
it('throws a nice error if no configuration is found', function() {
assert.throws(function() {
gulpLoadPlugins({
config: null
});
}, /Could not find dependencies. Do you have a package.json file in your project?/);
});
it("throws a nice error if there're repeated dependencies pattern in package.json ", function() {
assert.throws(function() {
gulpLoadPlugins({
pattern: [
'*',
'!gulp'
],
config: {
dependencies: {
'bar': '*',
'gulp-bar': '~0.0.12'
}
}
});
}, /Could not define the property "bar", you may have repeated dependencies in your package.json like "gulp-bar" and "bar"/);
});
});
// Contains common tests with and without lazy mode.
var commonTests = function(lazy) {
it('loads things in', function() {
var x = gulpLoadPlugins({
lazy: lazy,
config: {
dependencies: {
'gulp-foo': '1.0.0',
'gulp-bar': '*',
'gulp-insert': '*',
'gulp.baz': '*'
}
}
});
assert.deepEqual(x.foo(), {
name: 'foo'
});
assert.deepEqual(x.bar(), {
name: 'bar'
});
assert.deepEqual(x.baz(), {
name: 'baz'
});
assert.deepEqual(x.insert.wrap(), {
name: 'insert.wrap'
});
assert.deepEqual(x.insert.append(), {
name: 'insert.append'
});
});
it('can take a pattern override', function() {
var x = gulpLoadPlugins({
lazy: lazy,
pattern: 'jack-*',
replaceString: 'jack-',
config: {
dependencies: {
'jack-foo': '1.0.0',
'gulp-bar': '*'
}
}
});
assert.deepEqual(x.foo(), {
name: 'jack-foo'
});
assert(!x.bar);
});
it('allows camelizing to be turned off', function() {
var x = gulpLoadPlugins({
lazy: lazy,
camelize: false,
config: {
dependencies: {
'gulp-foo-bar': '*'
}
}
});
assert.deepEqual(x['foo-bar'](), {
name: 'foo-bar'
});
});
it('camelizes plugins name by default', function() {
var x = gulpLoadPlugins({
lazy: lazy,
config: {
dependencies: {
'gulp-foo-bar': '*'
}
}
});
assert.deepEqual(x.fooBar(), {
name: 'foo-bar'
});
});
it('lets something be completely renamed', function() {
var x = gulpLoadPlugins({
lazy: lazy,
config: { dependencies: { 'gulp-foo': '1.0.0' } },
rename: { 'gulp-foo': 'bar' }
});
assert.deepEqual(x.bar(), { name: 'foo' });
});
it('outputs debug statements', function() {
var restore = capture(process.stdout);
try {
var x = gulpLoadPlugins({
lazy: lazy,
DEBUG: true,
config: { dependencies: { 'gulp-foo': '*' } }
});
assert.deepEqual(x.foo(), {
name: 'foo'
});
} catch (err) {
restore();
throw err;
}
var output = restore('true');
assert(output.indexOf('gulp-load-plugins') !== -1, 'Expected output to be logged to stdout');
});
it('supports loading scopped package', function() {
var x = gulpLoadPlugins({
lazy: lazy,
config: { dependencies: { '@myco/gulp-test-plugin': '1.0.0' } }
});
assert.deepEqual(x.myco.testPlugin(), { name: 'test' });
});
it('supports custom rename functions', function () {
var x = gulpLoadPlugins({
renameFn: function () {
return 'baz';
},
config: {
dependencies: {
'gulp-foo-bar': '*'
}
}
});
assert.throws(function () {
x.fooBar();
});
assert.deepEqual(x.baz(), {
name: 'foo-bar'
});
});
it('supports transforming', function() {
var x = gulpLoadPlugins({
lazy: lazy,
config: { dependencies: { 'gulp-foo': '1.0.0' } },
postRequireTransforms: {
foo: function(foo) {
foo.bar = 'test string';
return foo;
}
}
});
assert.strictEqual(x.foo.bar, 'test string');
});
};
describe('no lazy loading', function() {
commonTests(false);
var spy;
before(function() {
spy = sinon.spy();
gulpLoadPlugins({
lazy: false,
config: {
dependencies: {
'gulp-insert': '*'
}
},
requireFn: function() {
spy();
return function() {};
}
});
});
it('does require at first', function() {
assert(spy.called);
});
});
describe('with lazy loading', function() {
commonTests(true);
var x, spy;
before(function() {
spy = sinon.spy();
x = gulpLoadPlugins({
lazy: true,
config: {
dependencies: {
'gulp-insert': '*'
}
},
requireFn: function() {
spy();
return function() {};
}
});
});
it('does not require at first', function() {
assert(!spy.called);
});
it('does when the property is accessed', function() {
x.insert();
assert(spy.called);
});
});
describe('common functionality', function () {
it('throws a sensible error when not found', function () {
var x = gulpLoadPlugins({ config: path.join(__dirname, '/package.json') });
assert.throws(function () {
x.oops();
}, /Cannot find module 'gulp-oops'/);
});
it('allows you to use in a lower directory', function() {
var plugins = require('../')();
assert.ok(typeof plugins.test === 'function');
});
});