arnaucube
arnaucube/comunicationLeapPublic Self-hosted repository
comunicationLeap/node_modules/toastr-master/gulpfile.js
node_modules/toastr-master/gulpfile.jsRaw
  1/* jshint node:true, camelcase:false */
  2var gulp = require('gulp');
  3var del = require('del');
  4var glob = require('glob');
  5var karma = require('karma').server;
  6var merge = require('merge-stream');
  7var plato = require('plato');
  8var plug = require('gulp-load-plugins')();
  9
 10var paths = {
 11    js: './toastr.js',
 12    less: './toastr.less',
 13    report: './report',
 14    build: './build'
 15};
 16
 17var colors = plug.util.colors;
 18var log = plug.util.log;
 19
 20/**
 21 * List the available gulp tasks
 22 */
 23gulp.task('help', plug.taskListing);
 24
 25/**
 26 * Lint the code, create coverage report, and a visualizer
 27 * @return {Stream}
 28 */
 29gulp.task('analyze', function() {
 30    log('Analyzing source with JSHint, JSCS, and Plato');
 31
 32    var jshint = analyzejshint([paths.js]);
 33    var jscs = analyzejscs([paths.js]);
 34
 35    startPlatoVisualizer();
 36
 37    return merge(jshint, jscs);
 38});
 39
 40/**
 41 * Minify and bundle the app's JavaScript
 42 * @return {Stream}
 43 */
 44gulp.task('js', function() {
 45    log('Bundling, minifying, and copying the app\'s JavaScript');
 46
 47    return gulp
 48        .src(paths.js)
 49        .pipe(plug.sourcemaps.init())
 50        .pipe(plug.bytediff.start())
 51        .pipe(plug.uglify({}))
 52        .pipe(plug.bytediff.stop(bytediffFormatter))
 53        .pipe(plug.sourcemaps.write('.'))
 54        .pipe(plug.rename(function(path) {
 55            if (path.extname === '.js') {
 56                path.basename += '.min';
 57            }
 58        }))
 59        .pipe(gulp.dest(paths.build));
 60});
 61
 62/**
 63 * Minify and bundle the CSS
 64 * @return {Stream}
 65 */
 66gulp.task('css', function() {
 67    log('Bundling, minifying, and copying the app\'s CSS');
 68
 69    return gulp.src(paths.less)
 70        .pipe(plug.less())
 71//        .pipe(plug.autoprefixer('last 2 version', '> 5%'))
 72        .pipe(gulp.dest(paths.build))
 73        .pipe(plug.bytediff.start())
 74        .pipe(plug.minifyCss({}))
 75        .pipe(plug.bytediff.stop(bytediffFormatter))
 76        .pipe(plug.rename('toastr.min.css'))
 77        .pipe(gulp.dest(paths.build));
 78});
 79
 80/**
 81 * Build js and css
 82 */
 83gulp.task('default', ['js', 'css'], function() {
 84    log('Analyze, Build CSS and JS');
 85});
 86
 87/**
 88 * Remove all files from the build folder
 89 * One way to run clean before all tasks is to run
 90 * from the cmd line: gulp clean && gulp build
 91 * @return {Stream}
 92 */
 93gulp.task('clean', function(cb) {
 94    log('Cleaning: ' + plug.util.colors.blue(paths.report));
 95    log('Cleaning: ' + plug.util.colors.blue(paths.build));
 96
 97    var delPaths = [paths.build, paths.report];
 98    del(delPaths, cb);
 99});
100
101/**
102 * Run specs once and exit
103 * To start servers and run midway specs as well:
104 *    gulp test --startServers
105 * @return {Stream}
106 */
107gulp.task('test', function(done) {
108    startTests(true /*singleRun*/ , done);
109});
110
111////////////////
112
113/**
114 * Execute JSHint on given source files
115 * @param  {Array} sources
116 * @param  {String} overrideRcFile
117 * @return {Stream}
118 */
119function analyzejshint(sources, overrideRcFile) {
120    var jshintrcFile = overrideRcFile || './.jshintrc';
121    log('Running JSHint');
122    return gulp
123        .src(sources)
124        .pipe(plug.jshint(jshintrcFile))
125        .pipe(plug.jshint.reporter('jshint-stylish'));
126}
127
128/**
129 * Execute JSCS on given source files
130 * @param  {Array} sources
131 * @return {Stream}
132 */
133function analyzejscs(sources) {
134    log('Running JSCS');
135    return gulp
136        .src(sources)
137        .pipe(plug.jscs('./.jscsrc'));
138}
139
140/**
141 * Start Plato inspector and visualizer
142 */
143function startPlatoVisualizer() {
144    log('Running Plato');
145
146    var files = glob.sync('toastr.js');
147
148    var options = {
149        title: 'Plato Inspections Report'
150    };
151    var outputDir = './report/plato';
152
153    plato.inspect(files, outputDir, options, platoCompleted);
154
155    function platoCompleted(report) {
156        var overview = plato.getOverviewReport(report);
157        log(overview.summary);
158    }
159}
160
161/**
162 * Start the tests using karma.
163 * @param  {boolean} singleRun - True means run once and end (CI), or keep running (dev)
164 * @param  {Function} done - Callback to fire when karma is done
165 * @return {undefined}
166 */
167function startTests(singleRun, done) {
168    karma.start({
169        configFile: __dirname + '/karma.conf.js',
170        singleRun: !!singleRun
171    }, karmaCompleted);
172
173    ////////////////
174
175    function karmaCompleted() {
176        done();
177    }
178}
179
180/**
181 * Formatter for bytediff to display the size changes after processing
182 * @param  {Object} data - byte data
183 * @return {String}      Difference in bytes, formatted
184 */
185function bytediffFormatter(data) {
186    var difference = (data.savings > 0) ? ' smaller.' : ' larger.';
187    return data.fileName + ' went from ' +
188        (data.startSize / 1000).toFixed(2) + ' kB to ' + (data.endSize / 1000).toFixed(2) + ' kB' +
189        ' and is ' + formatPercent(1 - data.percent, 2) + '%' + difference;
190}
191
192/**
193 * Format a number as a percentage
194 * @param  {Number} num       Number to format as a percent
195 * @param  {Number} precision Precision of the decimal
196 * @return {Number}           Formatted perentage
197 */
198function formatPercent(num, precision) {
199    return (num * 100).toFixed(precision);
200}