-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.js
191 lines (159 loc) · 5.09 KB
/
util.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
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
187
188
189
190
191
// copied from https://github.com/DaftMonk/generator-angular-fullstack
'use strict';
var path = require('path');
var fs = require('fs');
module.exports = {
rewrite: rewrite,
rewriteFile: rewriteFile,
appName: appName,
processDirectory: processDirectory,
insertModuleDep: insertModuleDep,
matchBetween: matchBetween
};
function insertModuleDep(fileStr, appName, moduleName) {
var fullModName = appName + '.' + moduleName;
// add reference to the module
fileStr += '\n' + fullModName + ' = ' +
'angular.module(\'' +
fullModName + '\', []);';
// match everything between `var {appName} = angular.module('{appName}', [` and `]`
var depArrayMatcher = new RegExp("var " + appName +
" = angular\\.module\\('" + appName +
"', \\[([\\s\\S]*?)\\]");
var m = depArrayMatcher.exec(fileStr);
if(m) {
// remove line breaks and tabs
var modStr = m[1].replace(/(?:\r\n|\r|\n)/g, '');
modStr = modStr.replace(/\s/g, '');
// add the module dep to the module array
var modArray = modStr.split(',');
if(modArray[0] === '') modArray = [];
modArray.push("'" + fullModName + "'");
modStr = "var " + appName + " = angular.module('" + appName +
"', [\n " + modArray.join(',\n ') + '\n]';
fileStr = fileStr.replace(depArrayMatcher, modStr);
}
return fileStr;
}
function rewriteFile (args) {
args.path = args.path || process.cwd();
var fullPath = path.join(args.path, args.file);
args.haystack = fs.readFileSync(fullPath, 'utf8');
var body = rewrite(args);
fs.writeFileSync(fullPath, body);
}
function escapeRegExp (str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}
function rewrite (args) {
// check if splicable is already in the body text
var re = new RegExp(args.splicable.map(function (line) {
return '\s*' + escapeRegExp(line);
}).join('\n'));
if (re.test(args.haystack)) {
return args.haystack;
}
var lines = args.haystack.split('\n');
var otherwiseLineIndex = -1;
lines.forEach(function (line, i) {
if (line.indexOf(args.needle) !== -1) {
otherwiseLineIndex = i;
}
});
if(otherwiseLineIndex === -1) return lines.join('\n');
var spaces = 0;
while (lines[otherwiseLineIndex].charAt(spaces) === ' ') {
spaces += 1;
}
var spaceStr = '';
while ((spaces -= 1) >= 0) {
spaceStr += ' ';
}
lines.splice(otherwiseLineIndex + 1, 0, args.splicable.map(function (line) {
return spaceStr + line;
}).join('\n'));
return lines.join('\n');
}
function appName (self) {
var counter = 0, suffix = self.options['app-suffix'];
// Have to check this because of generator bug #386
process.argv.forEach(function(val) {
if (val.indexOf('--app-suffix') > -1) {
counter++;
}
});
if (counter === 0 || (typeof suffix === 'boolean' && suffix)) {
suffix = 'App';
}
return suffix ? self._.classify(suffix) : '';
}
function filterFile (template) {
// Find matches for parans
var filterMatches = template.match(/\(([^)]+)\)/g);
var filters = [];
if(filterMatches) {
filterMatches.forEach(function(filter) {
filters.push(filter.replace('(', '').replace(')', ''));
template = template.replace(filter, '');
});
}
return { name: template, filters: filters };
}
function matchBetween(searchString, matchBegin, matchEnd) {
var ret = {
matches: [],
values: []
};
var r = RegExp(matchBegin + '([\\s\\S]*?)' + matchEnd, 'g');
var m;
while (m = r.exec(searchString)) {
//console.log(m);
ret.matches.push(m);
ret.values.push(m[0]);
}
return ret;
}
function templateIsUsable (self, filteredFile) {
var filters = self.config.get('filters');
var enabledFilters = [];
for(var key in filters) {
if(filters[key]) enabledFilters.push(key);
}
var matchedFilters = self._.intersection(filteredFile.filters, enabledFilters);
// check that all filters on file are matched
if(filteredFile.filters.length && matchedFilters.length !== filteredFile.filters.length) {
return false;
}
return true;
}
function processDirectory (self, source, destination) {
var root = self.isPathAbsolute(source) ? source : path.join(self.sourceRoot(), source);
var files = self.expandFiles('**', { dot: true, cwd: root });
var dest, src;
files.forEach(function(f) {
var filteredFile = filterFile(f);
if(self.name) {
filteredFile.name = filteredFile.name.replace('name', self.name);
}
var name = filteredFile.name;
var copy = false, stripped;
src = path.join(root, f);
dest = path.join(destination, name);
if(path.basename(dest).indexOf('_') === 0) {
stripped = path.basename(dest).replace(/^_/, '');
dest = path.join(path.dirname(dest), stripped);
}
if(path.basename(dest).indexOf('!') === 0) {
stripped = path.basename(dest).replace(/^!/, '');
dest = path.join(path.dirname(dest), stripped);
copy = true;
}
if(templateIsUsable(self, filteredFile)) {
if(copy) {
self.copy(src, dest);
} else {
self.template(src, dest);
}
}
});
}