使用Grunt.js副本将所有文件从目录复制到另一个目录 [英] Copy all files from directory to another with Grunt.js copy

查看:356
本文介绍了使用Grunt.js副本将所有文件从目录复制到另一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将目录中的所有文件复制到另一个目录,作为我的构建过程的一部分。它适用于我明确指定的单个文件,但是当我尝试复制整个目录时,它会执行奇怪的操作,例如复制完整的目录结构(或根本没有任何内容)。以下是我的GruntFile.js中的相关部分:

 复制:{
myvoice:{
files :[
{src:src / html / index.html,dest:dist / myvoice / index.html},
{src:src / html / css / style.css ,dest:dist / myvoice / css / style.css},
{src:src / html / js / require.js,dest:dist / myvoice / js / require.js},
{src:build / myvoice / main.js,dest:dist / myvoice / js / main.js},
{src:src / html / css / fonts / * ,dest:dist / myvoice / css / fonts /}
]
}
},

具体来说,这是我无法工作的最后一行:

  {src: src / html / css / fonts / *,dest:dist / myvoice / css / fonts /} 


>解决方案 :true 选项,如这个答案可能适用于某些情况,但在我看来,更常见的要求(如我的情况)是将文件夹及其子文件夹结构原样复制到 dest 。看来在大多数情况下,如果你有子文件夹,他们可能在代码中被这样引用。执行此操作的关键是 cwd 选项,该选项将保留相对于指定工作目录的文件夹结构:

<$ p
files:{
cwd:'path / to / files',//设置工作文件夹/ root复制
src:'* * / *',//复制所有文件和子文件夹
dest:'dist / files',//目标文件夹
展开:true //使用cwd时需要
}
}


I'm trying to copy all the files in a directory to another directory as part of my build process. It works fine for individual files that I specify explicitly but when I try to copy the whole directory it does weird things like copies the full directory structure (or nothing at all). Here is the relevant part from my GruntFile.js:

copy: {
  myvoice: {
    files: [
      { src:"src/html/index.html", dest:"dist/myvoice/index.html" },
      { src:"src/html/css/style.css", dest:"dist/myvoice/css/style.css" },
      { src:"src/html/js/require.js", dest:"dist/myvoice/js/require.js" },
      { src:"build/myvoice/main.js", dest:"dist/myvoice/js/main.js" },
      { src:"src/html/css/fonts/*", dest:"dist/myvoice/css/fonts/" }
    ]
  }
},

Specifically it's the last line that I can't get to work:

      { src:"src/html/css/fonts/*", dest:"dist/myvoice/css/fonts/" }

解决方案

The flatten: true option as in this answer might work for some cases, but it seems to me that the more common requirement (as in my case) is to copy a folder and its sub-folder structure, as-is, to dest. It seems that in most cases if you have sub-folders, they are probably being referenced that way in code. The key to doing this is the cwd option, which will preserve folder structure relative to the specified working directory:

copy: {
  files: {
    cwd: 'path/to/files',  // set working folder / root to copy
    src: '**/*',           // copy all files and subfolders
    dest: 'dist/files',    // destination folder
    expand: true           // required when using cwd
  }
}

这篇关于使用Grunt.js副本将所有文件从目录复制到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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