(src, dst, type)
| 7 | var isWin = process.platform === 'win32'; |
| 8 | |
| 9 | function createLink(src, dst, type) { |
| 10 | var dstDir = path.dirname(dst); |
| 11 | |
| 12 | // Create directory |
| 13 | return ( |
| 14 | Q.nfcall(mkdirp, dstDir) |
| 15 | // Check if source exists |
| 16 | .then(function() { |
| 17 | return Q.nfcall(fs.stat, src).fail(function(error) { |
| 18 | if (error.code === 'ENOENT') { |
| 19 | throw createError( |
| 20 | 'Failed to create link to ' + path.basename(src), |
| 21 | 'ENOENT', |
| 22 | { |
| 23 | details: |
| 24 | src + |
| 25 | ' does not exist or points to a non-existent file' |
| 26 | } |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | throw error; |
| 31 | }); |
| 32 | }) |
| 33 | // Create symlink |
| 34 | .then(function(stat) { |
| 35 | type = type || (stat.isDirectory() ? 'dir' : 'file'); |
| 36 | |
| 37 | return Q.nfcall(fs.symlink, src, dst, type).fail(function(err) { |
| 38 | if (!isWin || err.code !== 'EPERM') { |
| 39 | throw err; |
| 40 | } |
| 41 | |
| 42 | // Try with type "junction" on Windows |
| 43 | // Junctions behave equally to true symlinks and can be created in |
| 44 | // non elevated terminal (well, not always..) |
| 45 | return Q.nfcall(fs.symlink, src, dst, 'junction').fail( |
| 46 | function(err) { |
| 47 | throw createError( |
| 48 | 'Unable to create link to ' + |
| 49 | path.basename(src), |
| 50 | err.code, |
| 51 | { |
| 52 | details: |
| 53 | err.message.trim() + |
| 54 | '\n\nTry running this command in an elevated terminal (run as root/administrator).' |
| 55 | } |
| 56 | ); |
| 57 | } |
| 58 | ); |
| 59 | }); |
| 60 | }) |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | module.exports = createLink; |
no test coverage detected
searching dependent graphs…