Grunt - 如何观看和同步文件的删除 [英] Grunt - how to watch and synchronize files' deletion

查看:96
本文介绍了Grunt - 如何观看和同步文件的删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Grunt的新用户。目前我有一个包含图像源文件(.psd)和图像文件(.png,.jpg等)的文件夹 static_src / img 。此文件夹不公开。相反,我想只将图像文件的更改同步到另一个公用文件夹 static / img



问题是 - 它在我添加/更改 static_src / img 中的图像文件时运行良好,但我不知道如何在删除文件时同步更改。 Grunt-contrib-watch可以在 static_src / img 中检测到删除,但我不知道如何删除 static / img code>。我尝试了grunt-contrib-clean,但它似乎不适合我,也许我没有正确使用它。



我的Gruntfile.js是:

  module.exports = function(grunt){
grunt.initConfig({
pkg:grunt.file.readJSON('package .json'),
copy:{
dev:{
files:[
{expand:true,cwd:'static_src / img /',src:['** /*.{png,jpg,gif}'],dest:'static / img /'}
]
}
},
clean:{
dev :{
src:['static_src / img / ** / *。png']
}
},
观看:{
复制:{
文件:['static_src / img / ** / *。{png,jpg,gif}'],
任务:['copy'],
选项:{
event: 'added','changed'],
}
},
删除:{'b $ b files:['static_src / img / ** / *。{png,jpg,gif }'],
任务:['clean'],
options:{
event:['删除']
}
}
}
});

grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-watch');
};

那么如何删除grunt-contrib-watch任务中的特定文件?感谢您的帮助!

解决方案

对于已删除的事件,您可以删除static / img中的所有文件,并将其余文件复制到static_src / img to static / img。

  module.exports = function(grunt){
grunt.initConfig({
pkg:grunt.file.readJSON('package.json'),
copy:{
dev:{
files:[
{expand:true,cwd:' static_src / img /',src:['** / *。{png,jpg,gif}'],dest:'static / img /'}
]
}
},
clean:{
dev:{
src:['static / img / ** / *。{png,jpg,gif}'] //将它从static_src更改为static
$ b b

$ b $ {b $ b files:['static_src / img / ** / *。{png,jpg,gif}'],
任务:['copy'],
选项:{
事件:['added','changed'],
}
},
删除:{
文件:['static_src / i
任务:['clean','copy'],//在干净
选项后添加了复制任务:{
事件:['deleted']
}
}
}
});

grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-watch');
};

我想这可能与您希望删除特定文件的内容不同,但是按预期工作。

I am a new user of Grunt. Currently I have a folder static_src/img that contains image source files (.psd) and image files (.png, .jpg, etc.). This folder is not public. Instead, I want to sync the change of only image files to another public folder static/img.

The problem is - it works well when I add/change a image file in static_src/img, but I don't know how to sync the change when I delete a file. Grunt-contrib-watch can detect the deletion in static_src/img, but I don't know how to delete the file in static/img. I tried grunt-contrib-clean, but it seems not work for me, maybe I used it not properly.

My Gruntfile.js is:

module.exports = function(grunt){
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    copy: {
      dev: {
        files: [
          {expand: true, cwd: 'static_src/img/', src:['**/*.{png,jpg,gif}'], dest: 'static/img/'}
        ]
      }
    },
    clean: {
      dev: {
        src: ['static_src/img/**/*.png']
      }
    },
    watch: {
      copy: {
        files: ['static_src/img/**/*.{png,jpg,gif}'],
        tasks: ['copy'],
        options: {
          event: ['added', 'changed'],
        }
      },
      remove: {
        files: ['static_src/img/**/*.{png,jpg,gif}'],
        tasks: ['clean'],
        options: {
          event: ['deleted']
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-watch');
};

So how to delete a specific file in a grunt-contrib-watch task? Thank you for help!

解决方案

For deleted event, you can delete all files in static/img and copy remaining files in static_src/img to static/img.

module.exports = function(grunt){
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    copy: {
      dev: {
        files: [
          {expand: true, cwd: 'static_src/img/', src:['**/*.{png,jpg,gif}'], dest: 'static/img/'}
        ]
      }
    },
    clean: {
      dev: {
        src: ['static/img/**/*.{png,jpg,gif}']    // Changed this from static_src to static
      }
    },
    watch: {
      copy: {
        files: ['static_src/img/**/*.{png,jpg,gif}'],
        tasks: ['copy'],
        options: {
          event: ['added', 'changed'],
        }
      },
      remove: {
        files: ['static_src/img/**/*.{png,jpg,gif}'],
        tasks: ['clean', 'copy'],    // Added copy task after clean
        options: {
          event: ['deleted']
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-watch');
};

I guess this might be different from what you expected to delete a specific file, but works as expected.

这篇关于Grunt - 如何观看和同步文件的删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆