Gruntjs Grunt监视事件,Grunt复制仅用于更改的文件

Gruntjs Grunt监视事件,Grunt复制仅用于更改的文件,gruntjs,workflow,grunt-contrib-watch,grunt-contrib-copy,Gruntjs,Workflow,Grunt Contrib Watch,Grunt Contrib Copy,好吧,我已经被困在这个问题上两个星期了,希望这里的其他人也遇到了这个问题。我正在尝试使用Grunt仅复制已更改的文件。我已经看到了许多关于如何使用JSLINT和UGLIFY实现这一点的示例,但是没有关于如何使用grunt contrib copy实现这一点的具体示例 注册监视事件并将文件名传递给复制子任务时,文件名是可访问的(我正在注销它),但文件永远不会正确复制 我希望这是一件我忽略的简单事情。有人能看看我的代码,看看我做错了什么吗 //Gruntfile.js: module.export

好吧,我已经被困在这个问题上两个星期了,希望这里的其他人也遇到了这个问题。我正在尝试使用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'
]);

};
我正在监视应用程序文件夹,试图复制应用程序目录中更改的文件,并将其复制到dist目录。动态修改副本src似乎不起作用

当使用watch而不是watch事件单独运行copy任务时,它可以很好地复制每个文件,但我只对复制更改的文件感兴趣

在我的手表活动中,我也尝试过这种变化,但没有效果:

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 copy之前,是否有人成功地做到了这一点?还是我应该使用另一个任务?我也尝试过grunt同步,但似乎也不起作用。我卡住了


谢谢你的帮助。

我能够使用这个SO答案:并修改它以满足我的需要。我现在只能复制已更改的文件

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

 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
将监视应用程序目录的更改,并且无论何时修改
*.css
*.js
文件,它都会将其复制到
dist
目录


我真的希望这对其他人有帮助,因为我花了两周的时间让它正常工作。

您应该能够使用该软件包。我注意到的唯一一点是,如果文件从源中删除并且当前位于副本的目标中,它不会执行删除操作

然而,在大多数情况下,这应该执行您正在寻找的任务。文件更改时会触发监视,只有当目标中的文件比
src
旧时,更新的才会运行

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

我不确定文件:
[]
与复制任务中描述的
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']);

};
module.exports=函数(grunt){
grunt.initConfig({
选项:{
base:'应用程序',
地区:'地区',
},
副本:{
更改文件:{
是的,
多特:没错,
cwd:“”,
src:['****.'],
目的地:'/'
}
},
观察:{
选项:{
//nospawn是去润滑的,但为了兼容而保留。请改用spawn false
产卵:假,
cwd:'
//debounceDelay:1000,
},
css:{
//应该与上面的匹配
文件:['****.'],
//在检测新文件时运行copy:changedFiles
任务:['newer:copy:changedFiles']
}
}     
});
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks(“grunt-contrib-watch”);
grunt.loadNpmTasks('grunt-newer');
registerTask('copyChangedFiles',['watch:css']);
};
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']);

};