lobo
Version:
Elm test runner
150 lines (149 loc) • 5.96 kB
JavaScript
;
exports.__esModule = true;
var levenshtein = require("fast-levenshtein");
var fs = require("fs");
var _ = require("lodash");
var shelljs = require("shelljs");
var logger_1 = require("./logger");
var path = require("path");
var UtilImp = /** @class */ (function () {
function UtilImp(logger) {
this.logger = logger;
}
UtilImp.prototype.availablePlugins = function (fileSpec) {
var pattern = new RegExp(fileSpec + ".*\.js$");
var pluginDirectory = path.resolve(__dirname, "..", "plugin");
var files = shelljs.find(pluginDirectory).filter(function (file) { return file.match(pattern); });
return files.map(function (file) {
var pluginPath = path.relative(pluginDirectory, file);
return path.dirname(pluginPath);
});
};
UtilImp.prototype.checkNodeVersion = function (major, minor, patch) {
if (!this.isInteger(major)) {
throw new Error("major is not an integer" + major);
}
if (!this.isInteger(minor)) {
throw new Error("minor is not an integer" + major);
}
if (!this.isInteger(patch)) {
throw new Error("patch is not an integer" + major);
}
var nodeVersionString = process.versions.node;
var nodeVersion = _.map(_.split(nodeVersionString, "."), _.parseInt);
if ((nodeVersion[0] < major) ||
(nodeVersion[0] === major && nodeVersion[1] < minor) ||
(nodeVersion[0] === major && nodeVersion[1] === minor && nodeVersion[2] < patch)) {
this.logger.info("using node v" + nodeVersionString);
this.logger.error("lobo requires node v" + major + "." + minor + "." + patch + " or greater - upgrade the installed version of node and try again");
process.exit(1);
}
};
UtilImp.prototype.closestMatch = function (name, items) {
return _.minBy(items, function (i) { return levenshtein.get(name, i); });
};
UtilImp.prototype.getPlugin = function (type, pluginName, fileSpec) {
var value = this.load(type, pluginName, fileSpec, false);
var plugin = value.createPlugin();
this.logger.debug("Plugin loaded: " + pluginName);
this.logger.trace("plugin", plugin);
return plugin;
};
UtilImp.prototype.getPluginConfig = function (type, pluginName, fileSpec) {
var value = this.load(type, pluginName, fileSpec, true);
var config = value.PluginConfig;
this.logger.debug("Plugin configured: " + pluginName);
this.logger.trace("Plugin configuration", config);
return config;
};
UtilImp.prototype.isInteger = function (value) {
return parseInt(value.toString(), 10) === value;
};
UtilImp.prototype.load = function (type, pluginName, fileSpec, isConfiguration) {
try {
var filePath = void 0;
if (isConfiguration) {
filePath = path.join("..", "plugin", pluginName, "plugin-config");
}
else {
filePath = path.join("..", "plugin", pluginName, fileSpec);
}
return this.unsafeLoad(filePath);
}
catch (err) {
if (err && err instanceof SyntaxError) {
this.logger.error("Unable to load " + pluginName + " due to a syntax error in " + pluginName + "/" + fileSpec + ".js");
}
else {
var typeName = isConfiguration ? type + " configuration" : type;
this.logger.error(pluginName + " " + typeName + " not found");
var plugins = this.availablePlugins(fileSpec);
this.logger.error("Did you mean \"" + this.closestMatch(pluginName, plugins) + "\" ?");
}
process.exit(1);
return {};
}
};
UtilImp.prototype.logStage = function (stage) {
var length = 80;
var label = "[ " + stage + " ]";
var prefixLength = Math.ceil((length - label.length) / 2);
var prefix = _.repeat("-", prefixLength);
var suffix = _.repeat("-", length - prefixLength - label.length);
var result = prefix + "[ " + stage + " ]" + suffix;
this.logger.info(result);
};
UtilImp.prototype.padRight = function (value, length, spacer) {
if (!spacer) {
spacer = " ";
}
return (value.length < length) ? this.padRight(value + spacer, length, spacer) : value;
};
UtilImp.prototype.read = function (filePath) {
try {
if (!fs.existsSync(filePath)) {
return undefined;
}
return fs.readFileSync(filePath, "utf8");
}
catch (err) {
this.logger.debug(err);
return undefined;
}
};
UtilImp.prototype.resolveDir = function () {
var dirs = [];
for (var _i = 0; _i < arguments.length; _i++) {
dirs[_i] = arguments[_i];
}
var resolved = path.resolve.apply(path, dirs);
if (!fs.existsSync(resolved)) {
return resolved;
}
var stats = fs.lstatSync(resolved);
if (!stats.isSymbolicLink()) {
return resolved;
}
return fs.realpathSync(resolved);
};
UtilImp.prototype.sortObject = function (obj) {
var sortedKeys = Object.keys(obj).sort();
var result = {};
for (var _i = 0, sortedKeys_1 = sortedKeys; _i < sortedKeys_1.length; _i++) {
var k = sortedKeys_1[_i];
result[k] = obj[k];
}
return result;
};
UtilImp.prototype.unsafeLoad = function (filePath) {
// tslint:disable:no-require-imports
return require(filePath);
// tslint:enable:no-require-imports
};
return UtilImp;
}());
exports.UtilImp = UtilImp;
function createUtil() {
return new UtilImp(logger_1.createLogger());
}
exports.createUtil = createUtil;