-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgulpfile.js
341 lines (272 loc) · 7.76 KB
/
gulpfile.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
var gulp = require('gulp');
var templateCache = require('gulp-angular-templatecache');
var clean = require('gulp-clean');
var concat = require('gulp-concat');
var connect = require('gulp-connect');
var header = require('gulp-header');
var gulpJade = require('gulp-jade');
var gulpBeautify = require('gulp-jsbeautifier');
var rename = require('gulp-rename');
var sourcemaps = require('gulp-sourcemaps');
var template = require('gulp-template');
var uglify = require('gulp-uglify');
var gutil = require('gulp-util');
var beautify = require('js-beautify');
var connectLiveReload = require('connect-livereload')({port: 35729});
var glob = require('glob');
var jade = require('jade');
var karma = require('karma').server;
var merge = require('merge-stream');
var _ = require('lodash');
var _s = require('underscore.string'); _.mixin(_s.exports());
var fs = require('fs');
var pkg = require('./package.json');
var banner = (function() {
var tmpl = [
'pmkr.components v<%= pkg.version %>',
'<%= pkg.repository.url %>',
'License: <%= pkg.license %>',
'Author: <%= pkg.author.name %>',
'File created: <%= gutil.date(now, "m.d.yyyy") %>'
].join('\n');
var obj = {
tmpl : tmpl,
ml : ['/*',tmpl,'*/','\n'].join('\n'),
html : ['<!--',tmpl,'-->','\n'].join('\n'),
data : {pkg:pkg, gutil:gutil, now:new Date()}
};
return obj;
}());
gulp.task('default', ['watch']);
gulp.task('watch', [
'connect',
'tdd',
'watch-components',
'watch-demo'
]);
/*** BUILD ***/
gulp.task('build', [
'components',
'banner',
'demo'
]);
gulp.task('banner', [
'components'
], function() {
return gulp.src('build/**/*.js')
.pipe(header(banner.ml, banner.data))
.pipe(gulp.dest('build'))
});
/*** CLEAN ***/
gulp.task('clean-build', function() {
return gulp.src('build/*')
.pipe(clean())
});
gulp.task('clean-demo', function() {
return gulp.src(['demo/*', '!demo/src', '!demo/.git'])
.pipe(clean())
});
/*** SERVER ***/
gulp.task('connect', function() {
connect.server({
root: 'demo',
fallback: 'demo/index.html',
port: '1059',
livereload: {
port: 35729
},
middleware: function(connect, opt) {
return [
connectLiveReload
];
}
});
});
/*** TESTING ***/
gulp.task('tdd', ['build'], function() {
karma.start({
configFile: __dirname+'/karma.conf.js'
});
});
/*** COMPONENTS ***/
gulp.task('components', [
'components-full',
'components-min'
]);
gulp.task('watch-components', function() {
return gulp.watch(['src/**/*.js', '!src/**/{tests,samples}/**/*.js'],
['components-full', 'components-min']
);
});
gulp.task('components-full', function() {
return gulp.src(['src/**/*.js', '!src/**/{tests,samples}/**/*.js'])
.pipe(concat('components.js', {newLine: '\r\n\r\n'}))
.pipe(gulp.dest('build'))
;
});
gulp.task('components-min', function() {
return gulp.src(['src/**/*.js', '!src/**/{test,samples}/**/*.js'])
.pipe(sourcemaps.init())
.pipe(concat('components.min.js'))
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('build'))
;
});
/*** DEMO ***/
gulp.task('watch-demo', ['reload-demo'], function() {
gulp.watch('build/components.js', ['demo-components']);
gulp.watch(['demo/src/index.jade', 'demo/src/index-js'], ['demo-index']);
gulp.watch(['demo/src/page.jade', 'src/**/samples/**'], ['demo-pages']);
gulp.watch('demo/**/*.*', function() {
gulp.src('demo/**/*.*')
.pipe(connect.reload())
;
});
});
gulp.task('reload-demo', function() {
return
});
gulp.task('demo', [
'demo-components',
'demo-index',
'demo-pages'
]);
gulp.task('demo-components', function() {
return gulp.src('build/components.js')
.pipe(gulp.dest('demo'))
;
});
gulp.task('demo-index', function() {
var tasks = [];
tasks.push(gulp.src('demo/src/index.jade')
.pipe(gulpJade({
locals:{
data: getDemoStructure(),
_: _
}
}))
.pipe(gulpBeautify({html: {
indentSize: 2,
unformatted: []
}}))
.pipe(rename('index.html'))
.pipe(gulp.dest('demo'))
);
tasks.push(gulp.src('demo/src/index-js')
.pipe(template({
data: getDemoStructure(),
_: _
}))
.pipe(rename('index.js'))
.pipe(gulp.dest('demo'))
);
return merge(tasks);
});
gulp.task('demo-pages', function() {
var components = {};
_.forOwn(getDemoStructure(), function(componentsByType) {
_.extend(components, componentsByType);
});
var pageTasks = [];
var allJsTasks = [];
_.forOwn(components, function(component, componentName) {
if (!component.samples) { return; }
pageTasks.push(createDemoPageTask(component, componentName));
allJsTasks.push(createTaskForComponentJsSamples(component, componentName));
});
merge(allJsTasks)
.pipe(concat('samples.js', {newLine: '\r\n\r\n'}))
.pipe(gulp.dest('demo'))
;
return merge(pageTasks, allJsTasks);
});
function createDemoPageTask(component, componentName) {
return gulp.src('demo/src/page.jade')
.pipe(gulpJade({
pretty: true,
locals:{
componentName: componentName,
component: component,
samplesCode: getComponentSamplesCode(component.samples),
_: _
}
}))
.pipe(rename(componentName+'.html'))
.pipe(gulp.dest('demo/pages'))
;
}
function createTaskForComponentJsSamples(component, componentName) {
var componentJsStreams = component.samples.map(function(sample) {
return gulp.src(sample.js)
.pipe(template({
moduleName: 'pmkr.componentsDemo',
controllerName: 'pmkr.'+componentName+'.'+sample.name+'Controller'
}))
;
});
return merge(componentJsStreams);
}
function getDemoStructure() {
var structure = {};
var componentTypeFolders = glob.sync('src/*/');
componentTypeFolders.map(handleComponentTypeFolder);
return structure;
function handleComponentTypeFolder(componentTypeFolder) {
var componentType = getLastSegment(componentTypeFolder);
var componentFolders = glob.sync(componentTypeFolder+'*/');
var components = componentFolders.map(handleComponentFolder);
structure[componentType] = {}
components.map(function(component) {
structure[componentType][component.name] = component.files;
});
}
function handleComponentFolder(componentFolder) {
return {
name: getLastSegment(componentFolder),
files: {
samples: getSamples(componentFolder),
srcDir: componentFolder
}
};
}
function getSamples(componentFolder) {
var samplesFolder = glob.sync(componentFolder+'**/samples/');
if (!samplesFolder.length) { return null; }
var subDirs = glob.sync(samplesFolder+'*/');
if (!subDirs) { return []; }
return !subDirs.length ? [getSingleSample(samplesFolder, 'default')] : getManySamples(subDirs);
}
function getSingleSample(folder, name) {
return {
name: name,
jade: glob.sync(folder+'**/*.jade')[0],
js: glob.sync(folder+'**/*.js')[0]
};
}
function getManySamples(subDirs) {
return subDirs.map(function(dir) {
var name = getLastSegment(dir);
return getSingleSample(dir, name);
});
}
function getLastSegment(pathString) {
var result = pathString.match(/\/([^/]+)\/$/);
return result && result.length > 0 ? result[1] : '';
}
}
function getComponentSamplesCode(samples) {
return samples.map(function(sample) {
var jadeFile = fs.readFileSync(sample.jade, 'utf8');
var html = jade.render(jadeFile);
var prettyHtml = beautify.html(html, {
indent_size: 2,
unformatted: []
});
return {
html: prettyHtml,
jade: jadeFile,
js: fs.readFileSync(sample.js, 'utf8')
};
});
}