nativescript-unit-test-runner
Version:
NativeScript unit test runner component.
83 lines (82 loc) • 3.42 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var KarmaFilesService = (function () {
function KarmaFilesService(http, config) {
this.http = http;
this.extensionRegex = /\.([^.\/]+)$/;
this.appPrefix = null;
this.testsPrefix = null;
this.nodeModulesPrefix = null;
this.bundle = false;
this.appPrefix = "/base/" + config.options.appDirectoryRelativePath + "/";
this.testsPrefix = "/base/" + config.options.appDirectoryRelativePath + "/tests";
this.nodeModulesPrefix = "/base/node_modules/";
this.bundle = config.options.bundle;
}
KarmaFilesService.prototype.getServedFilesData = function (baseUrl) {
var _this = this;
var contextUrl = baseUrl + "/context.json";
console.log("NSUTR: downloading " + contextUrl);
var result = this.http.getString(contextUrl)
.then(function (content) {
var parsedContent = JSON.parse(content);
return parsedContent.files;
})
.then(function (scriptUrls) {
return Promise.all(scriptUrls.map(function (url) {
var _a = _this.getScriptData(url), extension = _a.extension, localPath = _a.localPath, type = _a.type;
if (localPath) {
return Promise.resolve({
url: url,
type: type,
localPath: localPath,
});
}
else {
return _this.http.getString(baseUrl + url)
.then(function (contents) {
return {
url: url,
type: type,
contents: contents,
shouldEval: !extension || extension.toLowerCase() === "js"
};
});
}
}));
});
return result;
};
KarmaFilesService.prototype.getScriptData = function (url) {
var queryStringStartIndex = url.lastIndexOf('?');
var pathWithoutQueryString = url.substring(0, queryStringStartIndex);
var extension = this.extensionRegex.exec(pathWithoutQueryString)[1];
var type = this.getScriptType(url);
var localPath = null;
if (!this.bundle && url.startsWith(this.appPrefix)) {
localPath = this.getScriptLocalPath(url, extension);
}
return { extension: extension, localPath: localPath, type: type };
};
KarmaFilesService.prototype.getScriptType = function (url) {
var type = 1;
if (url.startsWith(this.testsPrefix)) {
type = 0;
}
else if (url.startsWith(this.nodeModulesPrefix)) {
type = 2;
}
return type;
};
KarmaFilesService.prototype.getScriptLocalPath = function (url, scriptExtension) {
var localPath = null;
var queryStringStartIndex = url.lastIndexOf('?');
var relativePath = url.substring(this.appPrefix.length, queryStringStartIndex);
localPath = '../../../' + relativePath;
if (scriptExtension === "ts") {
localPath = localPath.substring(0, localPath.length - 2) + "js";
}
return localPath;
};
return KarmaFilesService;
}());
exports.KarmaFilesService = KarmaFilesService;