2017-03-01 13 views
0

我正尝试使用Gulp建立Cordova开发/部署链。我以这个gulpfile.js结束了,但是我并不是很满意,因为我需要杀死“gulp watch”任务才能运行“gulp deploy”任务。Cordova/Gulp最佳实践

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开发和缺陷科尔多瓦项目的最佳实践吗?我认为这个问题是在“准备”任务:它永远不会返回,可能是由于一个吞噬执行问题,但我真的不知道如何调试它。回到顶端这篇文章中的信息适用于:

+0

你打算怎么做? 'watch'不会把控制权交还给你..你想在每次任何文件改变时触发部署吗? –

+0

我想在每次更改任何文件时触发“准备”,并在准备将事物推送到服务器,apk或ipa时手动触发“部署”。实际上,如果我在运行gulp手表时运行“gulp deploy”,它什么也不做,也许是因为“准备”突然覆盖了js缩小的文件。 – g4b0

+0

这可能是原因,尝试使用单独的目标来执行不同的任务,还要确保deploy任务的目标文件夹中的更改没有被prepare - this line - >'gulp.watch('www/* */*',['prepare']);' –

回答

0

从我所了解的唯一问题是,gulp命令不返回控制,以您为执行gulp deploy

的准备工作永远不会返回,因为watch功能的行为 - 你已经通过控制到watch这些文件,因此只有在您停止观看时才会返回。这是预期的行为,而不是probably due a gulp-exec issue

我会采取在这种情况下的解决方案是在后台运行的gulp任务,使用本机nohup gulp &使得watching移动到后台为我执行deploy

+0

它不起作用。同样在后台'gulp deploy'中运行的nohup和gulp不要缩小js脚本。我注意到的一件事是文件修改时间的改变,所以看起来'gulp deploy'完成了它的工作,但是突然'gulp watch'覆盖了它们。 – g4b0