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