-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
93 lines (80 loc) · 2.37 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var dirname = require(`path`).dirname
var join = require(`path`).join
var normalize = require(`path`).normalize
var existsSync = require(`fs`).existsSync
var readFileSync = require(`fs`).readFileSync
var shortcuts = {}
function findRootDir(path) {
var packageJson = join(path, `package.json`)
var parentPath = normalize(path + `/..`)
return existsSync(packageJson) ? path : findRootDir(parentPath)
}
function getOptions(root) {
var path = join(root, `.paths`)
if (!existsSync(path)) return {}
var text = readFileSync(path, {encoding: `utf8`})
var re = /(\$[\w-]+)\s*=\s*(.+)/gm
var match, options = {}
while(match = re.exec(text)) {
var name = match[1]
var value = match[2]
var dir = value === `/` ? root : join(root, value)
options[name] = dir
}
return options
}
function setShortcuts(path) {
var root = findRootDir(path)
if (!shortcuts[root]) {
shortcuts[root] = getOptions(root)
}
return shortcuts[root]
}
function callerPath() {
var err = new Error()
var re = /at require \(internal\/module\.js:11:18\)\n.+\((.*):\d+:\d+\)/m
if (!err.stack.match(re)) {
re = /at Object\.<anonymous> \((.+):\d+:\d+\)/m
}
return err.stack.match(re)[1]
}
function moduleSearch(path) {
return function(mdl) {
var re = new RegExp(mdl.replace(/\\/g, `\\\\`))
return !path.search(re)
}
}
function getRoot() {
var path = dirname(callerPath())
var modules = Object.keys(shortcuts).reverse()
var findModule = moduleSearch(path)
return modules.find(findModule)
}
function rootPath(path) {
var root = getRoot()
var dir = join(root, path)
var exists = existsSync(dir) || existsSync(normalize(dir + `/..`))
return exists && dir
}
function shortcutPath(path) {
var root = getRoot()
if (!root) return false
var shortcut = path.match(/^\$[\w-]+/)[0]
var dir = shortcuts[root][shortcut]
return path.replace(shortcut, dir)
}
function getPaths(path) {
var dir = path || callerPath()
return setShortcuts(dir)
}
var Module = require(`module`)
var include = Module.prototype.require
Module.prototype.require = function(path) {
var dir = path
if (path[0] === `.`) return include.call(this, path)
if (path[0] === `/`) dir = rootPath(path) || path
if (path[0] === `$`) dir = shortcutPath(path) || path
if (path === `sexy-require`) return getPaths()
return include.call(this, dir)
}
module.exports = getPaths(module.parent.filename)