This repository was archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgulpfile.js
313 lines (220 loc) · 7.06 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
'use strict';
var gulp = require('gulp'),
clean = require('gulp-clean'),
ngmin = require('gulp-ngmin'),
concat = require('gulp-concat'),
uglify = require('gulp-uglifyjs'),
jshint = require('gulp-jshint'),
shell = require('gulp-shell'),
Dgeni = require('dgeni'),
path = require('canonical-path'),
fs = require('fs'),
_ = require('lodash'),
runSequence = require('run-sequence'),
rename = require('gulp-rename'),
browserSync = require('browser-sync')
;
var paths = {
src: 'src',
coverage: 'test/coverage',
build: 'dist',
temp: 'tmp',
docSrc: 'docs'
};
var components = [];
var createDocPackage = function(component) {
try {
var basePath = paths.src + '/' + component;
var newDocs = new Dgeni.Package(component + '-docs', [require('./docs/dgeni-conf')])
.config(function(log, readFilesProcessor, writeFilesProcessor) {
// Set logging level
log.level = 'warn';
readFilesProcessor.basePath = path.resolve(__dirname, '.');
readFilesProcessor.sourceFiles = [
{
include: basePath + '/main.js',
basePath: paths.src
},
{
include: basePath + '/*/**/*.js',
basePath: paths.src
}
];
writeFilesProcessor.outputFolder = paths.temp + '/docs';
})
;
return new Dgeni([newDocs]);
} catch(x) {
console.log(x.stack);
throw x;
}
};
gulp.task('docs-main', [], function() {
try {
var newDocs = new Dgeni.Package('toplevel-docs', [require('./docs/dgeni-toplev-conf')])
.config(function(log, readFilesProcessor, writeFilesProcessor) {
// Set logging level
log.level = 'warn';
readFilesProcessor.basePath = path.resolve(__dirname, '.');
readFilesProcessor.sourceFiles = [
{
include: paths.src + '/*/main.js',
basePath: paths.src
},
{
include: paths.src + '/app.js',
basePath: paths.src
}
];
writeFilesProcessor.outputFolder = paths.build + '/docs';
})
;
return (new Dgeni([newDocs])).generate();
} catch(x) {
console.log(x.stack);
throw x;
}
});
// add a task for each component
var buildComponent = function(component) {
return function() {
var outpath = paths.build + '/js';
return gulp.src([paths.src + '/' + component + '/**/*.js'], {base: paths.src})
// output for docs
.pipe(gulp.dest(paths.build + '/docs/src'))
// process for dist version
.pipe(concat('dcm-ui-' + component + '.js'))
.pipe(gulp.dest(outpath))
.pipe(rename({suffix: '.min'}))
.pipe(ngmin())
.pipe(uglify({outSourceMap: true, basePath: outpath}))
.pipe(gulp.dest(outpath))
;
};
};
var generateRawDocs = function(component) {
return function() {
try {
return createDocPackage(component).generate();
} catch(x) {
console.log(x.stack);
throw x;
}
};
};
var processDocs = function(component) {
return function() {
gulp.src(paths.temp + '/docs/partials/dcm-ui.' + component + '/**/*.html', {base: paths.temp + '/docs/partials/dcm-ui.' + component })
.pipe(gulp.dest(paths.build + '/docs/views/' + component))
;
};
};
var copyDocAssets = function(component) {
return function () {
return gulp.src([paths.src + '/' + component + '/**'])
.pipe(gulp.dest(paths.build + '/docs/src/' + component))
;
};
};
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
// setup tasks for each component
components = getFolders(path.resolve(__dirname, './src'));
_.each(components, function(component){
// createDocPackage(component);
gulp.task(component + '-build', [], buildComponent(component));
gulp.task(component + '-gen-docs', [], generateRawDocs(component));
gulp.task(component + '-doc-assets', [], copyDocAssets(component));
gulp.task(component + '-docs', [component + '-gen-docs', component + '-doc-assets'], processDocs(component));
gulp.task(component + '-reload', [component + '-build', component + '-docs']);
});
gulp.task('docs', function(callback) {
var tasks = _.map(components, function(item){ return item + '-docs'; });
tasks.push( ['docs-main', 'doc-assets', 'doc-examples', 'doc-main-js']);
tasks.push(callback);
runSequence.apply(this,tasks);
});
gulp.task('build', function(callback) {
runSequence('clean-build', 'docs', _.map(components, function(item){ return item + '-build'; }), callback);
});
gulp.task('doc-assets', function () {
var defaultDeployment = require('./docs/config/default');
var deployment = defaultDeployment();
var copyFiles = deployment.examples.copyFiles.scripts
.concat(deployment.examples.copyFiles.stylesheets)
.concat(deployment.examples.copyFiles.misc)
.concat(deployment.indexPage.copyFiles.scripts)
.concat(deployment.indexPage.copyFiles.stylesheets)
.concat(deployment.indexPage.copyFiles.misc)
;
return gulp.src(copyFiles, {cwd: 'docs', base: 'docs'})
.pipe(gulp.dest(paths.build + '/docs'));
});
gulp.task('doc-main-js', function () {
return gulp.src( paths.src + '/app.js')
.pipe(gulp.dest(paths.build + '/docs/src'));
});
gulp.task('doc-examples', function () {
return gulp.src( paths.temp + '/docs/examples/**')
.pipe(gulp.dest(paths.build + '/docs/examples'));
});
gulp.task('clean-docs', function() {
return gulp.src([paths.temp + '/docs', paths.build + '/docs'], {read: false})
.pipe(clean())
;
});
gulp.task('clean-build', [], function() {
return gulp.src([paths.build, paths.coverage, paths.temp], {read: false})
.pipe(clean())
;
});
gulp.task('reset-dist', [], function() {
return gulp.src('')
.pipe(shell(['git checkout HEAD -- dist/']))
;
});
// clean resets the output to the state of the previous commit
gulp.task('clean', function(callback) {
var tasks = ['clean-build', 'reset-dist', callback];
runSequence.apply(this,tasks);
});
// file watchers
gulp.task('server', ['dev'], function () {
// Rerun the build task when a file changes
for (var idx in components) {
gulp.watch([paths.src + '/' + components[idx] + '/**'], [components[idx] + '-reload']);
}
// watch asset templates dir
gulp.watch(['docs/**', '!docs/assets/bower_components/**'], ['docs']);
// watch examples output dir (and copy to dist/docs if changed)
gulp.watch(['tmp/docs/examples/**'], function(changed){
gulp.src([changed.path], {base: 'tmp/docs/examples/'})
.pipe(gulp.dest(paths.build + '/docs/examples'))
;
});
// watch output docs dir
gulp.watch(['dist/docs/**'], _.debounce(browserSync.reload, 100));
});
gulp.task('test', function(){
return gulp.src(paths.src + '/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'))
;
});
// Dev task
gulp.task('dev', ['build'], function() {
browserSync({
server: {
baseDir: paths.build + '/docs',
routes: {
'/coverage': paths.coverage
}
},
notify: false
});
});
gulp.task('default', ['server']);