0

I'm trying to setup a Cordova develop/deployment chain using Gulp. I ended with this gulpfile.js, but I'm not really satisfied since I need to kill "gulp watch" task in order to run "gulp deploy" task.

var gulp = require('gulp'),
gutil = require('gulp-util'),
exec = require('gulp-exec');
var spawn = require('child_process').spawn;
var stripDebug = require('gulp-strip-debug');
var uglify = require('gulp-uglify');

/**
* Config ogj
*/
var config = {
    jsDir: 'www/assets/js',
    jsDirBrowser: 'platforms/browser/www/assets/js',
    production: !!gutil.env.production
};

/**
* Automatically run 'cordova prepare browser' after any modification 
* into the www directory - really useful for development/deplyment purpose
* 
* @see watch task
*/
gulp.task('prepare', function () {
    gutil.log('Prepare browser');
    var options = {
        continueOnError: false, // default = false, true means don't emit error event 
        pipeStdout: false, // default = false, true means stdout is written to file.contents 
        customTemplatingThing: "test" // content passed to gutil.template() 
    };
    var reportOptions = {
        err: true, // default = true, false means don't write err 
        stderr: true, // default = true, false means don't write stderr 
        stdout: true // default = true, false means don't write stdout 
    }
    return gulp.src('./**/**')
    .pipe(exec('cordova prepare browser', options))
    .pipe(exec.reporter(reportOptions));

});

/**
* Watch for changes in www
*/
gulp.task('watch', function () {
    gulp.watch('www/**/*', ['prepare']);
});

/**
* Default task
*/
gulp.task('default', ['prepare']);


/**
* Javascript production depolyment.
*/
gulp.task('deploy-js', function () {
    gutil.log('Deploy');
    return gulp.src(config.jsDir + '/*.js')
    .pipe(stripDebug())
    .pipe(uglify())
    .pipe(gulp.dest(config.jsDirBrowser));
});

/**
* Production deployment
* To be run before uploading files to the server with no gulp instaces running
*/
gulp.task('deploy', ['deploy-js']);

Which could be a best practice for develop and deply a Cordova project using Gulp?

[EDIT] I think the problem is in the "prepare" task: it never returns, probably due a gulp-exec issue, but I really don't know how to debug it.

g4b0
  • 929
  • 1
  • 8
  • 21
  • what is the behavior you are going for ? `watch` does not return control to you.. do you want to trigger deploy every time any file changes ? – Gulfaraz Rahman Mar 01 '17 at 10:36
  • I would like to trigger "prepare" every time any file changes, and manually trigger "deploy" when I'm ready to push things to the server, apk or ipa. Actually if I run "gulp deploy" when a gulp watch is running it does nothing, maybe because the "prepare" suddenly overwrite js minified files. – g4b0 Mar 01 '17 at 11:25
  • That's probably the reason, try using separate destinations for the different tasks, also make sure that the changes in the destination folder of the deploy task are not being watched by the prepare - this line -> `gulp.watch('www/**/*', ['prepare']);` – Gulfaraz Rahman Mar 02 '17 at 07:13
  • The destination directory is already outside the watched one, because it's in `platforms/browser/www/assets/js`, not in `www/**/*`. Separate destination for different tasks is not an option, since I would like to fire automagically `cordova prepare browser` on every file modification into `www/**/*`, including changes in `www/assets/js`. On the other hand I would like to run `gulp deploy` manually only when I'm ready, and it will write minified js files into `platforms/browser/www/assets/js`, overwriting the ones written by `cordova prepare browser` – g4b0 Mar 02 '17 at 07:55

1 Answers1

0

From what I've understood the only issue is that gulp command does not return control to you for you to execute gulp deploy

the prepare task never returns because of the behavior of the watch feature - you've passed control to watch the files so it will return only when you stop watching. It is the expected behavior and not a probably due a gulp-exec issue.

The solution I would adopt in this situation is to run the gulp task in the background, using the native nohup gulp & so that the watching is moved to the background for me to execute deploy.

Community
  • 1
  • 1
Gulfaraz Rahman
  • 417
  • 4
  • 13
  • It doesn't work. Also with nohup and gulp running in background `gulp deploy` don't minify js scripts. One thing I've noticed is that files modification time change, so it seems that `gulp deploy` does its work, but then suddenly `gulp watch` overwrite them. – g4b0 Mar 02 '17 at 15:29