Running gulp exec based on condition - ionic-framework

I am working on ionic project integrated with gulp. How can run exec command based on condition?
I have tried following
var isIOSBuild = false;
if(args.iosBuild){
isIOSBuild = true;
console.log("Creating IOS Build...");
}
// Build application
gulp.task('ios_build', function (cb) {
gulp.src('', {read: false})
.pipe(gulpif(false,
exec(IOS_BUILD_COMMAND,
{
cwd : './',
maxBuffer: 1024 * 1024
},
function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
})
));
});
Input: gulp -r
I have put up condition for running exec command still the IOS_BUILD_COMMAND is running. Unable to understand why this is happening...

Alternatively, you can call the gulp task with or without arguments and run the task internally on the argument condition.
I use yargs to make my life easier.
var args = require('yargs').argv;
gulp.task('some_task', function(){
if(args.condition){
// run your task
}
})
Call gulp like so: gulp some_task --condition=true or gulp some_task --condition=false
That allows you to maintain the condition outside of gulp

Related

gulp task with gulp-run-command doesn't work properly

I'm trying to run json-server in a gulp task and I'm checking if the server runs, with the function portInUse.
Like this:
var gulputil = require('gulp-util')
var run = require('gulp-run-command').default
var gulp = require('gulp')
const args = [
'json-server --watch .\\src\\main\\app\\reactjs\\api\\db.json --port 3005'
]
var net = require('net');
var portInUse = function(port, callback) {
var server = net.createServer(function(socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
server.listen(port, '127.0.0.1');
server.on('error', function (e) {
callback(true);
});
server.on('listening', function (e) {
server.close();
callback(false);
});
};
gulp.task("initLocalJsonServer", function() {
portInUse(3005, function(returnValue) {
gulputil.log('1 ' + returnValue);
});
run(args);
portInUse(3005, function(returnValue) {
gulputil.log('2 ' + returnValue);
});
});
That command that is the value of args here, works as intended when I run it in command line, so there's nothing wrong with the command or json-server itself. If I run it, I get to see the json-server at localhost:3005
Now the output from the portInUse function indicates that the server starts, as the output is:
[10:33:56] 1 false
[10:33:56] 2 true
But if I go to localhost:3005 after the gulp tasks are done, I can't see any server running. What might be the reason?

Sails.js + PM2 - process.send is undefined

I have a sails.js app. I am using pm2 in my production environment. According to this issue, I need to send a request from sails to pm2 indicating the app is online.
module.exports.bootstrap = function(cb) {
sails.on('lifted', function() {
process.send('ready') // process.send is undefined
});
};
How do I trigger the ready event here?
Please try:
var cp = require('child_process');
var p = cp.fork(__dirname + '/app');
sails.on('lifted', function() {
p.send('ready');
});
However, I use pm2 as well, and I just launch it like this for production without any code in the bootstrap.js file and keymetrics works just fine:
pm2 start app.js -x -- --prod
I hope this was helpful. Cheers

How to run protractor with different config file in sequence?

I have different configuration file for protractor, and I would like to create a gulp task which run protractor for each config file in sequence.
Here is my actual code:
gulp.src('conf/protractor.conf.*.js')
.pipe($.debug())
.pipe($.foreach(function(stream, file){
var configFileName = path.join('conf/', path.basename(file.path));
console.log(configFileName);
gulp.src(path.join(conf.paths.e2e, '/**/*.js'))
.pipe($.protractor.protractor({
configFile: configFileName,
args: args
}))
.on('error', function (err) {
// Make sure failed tests cause gulp to exit non-zero
console.log('Error catch by gulp');
throw err;
})
.on('end', function () {
// Close browser sync server
browserSync.exit();
done();
return stream;
});
}));
it run protractor only with the first configuration file and then stop, even if the different conf file were listed by foreach.
Does anyone has an idea of what I am missing?
Thanks
I found a workaround: instead of trying to use gulp only to do what I want to do, I created a little shell script.
Now, my gulp task take protractor config file path from command line argument like that:
var argv = require('minimist')(process.argv.slice(2));
if (!argv.conf || typeof argv.conf !== 'string' ) throw new Error('protractor configuration file path required');
function runProtractor (done) {
gulp.src(path.join(conf.paths.e2e, '/**/*.js'))
.pipe($.protractor.protractor({
configFile: argv.conf
}))
.on('error', function (err) {
// Make sure failed tests cause gulp to exit non-zero
throw err;
})
.on('end', function () {
// Close browser sync server
browserSync.exit();
done();
});
}
gulp.task('protractor', ['protractor:src']);
gulp.task('protractor:src', ['serve:e2e', 'webdriver-update'], runProtractor);
And I use a shell script to parse my folder and call gulp protractor for each configuration file:
#!/usr/bin/env bash
for filename in conf/protractor.conf.*.js; do
gulp protractor --conf=$filename
done
It works like a charm.

Ionic serve: how to rerun gulp task

I'm kinda new to ionic and gulp.
I was able to configure the ionic.project file in order to run the gulp tasks when I first run ionic serve.
But now when I change files I want that the gulp task will run again.. But this doesn't happen.. Is there a way to do that?
This is my ionic.project file:
{
"name": "test",
"app_id": "",
"gulpStartupTasks": [
"default"
],
"watchPatterns": [
"src/**/*",
"src/*",
"www/**/*",
"!www/lib/**/*"
]
}
I expected that when some file changes that match the wtachPatterns
it will invoke the gulp watch task, but this doesnt happen (I see that ionic see that the file has changed but nothing happen.)
this the the gulp watch task:
gulp.task('watch', function() {
gulp.watch(paths.sass, ['sass']);
gulp.watch(paths.script, ['script']);
});
Basically the task is minifying all the JS files and all the sass/scss files
and the index.html is looking on the minified files. so if the gulp task isn't invoked there are no changes in the minified file and I need to run ionic serve all over again.. Is there a proper way to do that?
UPDATE:
This is the complete gulpfile
var gulp = require('gulp');
var gutil = require('gulp-util');
var bower = require('bower');
require('require-dir')('./gulp/tasks');
var paths = {
sass: ['./scss/**/*.scss'],
style: ['./src/**/*.scss'],
script: ['./src/app.js'],
html:['./src/*.html']
};
gulp.task('default', ['sass', 'script','watch', 'html', 'style']);
gulp.task('watch', function() {
gulp.watch(paths.sass, ['sass']);
gulp.watch(paths.script, ['script']);
});
gulp.task('install', ['git-check'], function() {
return bower.commands.install()
.on('log', function(data) {
gutil.log('bower', gutil.colors.cyan(data.id), data.message);
});
});
gulp.task('git-check', function(done) {
if (!sh.which('git')) {
console.log(
' ' + gutil.colors.red('Git is not installed.'),
'\n Git, the version control system, is required to download Ionic.',
'\n Download git here:', gutil.colors.cyan('http://git- scm.com/downloads') + '.',
'\n Once git is installed, run \'' + gutil.colors.cyan('gulp install') + '\' again.'
);
process.exit(1);
}
done();
});
And this is an example of one of the files who have the actual task:
var browserify = require('browserify');
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var stringify = require('stringify');
var paths = ['./src/app.js'];
gulp.task('script', function() {
return browserify(paths, {debug: true})
.transform(stringify(['.html']))
.bundle()
.pipe(source('app.js'))
.pipe(gulp.dest('./www/js'));
});
Well So the problem was with paths I watched.
I removed the ./ and now its working
First thing first, you misunderstand the watchPatterns is for livereload, which means, the web will refresh if there is any file changed on watch. It's definitely not having any relation to gulp.
Read more at: http://ionicframework.com/docs/cli/test.html
To watch for file changes with watch, update your watch task, which is
gulp.task('watch', function() {
gulp.watch(paths.sass, ['sass']); <-- any file in paths.sass changed will trigger `gulp sass`
gulp.watch(paths.script, ['script']); <-- any file in paths.script changed will trigger `gulp script`
});
So if you want to watch more files to be processed by Gulp, just add tasks and watch them in gulp watch.
Oh hey, you are watching only files in ./scss/**/*.scss and ./src/app.js. Add more if you wish.

Karma exits with code 1 when it doesnt execute any spec tests

Karma test runs fine but exits with code 1 if 0 of 0 tests are run. Does anyone know how to return exit code 0 and normally exit in this case? Using gulp-karma which fails the task when no specs are run.
There is a configuration option that allows for empty test suites. Just add
failOnEmptyTestSuite: false
to your karma.conf.js and the process will exit with exit code 0.
BR
Chris
In your gulpfile, replace the "throw err" on the error callback in the your gulp test task with "this.emit('end')".
gulp.task('test', function() {
return gulp.src(testFiles)
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run'
}))
.on('error', function(err) {
throw err;
});
});
so your test task now looks like;
gulp.task('test', function() {
return gulp.src(testFiles)
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run'
}))
.on('error', function(err) {
this.emit('end');
});
});