| 1 |
'use strict'; |
| 2 |
|
| 3 |
var os = require('os'); |
| 4 |
var path = require('path'); |
| 5 |
|
| 6 |
var fo = require('../../file-operations'); |
| 7 |
|
| 8 |
var isWindows = (os.platform() === 'win32'); |
| 9 |
|
| 10 |
function writeSymbolicLink(file, optResolver, onWritten) { |
| 11 |
if (!file.symlink) { |
| 12 |
return onWritten(new Error('Missing symlink property on symbolic vinyl')); |
| 13 |
} |
| 14 |
|
| 15 |
var isRelative = optResolver.resolve('relativeSymlinks', file); |
| 16 |
var flags = fo.getFlags({ |
| 17 |
overwrite: optResolver.resolve('overwrite', file), |
| 18 |
append: optResolver.resolve('append', file), |
| 19 |
}); |
| 20 |
|
| 21 |
if (!isWindows) { |
| 22 |
// On non-Windows, just use 'file' |
| 23 |
return createLinkWithType('file'); |
| 24 |
} |
| 25 |
|
| 26 |
fo.reflectStat(file.symlink, file, onReflect); |
| 27 |
|
| 28 |
function onReflect(statErr) { |
| 29 |
if (statErr && statErr.code !== 'ENOENT') { |
| 30 |
return onWritten(statErr); |
| 31 |
} |
| 32 |
|
| 33 |
// This option provides a way to create a Junction instead of a |
| 34 |
// Directory symlink on Windows. This comes with the following caveats: |
| 35 |
// * NTFS Junctions cannot be relative. |
| 36 |
// * NTFS Junctions MUST be directories. |
| 37 |
// * NTFS Junctions must be on the same file system. |
| 38 |
// * Most products CANNOT detect a directory is a Junction: |
| 39 |
// This has the side effect of possibly having a whole directory |
| 40 |
// deleted when a product is deleting the Junction directory. |
| 41 |
// For example, JetBrains product lines will delete the entire contents |
| 42 |
// of the TARGET directory because the product does not realize it's |
| 43 |
// a symlink as the JVM and Node return false for isSymlink. |
| 44 |
|
| 45 |
// This function is Windows only, so we don't need to check again |
| 46 |
var useJunctions = optResolver.resolve('useJunctions', file); |
| 47 |
|
| 48 |
var dirType = useJunctions ? 'junction' : 'dir'; |
| 49 |
// Dangling links are always 'file' |
| 50 |
var type = !statErr && file.isDirectory() ? dirType : 'file'; |
| 51 |
|
| 52 |
createLinkWithType(type); |
| 53 |
} |
| 54 |
|
| 55 |
function createLinkWithType(type) { |
| 56 |
// This is done after prepare() to use the adjusted file.base property |
| 57 |
if (isRelative && type !== 'junction') { |
| 58 |
file.symlink = path.relative(file.base, file.symlink); |
| 59 |
} |
| 60 |
|
| 61 |
var opts = { |
| 62 |
flags: flags, |
| 63 |
type: type, |
| 64 |
}; |
| 65 |
fo.symlink(file.symlink, file.path, opts, onSymlink); |
| 66 |
|
| 67 |
function onSymlink(symlinkErr) { |
| 68 |
if (symlinkErr) { |
| 69 |
return onWritten(symlinkErr); |
| 70 |
} |
| 71 |
|
| 72 |
fo.reflectLinkStat(file.path, file, onWritten); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
module.exports = writeSymbolicLink; |