forked from abrobston/jscc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolvePackageDirectories.js
140 lines (131 loc) · 5.18 KB
/
resolvePackageDirectories.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(["require", "async", "./asyncSupport"], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(require);
} else {
root.jsccresolvePackageDirectories = factory(function(mod) {
return root["jscc" + mod];
});
}
}(this, function(require) {
require("./asyncSupport");
var async = require("async");
var thisDirName = "";
if (typeof __dirname === "string") {
thisDirName = __dirname;
} else if (typeof __DIR__ === "string") {
thisDirName = __DIR__;
}
// Backfill for path.join when running in non-Node environments
function pathJoin() {
var resultQueue = [], index, current;
for (index = 0; index < arguments.length; index++) {
current = arguments[index];
if (typeof current === "string" && current.trim() !== "") {
resultQueue.push(current.trim().replace(/^[\\\/]*/, "").replace(/[\\\/]*$/, ""));
}
}
if (!/^[A-Za-z]:(?:[\\\/].*)?$/.test(resultQueue[0])) {
resultQueue.unshift("");
}
return resultQueue.join("/");
}
function isDirectory(dirPath, callback) {
if (typeof process === "object" && typeof process.version === "string") {
// Node
require("fs").stat(dirPath, function(e, stat) {
callback(e, !e && stat.isDirectory());
});
} else if (typeof Java !== "undefined" && typeof Java.type === "function") {
// Nashorn
var File = Java.type("java.io.File");
var file = new File(dirPath);
callback(null, file.exists() && file.isDirectory());
} else if (typeof java !== "undefined") {
// Rhino
var rhinoFile = new java.io.File(dirPath);
callback(null, rhinoFile.exists() && rhinoFile.isDirectory());
} else {
callback(
new Error("Platform is not Node, Nashorn, or Rhino, so filesystem operations are currently unsupported."));
}
}
function resolveDefault(moduleName, callback) {
var mainModulePath = pathJoin(thisDirName, "node_modules", moduleName);
isDirectory(mainModulePath, function(e, result) {
if (!e && result) {
callback(null, mainModulePath);
return;
}
callback(new Error("Could not resolve moduleName '" + moduleName + "'" + (e ? (": " + e.message) : ".")));
});
}
function resolveLast(parentDirPath, moduleName, callback) {
if (parentDirPath) {
var childPath = pathJoin(parentDirPath, "node_modules", moduleName);
isDirectory(childPath, function(err, result) {
if (!err && result) {
callback(null, childPath);
return;
}
resolveDefault(moduleName, callback);
});
} else {
resolveDefault(moduleName, callback);
}
}
var resolve = async.memoize(function(packageTreeString, callback) {
var match = /^(.*)\s(\S+)$/.exec(packageTreeString.trim());
if (match) {
resolve(match[1], function(err, parentPath) {
if (err) {
callback(err);
return;
}
resolveLast(parentPath, match[2], callback);
});
} else {
resolveDefault(packageTreeString.trim(), callback);
}
}, function(packageTreeString) {
return packageTreeString.trim().replace(/\s+/g, " ");
});
/**
* To work with npm versions 2 and 3, whose installation behavior differs,
* use this function. Specify each module path as a space-delimited package
* tree. For example, to specify that we want the path to the version of
* the "esprima" package that the "amdclean" package uses, use the path
* "amdclean esprima".
* @param {!(string|Array<string>)} modulePaths - The space-delimited package
* tree or trees, e.g., "amdclean esprima" or ["amdclean esprima", "amdclean escodegen"]
* @param {function(?Error, Array<string>)=} cb - An optional callback that takes a
* nullable Error parameter and a parameter containing an array of paths to each
* module directory
* @returns {(undefined|Array<string>)} If there is no callback, returns an array of
* paths to each module directory
*/
function getNodeModuleDependencyPaths(modulePaths, cb) {
if (typeof modulePaths === "string") {
modulePaths = [modulePaths];
}
if (typeof cb === "function") {
async.map(modulePaths, resolve, cb);
return;
}
var outerError, outerResult, done = false;
async.map(modulePaths, resolve, function(error, result) {
outerError = error;
outerResult = result;
done = true;
});
while (!done) {
// no-op
}
if (outerError) {
throw outerError;
}
return outerResult;
}
return getNodeModuleDependencyPaths;
}));