forked from angular/angular.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
429 lines (376 loc) · 13.1 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
var gulp = require('gulp');
var watch = require('gulp-watch');
var gutil = require('gulp-util');
var taskListing = require('gulp-task-listing');
var path = require('canonical-path');
var del = require('del');
var _ = require('lodash');
var Git = require("nodegit");
var argv = require('yargs').argv;
var Q = require("q");
// delPromise is a 'promise' version of del
var delPromise = Q.denodeify(del);
var Minimatch = require("minimatch").Minimatch;
var Dgeni = require('dgeni');
var fsExtra = require('fs-extra');
var fs = fsExtra;
var exec = require('child_process').exec;
var execPromise = Q.denodeify(exec);
var prompt = require('prompt');
// TODO:
// 1. Think about using runSequence
// 2. Think about using spawn instead of exec in case of long error messages.
var docShredder = require('./public/doc-shredder/doc-shredder');
var _devguideShredOptions = {
examplesDir: './public/docs/_examples',
fragmentsDir: './public/docs/_fragments'
};
var _apiShredOptions = {
examplesDir: '../angular/modules/angular2/examples',
fragmentsDir: './public/docs/_fragments/_api'
};
var _excludePatterns = ["**/node_modules/**", "**/typings/**"];
var _excludeMatchers = _excludePatterns.map(function(excludePattern){
return new Minimatch(excludePattern)
});
gulp.task('help', taskListing.withFilters(function(taskName) {
var isSubTask = taskName.substr(0,1) == "_";
return isSubTask;
}, function(taskName) {
var shouldRemove = taskName === 'default';
return shouldRemove;
}));
gulp.task('serve-and-sync', ['build-docs'], function (cb) {
// execCommands(['harp server'], {}, cb);
execCommands(['npm run harp -- server .'], {}, cb);
var browserSync = require('browser-sync').create();
browserSync.init({
proxy: 'localhost:9000',
files: ["public/docs/**/*/**/*" ],
logFileChanges: true,
reloadDelay: 500
});
devGuideExamplesWatch(_devguideShredOptions, function() {
browserSync.reload();
});
apiSourceWatch(function() {
browserSync.reload();
});
});
gulp.task('serve-and-watch', function (cb) {
execCommands(['npm run harp -- server .'], {}, cb);
devGuideExamplesWatch(_devguideShredOptions);
});
gulp.task('build-docs', ['_shred-devguide-examples', 'build-api-docs'], function() {
return buildShredMaps(true);
});
gulp.task('build-devguide-docs', ['_shred-devguide-examples'], function() {
return buildShredMaps(true);
});
gulp.task('build-api-docs', ['_shred-api-examples'], function() {
if (!fs.existsSync('../angular')) {
throw new Error('build-api-docs task requires the angular2 repo to be at ' + path.resolve('../angular'));
}
return buildApiDocs();
});
gulp.task('_shred-devguide-examples', ['_shred-clean-devguide'], function() {
return docShredder.shred( _devguideShredOptions);
});
gulp.task('_shred-clean-devguide', function(cb) {
var cleanPath = path.join(_devguideShredOptions.fragmentsDir, '**/*.*')
return delPromise([ cleanPath, '!**/*.ovr.*', '!**/_api/**']);
});
gulp.task('_shred-api-examples', ['_shred-clean-api'], function() {
return docShredder.shred( _apiShredOptions);
});
gulp.task('_shred-clean-api', function(cb) {
var cleanPath = path.join(_apiShredOptions.fragmentsDir, '**/*.*')
return delPromise([ cleanPath, '!**/*.ovr.*' ]);
});
gulp.task('_build-shred-maps', function() {
return build-shred-maps(true);
});
gulp.task('git-changed-examples', ['_shred-devguide-examples'], function(){
var after, sha, messageSuffix;
if (argv.after) {
try {
after = new Date(argv.after);
messageSuffix = ' after: ' + argv.after;
} catch (e) {
throw argv.after + " is not a valid date.";
}
} else if (argv.sha) {
sha = argv.sha;
messageSuffix = ' on commit: ' + (argv.sha.length ? argv.sha : '[last commit]');
} else {
console.log('git-changed-examples may be called with either an "--sha" argument like this:');
console.log(' gulp git-changed-examples --sha=4d2ac96fa247306ddd2d4c4e0c8dee2223502eb2');
console.log('or with an "--after" argument like this')
console.log(' gulp git-changed-examples --after="August 1, 2015"');
return;
}
var jadeShredMap;
return buildShredMaps(false).then(function(docs) {
jadeShredMap = docs[0];
if (after) {
return getChangedExamplesAfter(after);
} else if (sha) {
return getChangedExamples(sha);
} else {
console.log('git-changed-examples may be called with either an "--sha" argument like this:');
console.log(' gulp git-changed-examples --sha=4d2ac96fa247306ddd2d4c4e0c8dee2223502eb2');
console.log('or with an "--after" argument like this')
console.log(' gulp git-changed-examples --after="August 1, 2015"');
}
}).then(function(examplePaths) {
examplePaths = filterOutExcludedPatterns(examplePaths, _excludeMatchers);
console.log('\nExamples changed ' + messageSuffix);
console.log(examplePaths)
console.log("\nJade files affected by changed example files " + messageSuffix);
var jadeExampleMap = jadeShredMapToJadeExampleMap(jadeShredMap, examplePaths);
console.log(JSON.stringify(jadeExampleMap, null, " "));
console.log("-----");
}).catch(function(err) {
console.log(err);
throw err;
});
});
gulp.task('check-deploy', function() {
console.log('running harp compile...');
return execPromise('npm run harp -- compile . ./www', {}).then(function() {
execPromise('npm run live-server ./www');
return askDeploy();
}).then(function(shouldDeploy) {
if (shouldDeploy) {
console.log('deploying...');
return execPromise('firebase deploy');
} else {
return ['Not deploying'];
}
}).then(function(s) {
console.log(s.join(''));
});
});
// returns a promise;
function askDeploy() {
prompt.start();
var schema = {
name: 'shouldDeploy',
description: 'Deploy to Firebase? (y/n): ',
type: 'string',
pattern: /Y|N|y|n/,
message: "Respond with either a 'y' or 'n'",
required: true
}
var getPromise = Q.denodeify(prompt.get);
return getPromise([schema]).then(function(result) {
return result.shouldDeploy.toLowerCase() === 'y';
});
}
gulp.task('test-api-builder', function (cb) {
execCommands(['npm run test-api-builder'], {}, cb);
});
function filterOutExcludedPatterns(fileNames, excludeMatchers) {
return fileNames.filter(function(fileName) {
return !excludeMatchers.some(function(excludeMatcher) {
return excludeMatcher.match(fileName);
});
});
}
function apiSourceWatch(postShredAction) {
var srcPattern = ['../angular/modules/angular2/src/**/*.*'];
watch(srcPattern, function (event, done) {
console.log('Event type: ' + event.event); // added, changed, or deleted
console.log('Event path: ' + event.path); // The path of the modified file
// need to run just build
buildApiDocs().then(done);
});
var examplesPattern = ['../angular/modules/angular2/examples/**/*.*'];
watch(examplesPattern, function (event, done) {
console.log('Event type: ' + event.event); // added, changed, or deleted
console.log('Event path: ' + event.path); // The path of the modified file
// need to run shredder
var cleanPath = path.join(_apiShredOptions.fragmentsDir, '**/*.*');
return delPromise([ cleanPath, '!**/*.ovr.*' ]).then(function() {
return docShredder.shred(_apiShredOptions);
}).then(function() {
postShredAction && postShredAction();
});
});
}
function buildApiDocs() {
try {
var dgeni = new Dgeni([require('./public/api-builder/angular.io-package')]);
return dgeni.generate().then(function() {
return gulp.src('./public/docs/js/latest/api/**/*.*')
.pipe(gulp.dest('./public/docs/ts/latest/api'));
})
} catch(err) {
console.log(err);
console.log(err.stack);
throw err;
}
}
function devGuideExamplesWatch(shredOptions, postShredAction) {
var pattern = path.join(shredOptions.examplesDir, "**/*.*");
watch([pattern], function (event, done) {
console.log('Event type: ' + event.event); // added, changed, or deleted
console.log('Event path: ' + event.path); // The path of the modified file
docShredder.shredSingleDir(shredOptions, event.path).then(function () {
postShredAction && postShredAction();
});
});
}
function buildShredMaps(shouldWrite) {
var options = {
devguideExamplesDir: _devguideShredOptions.examplesDir,
apiExamplesDir: _apiShredOptions.examplesDir,
fragmentsDir: _devguideShredOptions.fragmentsDir,
jadeDir: './public/docs',
outputDir: './public/docs',
writeFilesEnabled: shouldWrite
};
return docShredder.buildShredMap(options).then(function(docs) {
return docs;
});
}
// returns a promise containing filePaths with any changed or added examples;
function getChangedExamples(sha) {
var examplesPath = _devguideShredOptions.examplesDir;
var relativePath = path.relative(process.cwd(), examplesPath);
return Git.Repository.open(".").then(function(repo) {
if (sha.length) {
return repo.getCommit(sha);
} else {
return repo.getHeadCommit();
}
}).then(function(commit) {
return getChangedExamplesForCommit(commit, relativePath);
}).catch(function(err) {
});
}
function getChangedExamplesAfter(date, relativePath) {
var examplesPath = _devguideShredOptions.examplesDir;
var relativePath = path.relative(process.cwd(), examplesPath);
return Git.Repository.open(".").then(function(repo) {
return repo.getHeadCommit();
}).then(function(commit) {
var repo = commit.owner();
var revWalker = repo.createRevWalk();
revWalker.sorting(Git.Revwalk.SORT.TIME);
revWalker.push(commit.id());
return revWalker.getCommitsUntil(function (commit) {
return commit.date().getTime() > date.getTime();
});
}).then(function(commits) {
return Q.all(commits.map(function(commit) {
return getChangedExamplesForCommit(commit, relativePath);
}));
}).then(function(arrayOfPaths) {
var pathMap = {};
arrayOfPaths.forEach(function(paths) {
paths.forEach(function(path) {
pathMap[path] = true;
});
});
var uniqPaths = _.keys(pathMap);
return uniqPaths;
}).catch(function(err) {
var x = err;
});
}
function getChangedExamplesForCommit(commit, relativePath) {
return commit.getDiff().then(function(diffList) {
var filePaths = [];
diffList.forEach(function (diff) {
diff.patches().forEach(function (patch) {
if (patch.isAdded() || patch.isModified) {
var filePath = path.normalize(patch.newFile().path());
var isExample = filePath.indexOf(relativePath) >= 0;
// console.log(filePath + " isExample: " + isExample);
if (isExample) {
filePaths.push(filePath);
}
}
});
});
return filePaths;
});
}
function jadeShredMapToJadeExampleMap(jadeShredMap, examplePaths) {
// remove dups in examplePaths
var exampleSet = {};
examplePaths.forEach(function(examplePath) {
exampleSet[examplePath] = examplePath;
});
var basePath = path.resolve(".");
var jadeToFragMap = jadeShredMap.jadeToFragMap;
var jadeExampleMap = {};
for (var jadePath in jadeToFragMap) {
var relativeJadePath = path.relative(basePath, jadePath);
var vals = jadeToFragMap[jadePath];
vals.forEach(function(val) {
var relativeExamplePath = path.relative(basePath, val.examplePath);
if (exampleSet[relativeExamplePath] != null) {
addKeyValue(jadeExampleMap, relativeJadePath, relativeExamplePath);
}
});
}
return jadeExampleMap;
}
function jadeShredMapToExampleJadeMap(jadeShredMap) {
var jadeToFragMap = jadeShredMap.jadeToFragMap;
var exampleJadeMap = {};
for (var jadePath in jadeToFragMap) {
var vals = jadeToFragMap[jadePath];
vals.forEach(function(val) {
var examplePath = val.examplePath;
addKeyValue(exampleJadeMap, examplePath, jadePath);
});
}
return exampleJadeMap;
}
function addKeyValue(map, key, value) {
var vals = map[key];
if (vals) {
if (vals.indexOf(value) == -1) {
vals.push(value);
}
} else {
map[key] = [value];
}
}
// Synchronously execute a chain of commands.
// cmds: an array of commands
// options: { shouldLog: true, shouldThrow: true }
// cb: function(err, stdout, stderr)
function execCommands(cmds, options, cb) {
options = options || {};
options.shouldThrow = options.shouldThrow == null ? true : options.shouldThrow;
options.shouldLog = options.shouldLog == null ? true : options.shouldLog;
if (!cmds || cmds.length == 0) cb(null, null, null);
var exec = require('child_process').exec; // just to make it more portable.
exec(cmds[0], options, function(err, stdout, stderr) {
if (err == null) {
if (options.shouldLog) {
gutil.log('cmd: ' + cmds[0]);
gutil.log('stdout: ' + stdout);
}
if (cmds.length == 1) {
cb(err, stdout, stderr);
} else {
execCommands(cmds.slice(1), options, cb);
}
} else {
if (options.shouldLog) {
gutil.log('exec error on cmd: ' + cmds[0]);
gutil.log('exec error: ' + err);
if (stdout) gutil.log('stdout: ' + stdout);
if (stderr) gutil.log('stderr: ' + stderr);
}
if (err && options.shouldThrow) throw err;
cb(err, stdout, stderr);
}
});
}
gulp.task('default', ['help']);