gulp任务完成后显示成功消息

时间:2017-02-16 12:44:24

标签: gulp gulp-watch gulp-babel

我创建了一个简单的Gulp任务来检查我的ES6文件中的更改。我想将它们转发并在出现问题时显示错误消息。

正在显示错误屏幕。但是,我想在一切都成功时显示不同的消息。我尝试了.on('end')方法,但是当出现一些错误时也会调用此方法。

我当前的Gulpfile如下所示:

const gulp = require('gulp');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const pump = require('pump');
const imagemin = require('gulp-imagemin');
const sass = require('gulp-sass');
const DISTRIBUTION_PATH = 'public/theme/js/app';
const plumber = require('gulp-plumber');
const gutil = require('gulp-util');
const clear = require('clear');

gulp.task('transpile', () =>
    gulp.watch('theme/js/**/*.js', () => {
        return gulp.src('theme/js/**/*.js')
            .pipe(plumber())
            .pipe(babel({
                presets: ['es2015'],
                plugins: [
                    'transform-object-rest-spread'
                ]
            }))
            .on('error', err => {
                clear();
                gutil.log(gutil.colors.red('[Compilation Error]'));
                gutil.log(err.fileName + ( err.loc ? `( ${err.loc.line}, ${err.loc.column} ): ` : ': '));
                gutil.log(gutil.colors.red('error Babel: ' + err.message + '\n'));
                gutil.log(err.codeFrame);
            })
            .pipe(gulp.dest(DISTRIBUTION_PATH));
    })
);

任何想法如何实现这一目标?

2 个答案:

答案 0 :(得分:4)

答案可能有点晚,但对于像我这样的Google员工,我已经创建了一个解决方案。我添加了一个布尔值isSuccess来确定转换是否成功。如果出现错误,则isSuccess变为false,并且消息未显示为“on end”。

const gulp = require('gulp');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const pump = require('pump');
const imagemin = require('gulp-imagemin');
const sass = require('gulp-sass');
const DISTRIBUTION_PATH = 'public/theme/js/app';
const plumber = require('gulp-plumber');
const gutil = require('gulp-util');
const clear = require('clear');

gulp.task('transpile', () =>
    gulp.watch('theme/js/**/*.js', () => {
        let isSuccess = true;
        return gulp.src('theme/js/**/*.js')
            .pipe(plumber())
            .pipe(babel({
                presets: ['es2015'],
                plugins: [
                    'transform-object-rest-spread'
                ]
            }))
            .on('error', err => {
                isSuccess = false;
                clear();
                gutil.log(gutil.colors.red('[Compilation Error]'));
                gutil.log(err.fileName + ( err.loc ? `( ${err.loc.line}, ${err.loc.column} ): ` : ': '));
                gutil.log(gutil.colors.red('error Babel: ' + err.message + '\n'));
                gutil.log(err.codeFrame);
            })
            .pipe(gulp.dest(DISTRIBUTION_PATH))
            .on('end', ()=> {
                if( isSuccess )
                    console.log('Yay success!');
            });
    })
);

答案 1 :(得分:0)

你可以做

.on('error', () => {
   // some log or code for failure
 })
.on('end', () => {
   // some log or code for success
 });