kopia lustrzana https://github.com/c9/core
use builtin module resolution instead of reimplementing it in architect
- this also disables the version using async fs functions, since it was only partially async anyway.pull/377/merge
rodzic
046caa805b
commit
24eb50242a
|
@ -412,6 +412,7 @@ function quote(str) {
|
|||
}
|
||||
|
||||
|
||||
module.exports.getDeps = getDeps;
|
||||
module.exports.getSubmodules = getSubmodules;
|
||||
module.exports.resolveModulePath = resolveModulePath;
|
||||
module.exports.removeLicenceComments = removeLicenceComments;
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
.npmignore
|
|
@ -12,12 +12,7 @@ var DEBUG = typeof location != "undefined" && location.href.match(/debug=[123]/)
|
|||
// Only define Node-style usage using sync I/O if in node.
|
||||
if (typeof module === "object") (function () {
|
||||
var dirname = require('path').dirname;
|
||||
var resolve = require('path').resolve;
|
||||
var existsSync = require('fs').existsSync || require('path').existsSync;
|
||||
var realpathSync = require('fs').realpathSync;
|
||||
var exists = require('fs').exists || require('path').exists;
|
||||
var realpath = require('fs').realpath;
|
||||
var packagePathCache = {};
|
||||
var Module = require("module");
|
||||
|
||||
exports.loadConfig = loadConfig;
|
||||
exports.resolveConfig = resolveConfig;
|
||||
|
@ -34,8 +29,14 @@ if (typeof module === "object") (function () {
|
|||
function resolveConfig(config, base, callback) {
|
||||
if (!callback)
|
||||
return resolveConfigSync(config, base);
|
||||
else
|
||||
resolveConfigAsync(config, base, callback);
|
||||
try {
|
||||
var result = resolveConfigSync(config, base);
|
||||
} catch (e) {
|
||||
var err = e;
|
||||
}
|
||||
process.nextTick(function() {
|
||||
callback(err, result);
|
||||
});
|
||||
}
|
||||
|
||||
function resolveConfigSync(config, base) {
|
||||
|
@ -59,46 +60,6 @@ if (typeof module === "object") (function () {
|
|||
return config;
|
||||
}
|
||||
|
||||
function resolveConfigAsync(config, base, callback) {
|
||||
function resolveNext(i) {
|
||||
if (i >= config.length) {
|
||||
return callback(null, config);
|
||||
}
|
||||
|
||||
var plugin = config[i];
|
||||
|
||||
// Shortcut where string is used for plugin without any options.
|
||||
if (typeof plugin === "string") {
|
||||
plugin = config[i] = { packagePath: plugin };
|
||||
}
|
||||
// The plugin is a package on the disk. We need to load it.
|
||||
if (plugin.hasOwnProperty("packagePath") && !plugin.hasOwnProperty("setup")) {
|
||||
resolveModule(base, plugin.packagePath, function(err, defaults) {
|
||||
if (err) return callback(err);
|
||||
|
||||
Object.keys(defaults).forEach(function (key) {
|
||||
if (!plugin.hasOwnProperty(key)) {
|
||||
plugin[key] = defaults[key];
|
||||
}
|
||||
});
|
||||
plugin.packagePath = defaults.packagePath;
|
||||
try {
|
||||
plugin.setup = require(plugin.packagePath);
|
||||
} catch(e) {
|
||||
return callback(e);
|
||||
}
|
||||
|
||||
return resolveNext(++i);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
return resolveNext(++i);
|
||||
}
|
||||
|
||||
resolveNext(0);
|
||||
}
|
||||
|
||||
// Loads a module, getting metadata from either it's package.json or export
|
||||
// object.
|
||||
function resolveModuleSync(base, modulePath) {
|
||||
|
@ -107,7 +68,7 @@ if (typeof module === "object") (function () {
|
|||
packagePath = resolvePackageSync(base, modulePath + "/package.json");
|
||||
}
|
||||
catch (err) {
|
||||
if (err.code !== "ENOENT") throw err;
|
||||
if (err.code !== "ENOENT" && err.code !== 'MODULE_NOT_FOUND') throw err;
|
||||
}
|
||||
var metadata = packagePath && require(packagePath).plugin || {};
|
||||
if (packagePath) {
|
||||
|
@ -122,148 +83,17 @@ if (typeof module === "object") (function () {
|
|||
return metadata;
|
||||
}
|
||||
|
||||
// Loads a module, getting metadata from either it's package.json or export
|
||||
// object.
|
||||
function resolveModule(base, modulePath, callback) {
|
||||
resolvePackage(base, modulePath + "/package.json", function(err, packagePath) {
|
||||
//if (err && err.code !== "ENOENT") return callback(err);
|
||||
|
||||
var metadata = {};
|
||||
if (!err) {
|
||||
try {
|
||||
metadata = packagePath && require(packagePath).plugin || {};
|
||||
} catch(e) {
|
||||
return callback(e);
|
||||
}
|
||||
}
|
||||
|
||||
(function(next) {
|
||||
if (err) {
|
||||
//@todo Fabian what is a better way?
|
||||
resolvePackage(base, modulePath + ".js", next);
|
||||
}
|
||||
else if (packagePath) {
|
||||
next(null, dirname(packagePath));
|
||||
}
|
||||
else {
|
||||
resolvePackage(base, modulePath, next);
|
||||
}
|
||||
})(function(err, modulePath) {
|
||||
if (err) return callback(err);
|
||||
|
||||
var module;
|
||||
try {
|
||||
module = require(modulePath);
|
||||
} catch(e) {
|
||||
return callback(e);
|
||||
}
|
||||
|
||||
metadata.provides = metadata.provides || module.provides || [];
|
||||
metadata.consumes = metadata.consumes || module.consumes || [];
|
||||
metadata.packagePath = modulePath;
|
||||
callback(null, metadata);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Node style package resolving so that plugins' package.json can be found relative to the config file
|
||||
// It's not the full node require system algorithm, but it's the 99% case
|
||||
// This throws, make sure to wrap in try..catch
|
||||
function resolvePackageSync(base, packagePath) {
|
||||
var originalBase = base;
|
||||
if (!(base in packagePathCache)) {
|
||||
packagePathCache[base] = {};
|
||||
}
|
||||
var cache = packagePathCache[base];
|
||||
if (packagePath in cache) {
|
||||
return cache[packagePath];
|
||||
}
|
||||
var newPath;
|
||||
if (packagePath[0] === "." || packagePath[0] === "/") {
|
||||
newPath = resolve(base, packagePath);
|
||||
if (!existsSync(newPath)) {
|
||||
newPath = newPath + ".js";
|
||||
}
|
||||
if (existsSync(newPath)) {
|
||||
newPath = realpathSync(newPath);
|
||||
cache[packagePath] = newPath;
|
||||
return newPath;
|
||||
}
|
||||
}
|
||||
else {
|
||||
while (base) {
|
||||
newPath = resolve(base, "node_modules", packagePath);
|
||||
if (existsSync(newPath)) {
|
||||
newPath = realpathSync(newPath);
|
||||
cache[packagePath] = newPath;
|
||||
return newPath;
|
||||
}
|
||||
base = resolve(base, '..');
|
||||
}
|
||||
}
|
||||
var err = new Error("Can't find '" + packagePath + "' relative to '" + originalBase + "'");
|
||||
err.code = "ENOENT";
|
||||
throw err;
|
||||
return Module._resolveFilename(packagePath, {
|
||||
paths: Module._nodeModulePaths(base),
|
||||
filename: base + "/package.json",
|
||||
id: base + "/package.json",
|
||||
});
|
||||
}
|
||||
|
||||
function resolvePackage(base, packagePath, callback) {
|
||||
var originalBase = base;
|
||||
if (!packagePathCache.hasOwnProperty(base)) {
|
||||
packagePathCache[base] = {};
|
||||
}
|
||||
var cache = packagePathCache[base];
|
||||
if (cache.hasOwnProperty(packagePath)) {
|
||||
return callback(null, cache[packagePath]);
|
||||
}
|
||||
if (packagePath[0] === "." || packagePath[0] === "/") {
|
||||
var newPath = resolve(base, packagePath);
|
||||
exists(newPath, function(exists) {
|
||||
if (exists) {
|
||||
realpath(newPath, function(err, newPath) {
|
||||
if (err) return callback(err);
|
||||
|
||||
cache[packagePath] = newPath;
|
||||
return callback(null, newPath);
|
||||
});
|
||||
} else {
|
||||
var err = new Error("Can't find '" + packagePath + "' relative to '" + originalBase + "'");
|
||||
err.code = "ENOENT";
|
||||
return callback(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
tryNext(base);
|
||||
}
|
||||
|
||||
function tryNext(base) {
|
||||
if (base == "/") {
|
||||
var err = new Error("Can't find '" + packagePath + "' relative to '" + originalBase + "'");
|
||||
err.code = "ENOENT";
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var newPath = resolve(base, "node_modules", packagePath);
|
||||
exists(newPath, function(exists) {
|
||||
if (exists) {
|
||||
realpath(newPath, function(err, newPath) {
|
||||
if (err) return callback(err);
|
||||
|
||||
cache[packagePath] = newPath;
|
||||
return callback(null, newPath);
|
||||
});
|
||||
} else {
|
||||
var nextBase = resolve(base, '..');
|
||||
if (nextBase === base)
|
||||
tryNext("/"); // for windows
|
||||
else
|
||||
tryNext(nextBase);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}());
|
||||
|
||||
// Otherwise use amd to load modules.
|
||||
|
|
|
@ -206,7 +206,7 @@ function start(configName, options, callback) {
|
|||
|
||||
architect.resolveConfig(plugins, __dirname + "/plugins", function(err, config) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
console.error(err.stack || err);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue