我如何根据相应文件的父文件夹名称使用Grunt重命名文件? [英] How can I rename files with Grunt, based on the respective file's parent folder name?

查看:149
本文介绍了我如何根据相应文件的父文件夹名称使用Grunt重命名文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构:

  src / 
modules /
module1 /
js /
main.js
scss /
main.scss
index.html
module2 /
js /
main.js
scss /
main.scss
index.html

I我想运行一个咕task任务将它们复制到以下结构中:

$ $ $ $ $ $ $ $ $
$ / $
module1.js
module2.js
css /
module1.css
module2.css
module1.html
module2.html

有没有办法用现有的grunt插件做到这一点?如果没有,我怎么能做到这一点?

解决方案

这可以使用 grunt-contrib-copy 插件。



主要要注意的是,您可以更改目标编程方式是使用重命名函数(它接受每个文件的目的地和源文件)。

这是一个(稍微脆弱的)示例 Gruntfile。

  module.exports = function(grunt){$ js 应该复制到您想要的结构中: b $ b //项目配置。 
grunt.initConfig({
copy:{
main:{
files:[
{
展开:true,
cwd:' src / modules /',
src:['** / *。js'],
dest:'dev / js /',
rename:function(dest,src){
//使用源目录创建文件
//以你的目录结构为例
// dest ='dev / js /'
// src ='module1 / js /main.js'
return dest + src.substring(0,src.indexOf('/'))+'.js';
}
},
{
expand:true,
cwd:'src / modules /',
src:['** / *。scss'],
dest:'dev / css /',
rename:function(dest,src){
return dest + src.substring(0,src.indexOf('/'))+'.css';
}
},
{
expand:true,
cwd:'src / modules /',
src:['** / *。html'],
dest:'dev /',
rename:function(dest,src){
return dest + src.substring(0,src.indexOf('/'))+'.html';
}
}
]
}
}
});

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

//默认任务。
grunt.registerTask('default',['copy']);
};


I have a the following structure:

src/
    modules/
        module1/
            js/
                main.js
            scss/
                main.scss
            index.html
        module2/
            js/
                main.js
            scss/
                main.scss
            index.html

I'd like to run a grunt task to copy these out to the following structure:

dev/
    js/
        module1.js
        module2.js
    css/
        module1.css
        module2.css
    module1.html
    module2.html

Is there a way to do this with an existing grunt plugin? If not, how could I achieve this?

解决方案

This can be done using the grunt-contrib-copy plugin.

The main thing to note is that you can change the destination programmatically by using a rename function (which takes in the destination and source of each file).

Here is a (somewhat brittle) sample Gruntfile.js that should copy to your desired structure:

module.exports = function(grunt) {
  // Project configuration.
  grunt.initConfig({
    copy: {
      main: {
        files: [
          {
            expand: true, 
            cwd: 'src/modules/', 
            src: ['**/*.js'], 
            dest: 'dev/js/', 
            rename: function(dest, src) {
              // use the source directory to create the file
              // example with your directory structure
              //   dest = 'dev/js/'
              //   src = 'module1/js/main.js'
              return dest + src.substring(0, src.indexOf('/')) + '.js';
            }
          },
          {
            expand: true, 
            cwd: 'src/modules/', 
            src: ['**/*.scss'], 
            dest: 'dev/css/', 
            rename: function(dest, src) {
              return dest + src.substring(0, src.indexOf('/')) + '.css';
            }
          },
          {
            expand: true, 
            cwd: 'src/modules/', 
            src: ['**/*.html'], 
            dest: 'dev/', 
            rename: function(dest, src) {
              return dest + src.substring(0, src.indexOf('/')) + '.html';
            }
          }
        ]
      }
    }
  });

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

  // Default task(s).
  grunt.registerTask('default', ['copy']);
};

这篇关于我如何根据相应文件的父文件夹名称使用Grunt重命名文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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