Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / build-scripts / gulp-staged.js
blob23599aa45bbeb83b755553786e513bfa28e341c3
1 'use strict';
3 var gulp = require('gulp');
4 var runSequence = require('run-sequence');
5 var gutil = require('gulp-util');
7 var argv = require('yargs')
8 .option('skip-stage', {
9 array: true,
10 describe: 'skip a stage'
12 .help('help').argv;
14 function shouldSkip(stageName) {
15 return argv['skip-stage'] && argv['skip-stage'].indexOf(stageName) >= 0;
18 function findStageTasks(config, stageName) {
19 var items = Object.keys(config)
20 .map(function(name) {
21 return name + ':' + stageName;
23 .filter(function(fullName) {
24 var hasTask = !!gulp.tasks[fullName];
25 return hasTask;
26 });
28 return items;
31 var stageMetaData = {};
33 function configureTasks(config) {
34 function createStageTask(stageName, previousStages) {
35 if (shouldSkip(stageName)) return;
37 if (!previousStages) previousStages = [];
39 previousStages = previousStages.filter(function(name) {
40 return !shouldSkip(name);
41 });
43 var preTaskName = 'pre-' + stageName;
44 var postTaskName = 'post-' + stageName;
46 var preSteps = findStageTasks(config, preTaskName);
47 var steps = findStageTasks(config, stageName);
48 var postSteps = findStageTasks(config, postTaskName);
50 if (preSteps.length) {
51 gulp.task(preTaskName, preSteps, function() {
52 gutil.log(preTaskName + ' complete');
53 });
56 if (postSteps.length) {
57 gulp.task(postTaskName, postSteps, function() {
58 gutil.log(postTaskName + ' complete');
59 });
62 var metaData = {
63 dep: previousStages
66 stageMetaData[stageName] = metaData;
67 if (preSteps.length) {
68 metaData.pre = preSteps;
71 if (steps.length) {
72 metaData.main = steps;
75 if (postSteps.length) {
76 metaData.post = postSteps;
79 gulp.task(stageName, previousStages, function(callback) {
80 var seq = [];
81 if (preSteps.length) {
82 seq.push(preTaskName);
85 if (steps.length) {
86 seq.push(steps);
89 if (postSteps.length) {
90 seq.push(postTaskName);
93 if (seq.length === 0) {
94 return callback();
97 gutil.log('Starting ' + stageName + ' stage', seq);
99 seq.push(function(err) {
100 if (err) {
101 gutil.log('Stage ' + stageName + ' failed');
102 } else {
103 gutil.log('Stage ' + stageName + ' complete');
106 callback(err);
109 runSequence.apply(null, seq);
113 gulp.task('display-stages', function() {
114 gutil.log('-------------------');
115 gutil.log('Stages');
116 gutil.log('-------------------');
117 Object.keys(stageMetaData).forEach(function(stageName) {
118 var meta = stageMetaData[stageName];
119 gutil.log('+', stageName);
120 if (meta.dep && meta.dep.length) {
121 gutil.log(' Dependencies:');
122 meta.dep.forEach(function(m) {
123 gutil.log(' ' + m);
126 if (meta.pre && meta.pre.length) {
127 gutil.log(' Pre-steps:');
128 meta.pre.forEach(function(m) {
129 gutil.log(' ' + m);
133 if (meta.main && meta.main.length) {
134 gutil.log(' Main:');
135 meta.main.forEach(function(m) {
136 gutil.log(' ' + m);
140 if (meta.post && meta.post.length) {
141 gutil.log(' Post-steps:');
142 meta.post.forEach(function(m) {
143 gutil.log(' ' + m);
150 * ------------------------------------------------
151 * Stage Definitions:
152 * ------------------------------------------------
156 * Validation stage: is everything as we expect it to be
157 * before kicking off the build
159 createStageTask('validate');
162 * Test stage: does the code do what it should?
164 createStageTask('test');
167 * Compile stage: process the code into a new form
169 createStageTask('compile', ['test', 'validate']);
172 * Assemble stage: gather all the files
174 createStageTask('assemble', ['compile']);
177 * Package stage: create packaged artifacts
179 createStageTask('package', ['assemble']);
182 * Clean stage: clean the environment
184 createStageTask('clean');
187 * Watch stage: watch the development environment and
188 * reload if required
190 createStageTask('watch');
192 gulp.task('default', ['package']);
195 module.exports = configureTasks;