forked from takeshi/gulp-typescript-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
186 lines (158 loc) · 5.2 KB
/
Copy pathindex.js
File metadata and controls
186 lines (158 loc) · 5.2 KB
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
'use strict';
var through = require('through2');
var falafel = require('falafel');
module.exports = function angularify(opts) {
opts = opts || {};
if(typeof opts.ignore === 'undefined'){
opts.ignore = /^\$/;
}
if(typeof opts.moduleName === 'undefined'){
opts.moduleName = '<Must set moduleName>';
}
if(typeof opts.firstLowerCase === 'undefined'){
opts.firstLowerCase = true;
}
if(typeof opts.patterns === 'undefined'){
var DEFAULT_PATTERNS = [
{pattern:/Controller$/,type:'controller',firstLowerCase:false},
{pattern:/Ctrl$/,type:'controller',firstLowerCase:false},
{pattern:/Service$/,type:'service'},
{pattern:/Manager$/,type:'service'},
{pattern:/ServiceDelegate$/,type:'service'},
{pattern:/Provider$/,type:'provider',removePattern:true},
{pattern:/Directive$/,type:'directive',removePattern:true,firstLowerCase:true}
];
opts.patterns = DEFAULT_PATTERNS;
}
if(typeof opts.decoratorPatterns === 'undefined'){
var DEFAULT_PATTERNS = [
{pattern:/Controller$/,func:'Controller',firstLowerCase:false},
{pattern:/Service$/,func:'Service'},
{pattern:/Provider$/,func:'Provider',removePattern:true},
{pattern:/Directive$/,func:'Directive',removePattern:true,firstLowerCase:true}
];
opts.decoratorPatterns = DEFAULT_PATTERNS;
}
// creating a stream through which each file will pass
var stream = through.obj(function(file, enc, cb) {
if (file.isNull()) {
// do nothing
}
if (file.isBuffer()) {
var contents = file.contents.toString();
contents = transform(contents,opts);
file.contents = new Buffer(contents.toString());
}
this.push(file);
return cb();
});
// returning the file stream
return stream;
};
function transform(contents,opts){
return falafel(contents, {tolerant: true}, function (node) {
findClassDeclaration(node,opts);
}).toString();
}
function findClassDeclaration(node,opts){
var decls, decl;
if (node.type === 'VariableDeclaration' &&
(decls = node.declarations) && decls.length === 1 &&
(decl = decls[0]) && decl.init && decl.init.type === 'CallExpression' ) {
if(opts.ignore && decl.id.name.match(opts.ignore)){
return;
}
if(!decl.init.callee.body){
return;
}
if(opts.decorator){
var decorators = findDecorator(decl);
decorators.forEach(function(decorator){
opts.decoratorPatterns.forEach(function(decoratorPattern){
if(decorator === opts.decoratorModuleName + '.' + decoratorPattern.func){
addAngularModule(node,decl,opts,decoratorPattern);
}
});
});
}
else{
opts.patterns.forEach(function(pattern){
if(decl.id.name.match(pattern.pattern)){
addAngularModule(node,decl,opts,pattern);
}
});
}
}
}
function findDecorator(decl){
var body = decl.init.callee.body;
var decoratorBlock = body.body[body.body.length-2];
var decorators = decoratorBlock.expression.right;
return decorators.arguments[0].elements.map(function(element){
return element.source();
})
}
function addAngularModule(node,decl,opts,ptn){
var moduleName = opts.moduleName,
type = ptn.type,
pattern = ptn.pattern,
removePattern = ptn.removePattern
var firstLowerCase = ptn.firstLowerCase;
if(typeof firstLowerCase === 'undefined'){
firstLowerCase = opts.firstLowerCase;
}
var constructor = decl.init.callee.body.body[0];
var constructorParams = constructor.params.map(function(param){
return '\''+param.name + '\'';
});
var className = decl.id.name;
var conponentName = className;
if(removePattern){
conponentName = decl.id.name.replace(pattern,'');
}
if(firstLowerCase){
conponentName = conponentName.toLowerCase()[0] + conponentName.substring(1);
}
add$inject(decl.init.callee.body,className,conponentName,constructor,constructorParams);
function add$inject(body,className,componentName,constructor,constructorParams){
if(!constructorParams){
return;
}
var source = '/*<auto_generate>*/';
source += className + '.$inject = ['+constructorParams.join('\,')+'];';
source += className + '.$componentName = \'' + componentName + '\'';
source += '/*</auto_generate>*/';
constructor.update(constructor.source() + source);
}
if(opts.decoratorModuleName){
return;
}
var source = '/*<auto_generate>*/';
if(type === 'directive'){
source += createModule();
}
else if(type === 'value'){
source += createModule();
}
else if(type === 'constant'){
source += createModule();
}
else {
source += functionModule()
}
source +='/*</auto_generate>*/';
node.update(node.source() + source);
function functionModule(){
var source = '';
source += 'angular.module(\''+ moduleName +'\')';
source += '.' + type + '(\''+conponentName+'\','+className+');';
return source;
}
function createModule(){
constructorParams.push('function(){return new ' +className +'(arguments);}');
var source = '';
source += 'angular.module(\''+ moduleName +'\')';
source += '.' + type + '(\''+conponentName+'\',['+constructorParams.join('\,')+']);';
return source;
}
}