lobo
Version:
Elm test runner
95 lines (94 loc) • 3.83 kB
JavaScript
exports.__esModule = true;
var Bluebird = require("bluebird");
var logger_1 = require("./logger");
var shelljs = require("shelljs");
var path = require("path");
var fs = require("fs");
var tmp = require("tmp");
var OutputDirectoryManagerImp = /** @class */ (function () {
function OutputDirectoryManagerImp(logger) {
this.logger = logger;
}
OutputDirectoryManagerImp.prototype.cleanup = function (context) {
if (context.config.noCleanup || !context.tempDirectory) {
return Bluebird.resolve(context);
}
this.logger.debug("Cleaning lobo temp directory");
if (context.buildOutputFilePath) {
this.deleteTempFile(context.tempDirectory, context.buildOutputFilePath);
}
if (context.testSuiteOutputFilePath) {
this.deleteTempFile(context.tempDirectory, context.testSuiteOutputFilePath);
}
this.deleteTempDir(context.tempDirectory);
return Bluebird.resolve(context);
};
OutputDirectoryManagerImp.prototype.deleteTempDir = function (tempDir) {
try {
if (path.basename(path.dirname(tempDir)) !== ".lobo") {
this.logger.error("Unable to delete directories outside of the \".lobo\" directory");
return;
}
if (!fs.existsSync(tempDir)) {
return;
}
fs.rmdirSync(tempDir);
}
catch (err) {
this.logger.debug(err);
}
};
OutputDirectoryManagerImp.prototype.deleteTempFile = function (tempDir, filePath) {
try {
if (path.basename(path.dirname(path.dirname(filePath))) !== ".lobo") {
this.logger.error("Unable to delete files outside of the \".lobo\" directory");
return;
}
if (tempDir !== path.dirname(filePath)) {
this.logger.error("Unable to delete files outside of the lobo temp directory");
return;
}
if (!fs.existsSync(filePath)) {
return;
}
fs.unlinkSync(filePath);
}
catch (err) {
this.logger.debug(err);
}
};
OutputDirectoryManagerImp.prototype.ensureBuildDirectory = function (context) {
if (!fs.existsSync(context.config.loboDirectory)) {
shelljs.mkdir(context.config.loboDirectory);
}
};
OutputDirectoryManagerImp.prototype.prepare = function (context) {
var _this = this;
return new Bluebird(function (resolve, reject) {
try {
_this.ensureBuildDirectory(context);
_this.updateContextForRun(context);
resolve(context);
}
catch (err) {
var message = "Failed to configure lobo temp directory. " +
("Please try deleting the lobo directory (" + context.config.loboDirectory + ") and re-run lobo");
_this.logger.error(message, err);
reject();
}
});
};
OutputDirectoryManagerImp.prototype.updateContextForRun = function (context) {
var dir = path.resolve(context.config.loboDirectory);
context.tempDirectory = tmp.dirSync({ dir: dir, prefix: "lobo-", discardDescriptor: true }).name;
context.buildOutputFilePath = path.join(context.tempDirectory, "UnitTest.js");
context.testSuiteOutputFilePath = path.join(context.tempDirectory, context.config.testMainElm);
};
return OutputDirectoryManagerImp;
}());
exports.OutputDirectoryManagerImp = OutputDirectoryManagerImp;
function createOutputDirectoryManager() {
return new OutputDirectoryManagerImp(logger_1.createLogger());
}
exports.createOutputDirectoryManager = createOutputDirectoryManager;
;