forked from angular/material
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulp-utils.js
More file actions
308 lines (275 loc) · 9.49 KB
/
gulp-utils.js
File metadata and controls
308 lines (275 loc) · 9.49 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
var gulp = require('gulp');
var filter = require('gulp-filter');
var through2 = require('through2');
var lazypipe = require('lazypipe');
var gutil = require('gulp-util');
var Buffer = require('buffer').Buffer;
var fs = require('fs');
var path = require('path');
var findModule = require('../config/ngModuleData.js');
exports.humanizeCamelCase = function(str) {
switch (str) {
case 'fabSpeedDial':
return 'FAB Speed Dial';
case 'fabToolbar':
return 'FAB Toolbar';
default:
return str.charAt(0).toUpperCase() + str.substring(1).replace(/[A-Z]/g, function($1) {
return ' ' + $1.toUpperCase();
});
}
};
/**
* Copy all the demo assets to the dist directory
* NOTE: this excludes the modules demo .js,.css, .html files
*/
exports.copyDemoAssets = function(component, srcDir, distDir) {
gulp.src(srcDir + component + '/demo*/')
.pipe(through2.obj( copyAssetsFor ));
function copyAssetsFor( demo, enc, next){
var demoID = component + "/" + path.basename(demo.path);
var demoDir = demo.path + "/**/*";
var notJS = '!' + demoDir + '.js';
var notCSS = '!' + demoDir + '.css';
var notHTML= '!' + demoDir + '.html';
gulp.src([demoDir, notJS, notCSS, notHTML])
.pipe(gulp.dest(distDir + demoID));
next();
}
};
// Gives back a pipe with an array of the parsed data from all of the module's demos
// @param moduleName modulename to parse
// @param fileTasks: tasks to run on the files found in the demo's folder
// Emits demo objects
exports.readModuleDemos = function(moduleName, fileTasks) {
var name = moduleName.split('.').pop();
return gulp.src('src/{components,services}/' + name + '/demo*/')
.pipe(through2.obj(function(demoFolder, enc, next) {
var demoId = name + path.basename(demoFolder.path);
var srcPath = demoFolder.path.substring(demoFolder.path.indexOf('src/') + 4);
var split = srcPath.split('/');
var demo = {
ngModule: '',
id: demoId,
css:[], html:[], js:[]
};
gulp.src(demoFolder.path + '**/*', { base: path.dirname(demoFolder.path) })
.pipe(fileTasks(demoId))
.pipe(through2.obj(function(file, enc, cb) {
if (/index.html$/.test(file.path)) {
demo.moduleName = moduleName;
demo.name = path.basename(demoFolder.path);
demo.label = exports.humanizeCamelCase(path.basename(demoFolder.path).replace(/^demo/, ''));
demo.id = demoId;
demo.index = toDemoObject(file);
} else {
var fileType = path.extname(file.path).substring(1);
if (fileType == 'js') {
demo.ngModule = demo.ngModule || findModule.any(file.contents.toString());
}
demo[fileType] && demo[fileType].push(toDemoObject(file));
}
cb();
}, function(done) {
next(null, demo);
}));
function toDemoObject(file) {
return {
contents: file.contents.toString(),
name: path.basename(file.path),
label: path.basename(file.path),
fileType: path.extname(file.path).substring(1),
outputPath: 'demo-partials/' + name + '/' + path.basename(demoFolder.path) + '/' + path.basename(file.path)
};
}
}));
};
var pathsForModules = {};
exports.pathsForModule = function(name) {
return pathsForModules[name] || lookupPath();
function lookupPath() {
gulp.src('src/{services,components,core}/**/*')
.pipe(through2.obj(function(file, enc, next) {
var module = findModule.any(file.contents);
if (module && module.name == name) {
var modulePath = file.path.split(path.sep).slice(0, -1).join(path.sep);
pathsForModules[name] = modulePath + '/**';
}
next();
}));
return pathsForModules[name];
}
}
exports.filesForModule = function(name) {
if (pathsForModules[name]) {
return srcFiles(pathsForModules[name]);
} else {
return gulp.src('src/{services,components,core}/**/*')
.pipe(through2.obj(function(file, enc, next) {
var module = findModule.any(file.contents);
if (module && (module.name == name)) {
var modulePath = file.path.split(path.sep).slice(0, -1).join(path.sep);
pathsForModules[name] = modulePath + '/**';
var self = this;
srcFiles(pathsForModules[name]).on('data', function(data) {
self.push(data);
});
}
next();
}));
}
function srcFiles(path) {
return gulp.src(path)
.pipe(through2.obj(function(file, enc, next) {
if (file.stat.isFile()) next(null, file);
else next();
}));
}
};
exports.appendToFile = function(filePath) {
var bufferedContents;
return through2.obj(function(file, enc, next) {
bufferedContents = file.contents.toString('utf8') + '\n';
next();
}, function(done) {
var existing = fs.readFileSync(filePath, 'utf8');
bufferedContents = existing + '\n' + bufferedContents;
var outputFile = new gutil.File({
cwd: process.cwd(),
base: path.dirname(filePath),
path: filePath,
contents: new Buffer(bufferedContents)
});
this.push(outputFile);
done();
});
};
exports.buildNgMaterialDefinition = function() {
var srcBuffer = [];
var modulesSeen = [];
var count = 0;
return through2.obj(function(file, enc, next) {
var module = findModule.material(file.contents);
if (module) modulesSeen.push(module.name);
srcBuffer.push(file);
next();
}, function(done) {
var self = this;
var requiredLibs = ['ng', 'ngAnimate', 'ngAria'];
var dependencies = JSON.stringify(requiredLibs.concat(modulesSeen));
var ngMaterialModule = "angular.module('ngMaterial', " + dependencies + ');';
var angularFile = new gutil.File({
base: process.cwd(),
path: process.cwd() + '/ngMaterial.js',
contents: new Buffer(ngMaterialModule)
});
// Elevate ngMaterial module registration to first in queue
self.push(angularFile);
srcBuffer.forEach(function(file) {
self.push(file);
});
srcBuffer = [];
done();
});
};
function moduleNameToClosureName(name) {
return 'ng.' + name;
}
exports.addJsWrapper = function(enforce) {
return through2.obj(function(file, enc, next) {
var module = findModule.any(file.contents);
if (!!enforce || module) {
file.contents = new Buffer([
!!enforce ? '(function(){' : '(function( window, angular, undefined ){',
'"use strict";\n',
file.contents.toString(),
!!enforce ? '})();' : '})(window, window.angular);'
].join('\n'));
}
this.push(file);
next();
});
};
exports.addClosurePrefixes = function() {
return through2.obj(function(file, enc, next) {
var module = findModule.any(file.contents);
if (module) {
var closureModuleName = moduleNameToClosureName(module.name);
var requires = (module.dependencies || []).sort().map(function(dep) {
return dep.indexOf(module.name) === 0 ? '' : 'goog.require(\'' + moduleNameToClosureName(dep) + '\');';
}).join('\n');
file.contents = new Buffer([
'goog.provide(\'' + closureModuleName + '\');',
requires,
file.contents.toString(),
closureModuleName + ' = angular.module("' + module.name + '");'
].join('\n'));
}
this.push(file);
next();
});
};
exports.buildModuleBower = function(name, version) {
return through2.obj(function(file, enc, next) {
this.push(file);
var module = findModule.any(file.contents);
if (module) {
var bowerDeps = {};
(module.dependencies || []).forEach(function(dep) {
var convertedName = 'angular-material-' + dep.split('.').pop();
bowerDeps[convertedName] = version;
});
var bowerContents = JSON.stringify({
name: 'angular-material-' + name,
version: version,
dependencies: bowerDeps
}, null, 2);
var bowerFile = new gutil.File({
base: file.base,
path: file.base + '/bower.json',
contents: new Buffer(bowerContents)
});
this.push(bowerFile);
}
next();
});
};
exports.hoistScssVariables = function() {
return through2.obj(function(file, enc, next) {
var contents = file.contents.toString().split('\n');
var lastVariableLine = -1;
var openCount = 0;
var closeCount = 0;
var openBlock = false;
for( var currentLine = 0; currentLine < contents.length; ++currentLine) {
var line = contents[currentLine];
if (openBlock || /^\s*\$/.test(line) && !/^\s+/.test(line)) {
openCount += (line.match(/\(/g) || []).length;
closeCount += (line.match(/\)/g) || []).length;
openBlock = openCount != closeCount;
var variable = contents.splice(currentLine, 1)[0];
contents.splice(++lastVariableLine, 0, variable);
}
}
file.contents = new Buffer(contents.join('\n'));
this.push(file);
next();
});
};
exports.cssToNgConstant = function(ngModule, factoryName) {
return through2.obj(function(file, enc, next) {
var template = '(function(){ \nangular.module("%1").constant("%2", "%3"); \n})();\n\n';
var output = file.contents.toString().replace(/\n/g, '').replace(/\"/,'\\"');
var jsFile = new gutil.File({
base: file.base,
path: file.path.replace('css', 'js'),
contents: new Buffer(
template.replace('%1', ngModule)
.replace('%2', factoryName)
.replace('%3', output)
)
});
this.push(jsFile);
next();
});
};