link-writer.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. module.exports = LinkWriter
  2. var fs = require("graceful-fs")
  3. , Writer = require("./writer.js")
  4. , inherits = require("inherits")
  5. , path = require("path")
  6. , rimraf = require("rimraf")
  7. inherits(LinkWriter, Writer)
  8. function LinkWriter (props) {
  9. var me = this
  10. if (!(me instanceof LinkWriter)) throw new Error(
  11. "LinkWriter must be called as constructor.")
  12. // should already be established as a Link type
  13. if (!((props.type === "Link" && props.Link) ||
  14. (props.type === "SymbolicLink" && props.SymbolicLink))) {
  15. throw new Error("Non-link type "+ props.type)
  16. }
  17. if (props.linkpath === "") props.linkpath = "."
  18. if (!props.linkpath) {
  19. me.error("Need linkpath property to create " + props.type)
  20. }
  21. Writer.call(this, props)
  22. }
  23. LinkWriter.prototype._create = function () {
  24. // console.error(" LW _create")
  25. var me = this
  26. , hard = me.type === "Link" || process.platform === "win32"
  27. , link = hard ? "link" : "symlink"
  28. , lp = hard ? path.resolve(me.dirname, me.linkpath) : me.linkpath
  29. // can only change the link path by clobbering
  30. // For hard links, let's just assume that's always the case, since
  31. // there's no good way to read them if we don't already know.
  32. if (hard) return clobber(me, lp, link)
  33. fs.readlink(me._path, function (er, p) {
  34. // only skip creation if it's exactly the same link
  35. if (p && p === lp) return finish(me)
  36. clobber(me, lp, link)
  37. })
  38. }
  39. function clobber (me, lp, link) {
  40. rimraf(me._path, function (er) {
  41. if (er) return me.error(er)
  42. create(me, lp, link)
  43. })
  44. }
  45. function create (me, lp, link) {
  46. fs[link](lp, me._path, function (er) {
  47. // if this is a hard link, and we're in the process of writing out a
  48. // directory, it's very possible that the thing we're linking to
  49. // doesn't exist yet (especially if it was intended as a symlink),
  50. // so swallow ENOENT errors here and just soldier in.
  51. // Additionally, an EPERM or EACCES can happen on win32 if it's trying
  52. // to make a link to a directory. Again, just skip it.
  53. // A better solution would be to have fs.symlink be supported on
  54. // windows in some nice fashion.
  55. if (er) {
  56. if ((er.code === "ENOENT" ||
  57. er.code === "EACCES" ||
  58. er.code === "EPERM" ) && process.platform === "win32") {
  59. me.ready = true
  60. me.emit("ready")
  61. me.emit("end")
  62. me.emit("close")
  63. me.end = me._finish = function () {}
  64. } else return me.error(er)
  65. }
  66. finish(me)
  67. })
  68. }
  69. function finish (me) {
  70. me.ready = true
  71. me.emit("ready")
  72. if (me._ended && !me._finished) me._finish()
  73. }
  74. LinkWriter.prototype.end = function () {
  75. // console.error("LW finish in end")
  76. this._ended = true
  77. if (this.ready) {
  78. this._finished = true
  79. this._finish()
  80. }
  81. }