【发布时间】:2018-02-24 22:09:32
【问题描述】:
我正在尝试使用 grunt-run 和 curl 将文件上传到更改时的服务器。如果我将文件名硬编码到实际任务中,我可以让它工作,但我试图根据更改的文件运行它......到目前为止,这是我的 grunt 文件(精简到与此相关的部分问题)。
module.exports = function(grunt) {
var config = require('./config.json');
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
less : {
files : ['front-end/less/**/*.less'],
tasks : ['newer:less','sync:css','run:deploy_less_files']
},
},
less: {
development: {
options: {
paths: ['front-end/_builds/css'],
sourceMap : true,
},
files: [{
cwd: "front-end/less",
expand : true,
src : [ '**/*.less', '!_settings.less'],
dest : "front-end/_builds/css",
ext: ".css"
}]
},
},
sync : {
target: {},
css : {
expand: true,
flatten :true,
verbose: true,
cwd : "front-end/_builds/css/",
src : "**/*.css",
dest : "target/css/"
},
},
run : {
deploy_less_files : {}
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-sync');
grunt.loadNpmTasks('grunt-run');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-newer');
grunt.registerTask('default', ['watch']);
grunt.event.on('watch', function(action, filepath, target) {
if (target == "less") {
grunt.config.set('run.deploy_less_files.exec','curl -u ' + config.credentials.user + ':' + config.credentials.pw + ' -T ' + filepath + ' http://localhost:8080/assets/less/');
grunt.task.run("run:deploy_less_files");
}
});
}
这是我要按顺序执行的操作:
- 查看 /front-end/less 中的所有 LESS 文件
- 如果文件发生变化,编译成css并放在front-end/_builds/css目录下
- 将 front-end/_builds/css 的内容与我的 target/css 目录同步。
- 通过 curl 将文件上传到我的本地主机。
理想情况下,我只想从我的目标或 _builds 目录中获取 css 文件并将其上传到我的本地主机,但如果我能让这部分工作,我可以解决这个问题。
任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: javascript gruntjs