forked from laxa1986/gulp-angular-embed-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (76 loc) · 2.75 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
var through = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var ProcessorEngine = require('./lib/ProcessorEngine');
var Angular1Processor = require('./lib/Angular1Processor');
var Angular2TypeScriptTemplateProcessor = require('./lib/Angular2TypeScriptProcessor');
var Angular3TypeScriptTemplateProcessor = require('./lib/Angular3TypeScriptProcessor');
var utils = require('./lib/utils');
const PLUGIN_NAME = 'gulp-angular-embed-template';
module.exports = function (options) {
options = options || {};
var sourceType = options.sourceType;
delete options.sourceType;
switch (sourceType) {
case 'ts':
options.processors = [new Angular1Processor(), new Angular2TypeScriptTemplateProcessor()];
break;
case 'js':
default:
options.processors = [new Angular1Processor(), new Angular3TypeScriptTemplateProcessor()];
}
var logger = options.logger = utils.createLogger();
if (options.debug !== true) {
logger.debug = function () {}
}
delete options.debug;
logger.warn = function(msg) {
gutil.log(
PLUGIN_NAME,
gutil.colors.yellow('[Warning]'),
gutil.colors.magenta(msg)
);
};
var skipFiles = options.skipFiles || function() {return false;};
delete options.skipFiles;
if (typeof skipFiles === 'function') {
/* OK */
} else if (skipFiles instanceof RegExp) {
var regexp = skipFiles;
skipFiles = function(file) {
return regexp.test(file.path);
}
} else {
logger.warn('"skipFiles" options should be either function or regexp, actual type is ' + typeof skipFiles);
skipFiles = function() {return false;}
}
var processorEngine = new ProcessorEngine();
processorEngine.init(options);
/**
* This function is 'through' callback, so it has predefined arguments
* @param {File} file file to analyse
* @param {String} enc encoding (unused)
* @param {Function} cb callback
*/
function transform(file, enc, cb) {
// ignore empty files
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
throw new PluginError(PLUGIN_NAME, 'Streaming not supported. particular file: ' + file.path);
}
logger.debug('\n\nfile.path: %s', file.path || 'fake');
if (skipFiles(file)) {
logger.info('skip file %s', file.path);
cb(null, file);
return;
}
var pipe = this;
processorEngine.process(file, cb, function onErr(msg) {
pipe.emit('error', new PluginError(PLUGIN_NAME, msg));
});
}
return through.obj(transform);
};