Files
power/lib/configuration.js
2014-06-25 06:51:35 +08:00

238 lines
7.7 KiB
JavaScript

// Generated by CoffeeScript 1.6.2
(function() {
var Configuration, Logger, async, compilePattern, fs, getFilenamesForHost, getUserEnv, libraryPath, mkdirp, path, rstat, sourceScriptEnv,
__slice = [].slice;
fs = require("fs");
path = require("path");
async = require("async");
Logger = require("./logger");
mkdirp = require("./util").mkdirp;
sourceScriptEnv = require("./util").sourceScriptEnv;
getUserEnv = require("./util").getUserEnv;
module.exports = Configuration = (function() {
Configuration.userConfigurationPath = path.join(process.env.HOME, ".powerconfig");
Configuration.loadUserConfigurationEnvironment = function(callback) {
var _this = this;
return getUserEnv(function(err, env) {
var p;
if (err) {
return callback(err);
} else {
return fs.exists(p = _this.userConfigurationPath, function(exists) {
if (exists) {
return sourceScriptEnv(p, env, callback);
} else {
return callback(null, env);
}
});
}
});
};
Configuration.getUserConfiguration = function(callback) {
return this.loadUserConfigurationEnvironment(function(err, env) {
if (err) {
return callback(err);
} else {
return callback(null, new Configuration(env));
}
});
};
Configuration.optionNames = ["bin", "dstPort", "httpPort", "dnsPort", "domains", "extDomains", "hostRoot", "logRoot"];
function Configuration(env) {
if (env == null) {
env = process.env;
}
this.loggers = {};
this.initialize(env);
}
Configuration.prototype.initialize = function(env) {
var _base, _base1, _ref, _ref1, _ref10, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9;
this.env = env;
this.bin = (_ref = env.POWER_BIN) != null ? _ref : path.join(__dirname, "../bin/power");
this.dstPort = (_ref1 = env.POWER_DST_PORT) != null ? _ref1 : 80;
this.httpPort = (_ref2 = env.POWER_HTTP_PORT) != null ? _ref2 : 20559;
this.dnsPort = (_ref3 = env.POWER_DNS_PORT) != null ? _ref3 : 20560;
this.domains = (_ref4 = (_ref5 = env.POWER_DOMAINS) != null ? _ref5 : env.POWER_DOMAIN) != null ? _ref4 : "dev";
this.extDomains = (_ref6 = env.POWER_EXT_DOMAINS) != null ? _ref6 : [];
this.domains = (_ref7 = typeof (_base = this.domains).split === "function" ? _base.split(",") : void 0) != null ? _ref7 : this.domains;
this.extDomains = (_ref8 = typeof (_base1 = this.extDomains).split === "function" ? _base1.split(",") : void 0) != null ? _ref8 : this.extDomains;
this.allDomains = this.domains.concat(this.extDomains);
this.allDomains.push(/\d+\.\d+\.\d+\.\d+\.xip\.io$/, /[0-9a-z]{1,7}\.xip\.io$/);
this.supportRoot = libraryPath("Application Support", "Power");
this.hostRoot = (_ref9 = env.POWER_HOST_ROOT) != null ? _ref9 : path.join(this.supportRoot, "Hosts");
this.logRoot = (_ref10 = env.POWER_LOG_ROOT) != null ? _ref10 : libraryPath("Logs", "Power");
this.dnsDomainPattern = compilePattern(this.domains);
return this.httpDomainPattern = compilePattern(this.allDomains);
};
Configuration.prototype.toJSON = function() {
var key, result, _i, _len, _ref;
result = {};
_ref = this.constructor.optionNames;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
key = _ref[_i];
result[key] = this[key];
}
return result;
};
Configuration.prototype.getLogger = function(name) {
var _base;
return (_base = this.loggers)[name] || (_base[name] = new Logger(path.join(this.logRoot, name + ".log")));
};
Configuration.prototype.findHostConfiguration = function(host, callback) {
var _this = this;
if (host == null) {
host = "";
}
return this.gatherHostConfigurations(function(err, hosts) {
var config, domain, file, _i, _j, _len, _len1, _ref, _ref1;
if (err) {
return callback(err);
}
_ref = _this.allDomains;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
domain = _ref[_i];
_ref1 = getFilenamesForHost(host, domain);
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
file = _ref1[_j];
if (config = hosts[file]) {
return callback(null, domain, config);
}
}
}
if (config = hosts["default"]) {
return callback(null, _this.allDomains[0], config);
}
return callback(null);
});
};
Configuration.prototype.gatherHostConfigurations = function(callback) {
var hosts,
_this = this;
hosts = {};
return mkdirp(this.hostRoot, function(err) {
if (err) {
return callback(err);
}
return fs.readdir(_this.hostRoot, function(err, files) {
if (err) {
return callback(err);
}
return async.forEach(files, function(file, next) {
var name, root;
root = path.join(_this.hostRoot, file);
name = file.toLowerCase();
return rstat(root, function(err, stats, path) {
if (stats != null ? stats.isDirectory() : void 0) {
hosts[name] = {
root: path
};
return next();
} else if (stats != null ? stats.isFile() : void 0) {
return fs.readFile(path, 'utf-8', function(err, data) {
if (err) {
return next();
}
data = data.trim();
if (data.length < 10 && !isNaN(parseInt(data))) {
hosts[name] = {
url: "http://localhost:" + (parseInt(data))
};
} else if (data.match("https?://")) {
hosts[name] = {
url: data
};
}
return next();
});
} else {
return next();
}
});
}, function(err) {
return callback(err, hosts);
});
});
});
};
return Configuration;
})();
libraryPath = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return path.join.apply(path, [process.env.HOME, "Library"].concat(__slice.call(args)));
};
getFilenamesForHost = function(host, domain) {
var i, length, parts, _i, _ref, _ref1, _results;
host = host.toLowerCase();
if (domain.test != null) {
domain = (_ref = (_ref1 = host.match(domain)) != null ? _ref1[0] : void 0) != null ? _ref : "";
}
if (host.slice(-domain.length - 1) === ("." + domain)) {
parts = host.slice(0, -domain.length - 1).split(".");
length = parts.length;
_results = [];
for (i = _i = 0; 0 <= length ? _i < length : _i > length; i = 0 <= length ? ++_i : --_i) {
_results.push(parts.slice(i, length).join("."));
}
return _results;
} else {
return [];
}
};
rstat = function(path, callback) {
return fs.lstat(path, function(err, stats) {
if (err) {
return callback(err);
} else if (stats != null ? stats.isSymbolicLink() : void 0) {
return fs.realpath(path, function(err, realpath) {
if (err) {
return callback(err);
} else {
return rstat(realpath, callback);
}
});
} else {
return callback(err, stats, path);
}
});
};
compilePattern = function(domains) {
return RegExp("((^|\\.)(" + (domains.join("|")) + "))\\.?$", "i");
};
}).call(this);