-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (75 loc) · 2.57 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
var through = require('through2'),
gutil = require('gulp-util'),
_ = require('lodash'),
PluginError = gutil.PluginError;
// consts
const PLUGIN_NAME = 'gulp-ng';
// plugin to add Angular registrations in TypeScript files.
function gulpNg() {
var config = {
className: '',
typeName: '',
params: []
};
// creating a stream through which each file will pass
var stream = through.obj(function(file, enc, cb) {
if (file.isStream()) {
return this.emit('error', new PluginError('gulp-ng-transform', 'Streaming not supported'));
}
if (file.isBuffer()) {
var contents = file.contents.toString();
// Get the class name.
var className = /export class (.*) (?=extends)/.exec(contents);
if (className) {
config.className = className[1];
}
else {
className = /export class (.*) (?=implements)/.exec(contents);
if (className) {
config.className = className[1];
}
}
// console.log(config.className);
// Get the type of class, e.g. Controller, Service, Directive
var typeName = /implements I(.*)/.exec(contents);
if (typeName) {
config.typeName = typeName[1];
// console.log(config.typeName);
}
// The following handles the contructor parameters.
var params = /constructor\([^.]*\)/m.exec(contents);
var paramsArray = params[0].split('\n');
_.each(paramsArray, function (line) {
if (line.indexOf('constructor(') < 0 && line.indexOf(')') < 0) {
if (line.indexOf(':') > 0) {
var sub = line.substring(line.indexOf(':'));
line = line.replace(sub, '');
}
var entry = line
.replace('public', '')
.replace('private', '')
.replace(',', '')
// .replace(':any', '')
// .replace(': any', '')
.trim();
config.params.push(entry);
}
});
// console.log(params[0]);
// Create the register entry.
var register = "\n\napp.register" + config.typeName + "('" + config.className +
"', ['" + config.params.join('\', \'') + "']);";
var newContentString = contents + register;
// change the file contents
file.contents = new Buffer(newContentString);
}
// make sure the file goes through the next gulp plugin
this.push(file);
// tell the stream engine that we are done with this file
return cb();
});
// returning the file stream
return stream;
};
// exporting the plugin main function
module.exports = gulpNg;