node.js - mem-fs-编辑器 : How to copy relative symlinks?

标签 node.js yeoman yeoman-generator

我有一个目录,其中包含许多文件和一个(或多个)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/

相关文章:

node.js api 保持代码干燥

node.js - 禁用 Socket.io 中特定套接字的输出

javascript - Yeoman 发电机,咕噜声 : proper usage of grunt-usemin with grunt-rev

angularjs - 如何创建新的应用程序项目而不需要一次又一次下载和安装 npm 模块?

javascript - Yeoman.io 在安装 generator-angular 时出现问题

javascript - json - 一次在同一个 Node 上添加 2 个元素? (或者在不存在的 Node 中)

javascript - 仅 GET/POST 模型 Sails.js 的优势

gruntjs - 配置 grunt bower-install 时遇到问题

yeoman - 在 Yeoman Generator 中创建空目录

javascript - Yeoman + Grunticon : Gruntfile. js 问题