This repository was archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
104 lines (88 loc) · 3.26 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
94
95
96
97
98
99
100
101
102
103
104
const path = require("path");
const findRoot = require("find-root");
const fs = require("fs");
const resolve = require("resolve");
const globParent = require("glob-parent");
exports.interfaceVersion = 2;
exports.resolve = function (source, file, config) {
const _configPath = config.config;
let jsConfig;
let packageDir;
let configPath = typeof _configPath === "string" ? path.resolve(_configPath) : _configPath;
if (!configPath || typeof configPath === "string") {
if (!configPath || !path.isAbsolute(configPath)) {
packageDir = findRoot(path.resolve(file));
if (!packageDir) throw new Error("package not found above " + file);
}
configPath = findConfigPath(configPath, packageDir);
if (configPath) {
try {
jsConfig = require(configPath);
} catch (e) {
console.log("Error resolving jsConfig", e);
throw e;
}
} else {
jsConfig = {};
}
} else {
jsConfig = configPath;
configPath = null;
}
const aliases = jsConfig.compilerOptions.paths;
const packages = (config.packages || [])
.map((pkg) => {
const parent = path.resolve(process.cwd(), globParent(pkg));
const dir = fs
.readdirSync(parent)
.map((name) => path.resolve(parent, name))
.filter((name) => !fs.statSync(name).isFile());
return dir;
})
.reduce((prev, curr) => prev.concat(curr), []);
try {
const modifiedSource = Object.keys(aliases || {}).reduce((currentSource, initPrefix) => {
const prefix = initPrefix.replace(/\/\*/gm, "");
let ret = currentSource;
if (currentSource.indexOf(prefix) === 0 && currentSource[prefix.length] === "/") {
const prefixPath = getPath(getAliasValue(aliases, initPrefix), file, packages);
ret = `${prefixPath}${currentSource.substr(prefix.length)}`;
} else if (currentSource === prefix) {
ret = getPath(getAliasValue(aliases, initPrefix), file, packages);
}
return ret;
}, source);
const resolvedPath = resolve.sync(modifiedSource, getOptions(file, config));
return { found: true, path: resolvedPath };
} catch (e) {
console.log(e);
return { found: false };
}
};
function getPath(configPath, filepath, packages) {
const workspaces = packages.filter(
(pkg) => filepath.indexOf(pkg) === 0 && filepath[pkg.length] === "/"
);
const relativeRoot = workspaces[0] || process.cwd();
return path.resolve(relativeRoot, configPath);
}
function getAliasValue(aliases, prefix) {
return aliases[prefix][0].replace(/\*/gm, "");
}
function findConfigPath(configPath, packageDir) {
if (path.isAbsolute(configPath)) {
return configPath;
}
configPath = path.join(packageDir, configPath);
return configPath;
}
function getOptions(file, config) {
return {
extensions: config.extensions || [".js", ".jsx"],
basedir: path.dirname(path.resolve(file)),
packageFilter(pkg) {
pkg.main = pkg["jsnext:main"] || pkg.main;
return pkg;
},
};
}