-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
81 lines (67 loc) · 2.28 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
var tsd = require('./lib/tsd');
var through = require('through2');
var gutil = require('gulp-util');
var path = require('path');
var debug = require('debug')('gulp-tsd');
module.exports = function (options, callback) {
'use strict';
var settings = [];
if (options) {
settings.push(options);
}
var logger = {
'log': function () {
var args = Array.prototype.slice.call(arguments),
printfString = Array.apply(null, Array(args.length)).map(function(){return '%o';}).join(' ');
args.unshift(printfString);
debug.apply(null, args);
}
};
var finishedWorkers = 0;
var promised = function (context, promise, callback) {
promise.done(function () {
if (settings.length === ++finishedWorkers) {
logger.log('finish');
return callback();
}
}, function (err) {
if (settings.length === ++finishedWorkers) {
context.emit('error', new gutil.PluginError('gulp-tsd', 'Failed command execution: ' + err.stack));
return callback();
}
});
};
function transform(file, encode, callback) {
/*jshint validthis:true */
if (file.isNull()) {
this.push(file);
return callback();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-tsd', 'Streaming not supported'));
return callback();
}
settings.push(require(path.resolve(file.path)));
callback();
}
function flush(callback) {
/*jshint validthis:true */
var self = this;
settings.forEach(function (setting) {
var command = setting.command;
var tsdCommand = tsd.getRunner(logger).commands[command];
if (typeof tsdCommand === 'undefined') {
self.emit(
'error',
new gutil.PluginError('gulp-tsd', '"' + command + '"' + ' command is not supported')
);
return callback();
}
return promised(self, tsdCommand(setting), callback);
});
}
if (callback) {
flush(callback);
}
return through.obj(transform, flush);
};