forked from yagoferrer/gulp-angular-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (70 loc) · 2.02 KB
/
index.js
File metadata and controls
105 lines (70 loc) · 2.02 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
var through = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var File = gutil.File;
var path = require("path");
var stripComments = require("strip-comments");
// consts
const PLUGIN_NAME = 'gulp-angular-modules';
function angularModules(fileName, options)
{
var opts = options || {};
options.name = options.name || "gulp-angular-modules";
if (!fileName) {
throw new PluginError(PLUGIN_NAME, 'Missing Filename');
}
var list = [];
var resultFile = new File({
base: __dirname,
cwd: __dirname,
path: path.join(__dirname, fileName)
});
function getModuleNames(file, enc, cb){
if (file.isNull()) {
// do nothing if no contents
cb();
return false;
}
if (file.isStream()) {
// Support streams?
cb();
return false;
}
var contents = file.contents.toString();
var contents = stripComments(contents);
var re = new RegExp(/\.module\((["'])([a-zA-Z0-9_.-]*)/);
var matches = contents.match(re);
if (matches && list.indexOf(matches[2]) === -1) {
list.push(matches[2]);
}
cb();
}
function getContent()
{
if (options.modules)
{
list = list.concat(options.modules);
}
list.sort();
var result;
result = "(function (ng) {\n";
result += "'use strict';\n";
if ( list.length > 0 ) {
result += "ng.module('"+ options.name +"', ['"+ list.join("','") +"']);\n";
}
else {
result += "ng.module('"+ options.name +"', []);\n";
}
result += "})(angular);";
return result;
}
function sendBack(cb)
{
var content = getContent();
resultFile.contents = new Buffer(content);
this.push(resultFile);
cb();
}
return through.obj(getModuleNames, sendBack);
}
module.exports = angularModules;