This repository has been archived by the owner on Nov 14, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
66 lines (54 loc) · 1.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
var path = require('path');
var root = process.cwd();
var accessSync = require('fs').accessSync;
var lstatSync = require('fs').lstatSync;
var readlinkSync = require('fs').readlinkSync;
function existsSync(filepath, parent){
var depth, link, linkError, linkRoot, relativeLink, resolvedLink, stats;
var resolvedPath = path.resolve(filepath);
try {
stats = lstatSync(resolvedPath);
// if symlink, check if target
if (stats && stats.isSymbolicLink()) {
link = path.normalize(readlinkSync(resolvedPath));
linkRoot = path.dirname(resolvedPath);
if (link && link.indexOf('..') !== -1) {
// resolve relative path
depth = pathDepth(link);
relativeLink = path.relative(path.resolve(linkRoot, depth), path.basename(link));
resolvedLink = path.resolve(linkRoot, relativeLink);
} else {
// assume root and resolve
resolvedLink = path.resolve(root, link);
}
try {
accessSync(path.dirname(resolvedLink));
} catch (err) {
if (err.code === "ENOENT") {
// Log message for user so they can investigate
console.log(err.message);
console.log('Please verify that the symlink for ' + resolvedPath + ' can be resolved from ' + root + '.');
}
}
if (parent && parent === resolvedLink) {
linkError = new Error('Circular symlink detected: ' + resolvedPath + ' -> ' + resolvedLink);
throw linkError;
}
return existsSync(resolvedLink, resolvedPath);
}
return true;
} catch (err) {
if (err.message.match(/Circular symlink detected/)) {
throw err;
}
return checkError(err);
}
}
function checkError(err) {
return err && err.code === "ENOENT" ? false : true;
}
function pathDepth(filepath) {
return new Array(filepath.split(path.sep).length).join('..' + path.sep);
}
module.exports = existsSync;