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