javascript 使用 Grunt.js copy 将所有文件从目录复制到另一个目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18966485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy all files from directory to another with Grunt.js copy
提问by Evan Hobbs
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:
作为构建过程的一部分,我正在尝试将目录中的所有文件复制到另一个目录。它适用于我明确指定的单个文件,但是当我尝试复制整个目录时,它会做一些奇怪的事情,比如复制完整的目录结构(或根本没有)。这是我的 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/" }
回答by Brian Moeskau
The flatten: true
option as in this answermight 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:
此答案中的flatten: true
选项可能适用于某些情况,但在我看来,更常见的要求(如我的情况)是将文件夹及其子文件夹结构按原样复制到. 似乎在大多数情况下,如果您有子文件夹,它们可能会在代码中以这种方式被引用。这样做的关键是选项,它将保留相对于指定工作目录的文件夹结构:dest
cwd
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
}
}
回答by Ben
This task will maintain folder structure if you specify a file glob. What you want is the flatten
option which will remove the structure.
如果您指定文件 glob,此任务将维护文件夹结构。您想要的是flatten
删除结构的选项。
{
expand: true,
flatten: true,
src: ['src/html/css/fonts/**'],
dest: 'dist/myvoice/css/fonts/',
filter: 'isFile'
}
Find the rest of the available options in the Github repo. Hope this helps.
在Github 存储库中找到其余的可用选项。希望这可以帮助。
回答by Jorge Bucaran
I would like to add that changing the format of the globin srcwill modify how the copy works.
我想补充一点改变的格式水珠在SRC将修改副本是如何工作的。
As pointed out by bmoeskauabove, the following will copy everythinginside dist/
and move it to path/to/dir
(overwriting the destination if it already exists).
正如上面bmoeskau所指出的,以下内容将复制里面的所有内容dist/
并将其移动到path/to/dir
(如果目标已经存在,则覆盖它)。
copy: {
files: {
expand: true,
dest: 'path/to/dir',
cwd: 'dist/',
src: '**'
}
}
Note however, that:
但是请注意:
copy: {
files: {
expand: true,
dest: 'path/to/dir',
cwd: 'dist/',
src: '*'
}
}
Will only copy files inside dist/
as well as directories, but will notcopy the contents of those directories to the destination.
只会复制里面的文件dist/
和目录,但不会将这些目录的内容复制到目的地。
Also, the following with src: '*/*'
will onlycopy directories with contents inside dist/
. That is, files just inside dist/
will not be copied.
此外,在下面src: '*/*'
将只复制目录里面的内容dist/
。也就是说,里面的文件dist/
不会被复制。
copy: {
files: {
expand: true,
dest: 'path/to/dir',
cwd: 'dist/',
src: '*/*'
}
}
Finally, same as above, but src: '**/**'
will copy onlyfiles inside dist/
as well as files inside dist/
subdirectories to path/to/dir
. So there will be no folders inside the destination.
最后,如上面一样,但src: '**/**'
会复制只内的文件dist/
以及文件里面dist/
子目录path/to/dir
。所以目的地内不会有文件夹。
copy: {
files: {
expand: true,
dest: 'path/to/dir',
cwd: 'dist/',
src: '*/*',
flatten: true,
filter: 'isFile'
}
}
回答by Saschlong
Had to use egdy instead curly braces for the files segment (in Coffeescript)...
必须使用 egdy 而不是花括号作为文件段(在 Coffeescript 中)...
copy: {
files: [
cwd: 'path/to/files'
src: '**/*'
dest: 'dist/files'
expand: true
]
}
回答by LearnToday
If you are developing with angular yeoman , then this is the better way to copy with grunt. expand: true is required when using cwd. <%= yeoman.app %> is just the app route ('.').
如果您正在使用 angular yeoman 进行开发,那么这是使用 grunt 进行复制的更好方法。expand: 使用 cwd 时需要 true。<%= yeoman.app %> 只是应用程序路径 ('.')。
{
expand: true,
cwd: '<%= yeoman.app %>/data',
dest: '<%= yeoman.dist %>/data',
src: ['**']
}