Grunt观看事件与仅用于更改文件的Grunt复制

时间:2014-10-30 22:30:05

标签: gruntjs workflow grunt-contrib-watch grunt-contrib-copy

好的,我已经坚持了2周,所以希望其他人遇到这个问题。我试图使用Grunt只复制已更改的文件。我已经看过很多关于如何使用JSLINT和UGLIFY执行此操作的示例,但没有关于如何使用grunt-contrib-copy执行此操作的具体示例。

当您注册监视事件并将文件名传递给复制子任务时,可以访问文件名(我将其注销),但该文件永远不会正确复制。

我希望这是一件我能忽视的简单事情。有人可以看看我的代码,看看我做错了什么?

//Gruntfile.js:

module.exports = function(grunt) {
grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    options: {
        base: 'app',
        dist: 'dist',
    },

    copy: {
        changedFiles: {
            expand: true,
            dot: true,
            cwd: '<%= options.base %>',
            src: ['**/*.*'],
            dest: '<%= options.dist %>/'

        }
     },
    watch: {
        options: {
            nospawn: true,
            //debounceDelay: 1000,
        },
        css: {
            files: ['app/css/*.css',
                   'app/js/*.js'
                   ],
            tasks: ['copy:changedFiles'],

        }
    }    

});

grunt.event.on('watch', function(action, filepath, target){
    grunt.log.writeln('target: ', target + '\n filepath: ' + filepath + '\n action: has ' + action);
    grunt.config('copy.changedFiles.src', new Array(filepath) );
});

//load our copy task
grunt.loadNpmTasks('grunt-contrib-copy');

//load our watch task
grunt.loadNpmTasks('grunt-contrib-watch');

grunt.registerTask('copyChangedFiles', [
        'watch:css'
]);

};

基本上我的文件夹设置如下:

-app
| - css
| - js
-dist

我正在观看应用文件夹并尝试复制应用目录中更改的文件并将其复制到dist目录。动态修改副本src似乎无法正常工作。

使用watch而不是watch事件自行运行的复制任务可以正常运行并复制每个文件,但我只对复制的文件感兴趣。

我也在我的观看活动中尝试过这种变体,但无济于事:

var copyDest = filepath.replace(grunt.config('copy.changedFiles.dest'), '');
var copyCwd = filepath.replace(grunt.config('copy.changedFiles.cwd'), '');
grunt.config('copy.changedFiles.cwd' , copyCwd);
grunt.config(['copy', 'changedFiles', 'src'] , [filepath]);

有没有人在使用grunt副本之前成功完成此操作?或者我应该使用另一项任务?我已经尝试过同样的grunt-sync,但似乎也没有用。我被卡住了。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

我能够使用这个SO答案:How to modify grunt watch tasks based on the file changed?并根据我的需要进行修改。我现在只能复制更改的文件。

我的手表现在看起来像这样:

 var path = require('path');

 grunt.event.on('watch', function(action, filepath, target){
    grunt.log.writeln(target + ': ' + filepath + ' might have ' + action);
    var siteDirectory = path.dirname(filepath);

    //changes changed file source to that of the changed file
    var option = 'copy.changedFiles.src';
    var result = filepath;
    grunt.log.writeln(option + ' changed to ' + result);
    grunt.config(option, result);

    //customizes output directory so that file goes to correct place
    option = 'copy.changedFiles.dest';
    result = path.resolve(__dirname + '/dist');
    grunt.log.writeln(option + ' changed to ' + result);
    grunt.config(option, result);

});

现在运行grunt copyChangedFiles会在app目录中查看更改,每次修改*.css*.js文件时,都会将其复制到dist目录

我真的希望这可以帮助别人,因为我花了两周的时间让它正常工作。

答案 1 :(得分:1)

您应该可以使用grunt-newer包。我唯一注意到的是,如果文件从源中删除并且当前位于副本的目标位置,则它不会执行删除操作。

但是,对于大多数用途,这应该执行您正在寻找的任务。手表将在文件更改时触发,只有当目标中的文件早于src时才会运行。

注意:nospawn已弃用,现在为spawn。这是为了向后兼容。

我不确定文件是否有意义:[<pattern>]与复制任务中描述的src模式不匹配。

module.exports = function(grunt) {
grunt.initConfig({
    options: {
        base: 'app',
        dist: 'dist',
    },
    copy: {
        changedFiles: {
            expand: true,
            dot: true,
            cwd: '<%= options.base %>',
            src: ['**/*.*'],
            dest: '<%= options.dist %>/'
        }
     },
    watch: {
        options: {
            //nospawn is depricated but kept for compatibility.  use spawn false instead
            spawn: false,
            cwd: '<%= options.base %>'
            //debounceDelay: 1000,
        },
        css: {
            //should match above
            files: ['**/*.*'],
            //On new file detection run copy:changedFiles
            tasks: ['newer:copy:changedFiles']
        }
    }     
});

grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-newer');

grunt.registerTask('copyChangedFiles', ['watch:css']);

};
相关问题