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