Cordova / Gulp最佳实践

时间:2017-03-01 10:21:19

标签: javascript cordova deployment gulp

我正在尝试使用Gulp设置Cordova开发/部署链。我结束了这个gulpfile.js,但是我真的不满意,因为我需要杀掉“gulp watch”任务才能运行“gulp deploy”任务。

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']);

这可能是使用Gulp开发和拯救Cordova项目的最佳实践?

[编辑] 我认为问题在于“准备”任务:它永远不会返回,可能是因为gulp-exec问题,但我真的不知道如何调试它。

1 个答案:

答案 0 :(得分:0)

据我所知,唯一的问题是gulp命令无法将控制权交给您执行gulp deploy

由于watch功能的行为,准备任务永远不会返回 - 您已将控件传递给watch文件,因此只有在您停止观看时才会返回。这是预期的行为而不是probably due a gulp-exec issue

我在这种情况下采用的解决方案是使用原生nohup gulp &在后台运行gulp任务,以便将watching移到后台让我执行deploy