MCPcopy
hub / github.com/bower/bower / createLink

Function createLink

lib/util/createLink.js:9–62  ·  view source on GitHub ↗
(src, dst, type)

Source from the content-addressed store, hash-verified

7var isWin = process.platform === 'win32';
8
9function 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
64module.exports = createLink;

Callers 3

linkSelfFunction · 0.85
linkToFunction · 0.85
createLink.jsFile · 0.85

Calls 1

createErrorFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…