我有一个目录,其中包含许多文件和一个(或多个)symlinkname -> .
符号链接(symbolic link)。我想将整个目录的内容复制到新位置。下面的代码可以很好地复制所有内容,但它会跳过符号链接(symbolic link)。添加 globOptions.follow = true 只会使其无限循环,这是有道理的,因为它会尝试取消引用它。我怎样才能让它复制所有内容+符号链接(symbolic link)而不尝试跟随它们?
this.fs.copy(
this.destinationPath() + '/**',
this.destinationPath('build/html'),
{
globOptions: {
follow: true // This will make the copy loop infinitely, which makes sense.
}
}
});
最佳答案
在发现 Yeoman 通过排除对符号链接(symbolic link)的支持来避免糟糕的用户体验后(请参阅 Simon Boudrias 的评论),我知道我必须解决这个问题。我做了以下解决方法,请注意,只有当您无法像我一样避免符号链接(symbolic link)时才应应用此方法。
var fs = require('fs');
// Find out if there are symlinks
var files = fs.readdirSync(this.destinationPath());
for (var i = 0; i < files.length; i++) {
var path = this.destinationPath(files[i]),
stats = fs.lstatSync(path);
if (stats.isSymbolicLink()) {
// Find the target of the symlink and make an identical link into the new location
var link = fs.readlinkSync(path);
fs.symlinkSync(link, this.destinationPath('build/html/' + files[i]));
}
}
关于node.js - mem-fs-编辑器 : How to copy relative symlinks?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34181552/