link-writer.js 2.8 KB

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