check if runSequence fail - gulp

I have the following task on my gulpfile:
gulp.task('build', function (cb) {
return runSequence(
'clean',
'tsd',
'ts-lint',
['sass', 'copy-assets', 'ts-compile', 'templates', 'copy-vendor'],
'karma-once',
'index',
cb
);
});
How can I check if any of the tasks in a runSequence fail in gulp?

The callback function that runSequence accepts as last argument, gives you an err object if some task failed.
runSequence('mytask', ['othertask'], function(err) {
if (err) {
// you can check for err.message or err.task for the name of the task
}
});

Related

GUlp - following tasks did not complete

I'm using gulp 4, and i have the following gulpfile.js:
'use strict';
var gulp = require('gulp'), del = require('del');
gulp.task('copy-fonts', () => {
return gulp.src(['node_modules/ng/resources/fonts/**/*'])
.pipe(gulp.dest('apps/src/assets/fonts'));
});
gulp.task('clean-copy', () => {
del(['apps/src/assets/fonts']);
});
gulp.task('copy-assets', gulp.series(['clean-copy', 'copy-fonts']), (done) => {
console.log("Gulp is running...");
done();
});
When i run this command: gulp copy-assets, i get the below error:
[11:59:06] The following tasks did not complete: copy-assets
[11:59:06] Did you forget to signal async completion?
In this code:
gulp.task('copy-assets', gulp.series(['clean-copy', 'copy-fonts']), (done) => {
console.log("Gulp is running...");
done();
});
the task has three arguments - that is gulp v3 syntax, not gulp 4. So change that to
gulp.task('copy-assets', gulp.series(['clean-copy', 'copy-fonts'], (done) => {
console.log("Gulp is running...");
done();
));
The done return is now within the gulp.series arguments. Also make this change, I think it will be necessary:
gulp.task('clean-copy', (cb) => {
del(['apps/src/assets/fonts'] );
cb();
});

Trying to start server with Gulp and it throw error

I'm trying to run sever and I get this assertion error. I've uninstalled node and NPM and reinstalled again also I tried many steps and suggested solutions here but it doesn't fit with my problem.
AssertionError [ERR_ASSERTION]: Task never defined: node_modules/scss/bootstrap.scss
at getFunction (C:\Users\Saviour\Desktop\Portfolio\Bootstrap\Project1\node_modules\undertaker\lib\helpers\normalizeArgs.js:15:5)
at map (C:\Users\Saviour\Desktop\Portfolio\Bootstrap\Project1\node_modules\arr-map\index.js:20:14)
at normalizeArgs (C:\Users\Saviour\Desktop\Portfolio\Bootstrap\Project1\node_modules\undertaker\lib\helpers\normalizeArgs.js:22:10)
at Gulp.series (C:\Users\Saviour\Desktop\Portfolio\Bootstrap\Project1\node_modules\undertaker\lib\series.js:13:14)
at C:\Users\Saviour\Desktop\Portfolio\Bootstrap\Project1\gulpfile.js:7:26
at sass (C:\Users\Saviour\Desktop\Portfolio\Bootstrap\Project1\node_modules\undertaker\lib\set-task.js:13:15)
at bound (domain.js:422:14)
at runBound (domain.js:435:12)
at asyncRunner (C:\Users\Saviour\Desktop\Portfolio\Bootstrap\Project1\node_modules\async-done\index.js:55:18)
at processTicksAndRejections (internal/process/task_queues.js:75:11)
This's my code
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var sass = require('gulp-sass');
gulp.task('sass', async function () {
return gulp.src(gulp.series('node_modules/scss/bootstrap.scss', 'src/scss/*.scss'))
.pipe(sass())
.pipe(gulp.dest("src/css"))
.pipe(browserSync.stream());
});
gulp.task('js', async function () {
return gulp.src(gulp.series('node_modules/bootstrap/dist/js/bootstrap.min.js', 'node_modules/jquery/dist/jquery.min.js', 'node_modules/popper.js/dist/popper.min.js'))
.pipe(gulp.dest('src/js'))
.pipe(browserSync.stream());
});
gulp.task('serve', async function () {
browserSync.init({
server: "./src",
});
gulp.watch(['node_modules/bootstrap/scss/bootstrap.scss', 'src/scss/*.scss'], 'sass');
gulp.watch("src/*.html").on('change', browserSync.reload);
})
gulp.task('default', gulp.series(gulp.parallel('sass', 'js', 'serve')));
The error is coming from the sass and js tasks. You are wrapping your sources in gulp.series. So Gulp is trying to run your sources as tasks.
They just need to be in an array [] like in the watch task.
Just change:
gulp.task('sass', async function () {
return gulp.src(gulp.series('node_modules/scss/bootstrap.scss', 'src/scss/*.scss'))
to:
gulp.task('sass', async function () {
return gulp.src(['node_modules/scss/bootstrap.scss', 'src/scss/*.scss'])
and then the same format in the js task
The second error (coming from your watch task) is because you need to wrap the tasks you are watching in either series or parallel. Even when theres only one task So change:
gulp.watch(['node_modules/bootstrap/scss/bootstrap.scss', 'src/scss/*.scss'], 'sass');
to:
gulp.watch(['node_modules/bootstrap/scss/bootstrap.scss', 'src/scss/*.scss'], gulp.series('sass'));

Gulp notify not working on successful cucumber steps

I am using gulp-notify to get notification of passing and failing cucumber steps.
The thing is that I only get notifications when it is failing, not when tests are passing.
No errors are thrown but the terminal shows passing tests, and I don't get any notification.
Here the contents of my Gulpfile.js:
var gulp = require('gulp');
var cucumber = require('gulp-cucumber');
var notify = require('gulp-notify');
gulp.task('cucumber', function() {
gulp.src('*features/*')
.pipe(cucumber({
'steps': '*features/step_definitions/*.js',
'support': '*features/support/*.js'
}))
.on('error', notify.onError({
title: 'Red',
message: 'Your test(s) failed'
}))
.pipe(notify({
title: 'Green',
message: 'All tests passed (you can refactor)'
}));
});
gulp.task('watch', function() {
gulp.watch(['features/**/*.feature', 'features/**/*.js', 'script/**/*.js'], ['cucumber']);
});
gulp.task('default', ['watch']);
Any ideas what I could be missing?
I got it working by calling directly cucumberjs, like this:
const gulp = require('gulp');
const notifier = require('node-notifier');
const path = require('path');
gulp.task('cucumber', function() {
const { exec } = require('child_process');
exec('clear && node_modules/.bin/cucumber-js', (error, stdout, stderr) => {
if (error) {
notifier.notify({
title: 'Red',
message: 'Your test(s) failed',
icon: path.join(__dirname, 'failed.png')
});
} else {
notifier.notify({
title: 'Green',
message: 'All tests passed (you can refactor)',
icon: path.join(__dirname, 'passed.png')
});
}
console.log(stdout);
console.log(stderr);
});
});
gulp.task('watch', function() {
gulp.watch(['features/**/*.js', 'script/**/*.js'], ['cucumber']);
});
gulp.task('default', ['cucumber', 'watch']);

An error when use gulp and babelify?

This is my gulp file
var gulp = require('gulp');
var browserify = require('browserify');
var babelify = require('babelify');
var source = require('vinyl-source-stream');
gulp.task('build', function () {
return browserify({entries: 'main.js', extensions: ['.js'], debug: true})
.transform(babelify)
.bundle()
.on("error", function (err) { console.log("Error : " + err.message); })
.pipe(source('bundle.js'))
.pipe(gulp.dest('dist'));
});
gulp.task('watch', ['build'], function () {
gulp.watch('main.js', ['build']);
});
gulp.task('default', ['watch']);
Here is my main.js
var React = require('react');
var ReactDOM = require('react-dom');
ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('example')
);
There is an error:unexcepted token (5:2) while parsing file main.js.
I have checked my code serveral times, still don't know why?
Please try to run this command below and change gulp.task() method to below.
npm i babel-preset-react
gulp.task('build', function () {
return browserify({entries: 'main.js', extensions: ['.js'], debug: true})
.transform(babelify.configure({
presets: ["react"]
}))
.bundle()
.on("error", function (err) { console.log("Error : " + err.message); })
.pipe(source('bundle.js'))
.pipe(gulp.dest('dist'));
});

Gulp BrowserSync and npm start

I'd like to start my NodeJS application using BrowserSync but it seems not working.
Error message : Error: listen EADDRINUSE
Edit #1:
I found this way to resolve my issue.
gulp.task('sync', ['start', 'watch'], function(cb) {
browserSync.init({
files: ['**/*'],
proxy: 'http://localhost:3000',
port: 4000
});
});
gulp.task('start', function() {
var called = false;
return nodemon({
script: './bin/www',
watch: ['Application.js']
}).on('start', function onStart() {
if (!called) {
cb();
}
called = true;
}).on('restart', function onRestart() {
setTimeout(function reload() {
browserSync.reload({
stream: false
});
}, 500);
});
});
EADDRINUSE means that the port number which listen() tries to bind the server to is already in use. So, in your case, there must be running a server on port 80 already. If you have another webserver running on this port you will get the EADDRINUSE error.
var gulp = require('gulp');
var browserSync = require('browser-sync');
var nodemon = require('gulp-nodemon');
gulp.task('default', ['browser-sync'], function () {
});
gulp.task('browser-sync', ['nodemon'], function() {
browserSync.init(null, {
proxy: "http://localhost:5000",
files: ["**/*.*"],
browser: "google chrome",
port: 7000,
});
});
gulp.task('nodemon', function (cb) {
nodemon({
script: './bin/www/app.js'
})
.on('start', function () {
cb();
})
.on('error', function(err) {
// Make sure failure causes gulp to exit
throw err;
});
});
Hope it helps :)