appcenter-cli
Version:
Command line tool for Visual Studio App Center
222 lines (221 loc) • 9.79 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const path = require("path");
const commandline_1 = require("../../../../util/commandline");
const interaction_1 = require("../../../../util/interaction");
const appium_1 = require("../../run/appium");
const fs_helper_1 = require("../../../../util/misc/fs-helper");
// Used to sort the found folders in the order of their predicted probability of containing the appium tests.
// Any match level implies that the folder contains "dependency-jars" and "test-classes" with at least one .class file folders.
var FolderMatchLevel;
(function (FolderMatchLevel) {
FolderMatchLevel[FolderMatchLevel["PerfectMatch"] = 1] = "PerfectMatch";
FolderMatchLevel[FolderMatchLevel["PerfectMatchNoJar"] = 2] = "PerfectMatchNoJar";
FolderMatchLevel[FolderMatchLevel["NoTargetFolder"] = 3] = "NoTargetFolder";
FolderMatchLevel[FolderMatchLevel["NoTargetFolderNoJar"] = 4] = "NoTargetFolderNoJar";
FolderMatchLevel[FolderMatchLevel["NoTargetNoUpload"] = 5] = "NoTargetNoUpload";
FolderMatchLevel[FolderMatchLevel["NoTargetNoUploadNoJar"] = 6] = "NoTargetNoUploadNoJar"; // Doesn't conform to the needed structure (target/upload/..) and has no .jar file in parent.
})(FolderMatchLevel || (FolderMatchLevel = {}));
class RunAppiumWizardTestCommand extends commandline_1.AppCommand {
constructor(args, interactiveArgs) {
super(args);
this._args = args;
this._args.args.push(...interactiveArgs);
}
run(client, portalBaseUrl) {
return __awaiter(this, void 0, void 0, function* () {
const searchFolder = this.scanFolder();
if (this._args.args.indexOf("--async") < 0) {
const mergeXml = yield this.promptMergeXml();
if (mergeXml) {
this._args.args.push("--merge-junit-xml");
}
}
const foundFolders = yield searchFolder;
const folder = yield this.promptFolder(foundFolders);
this._args.args.push("--build-dir", folder);
interaction_1.out.text("\nRunning command: appcenter test run appium " + this._args.args.join(" ") + "\n");
return new appium_1.default(this._args).run(client, portalBaseUrl);
});
}
promptMergeXml() {
return __awaiter(this, void 0, void 0, function* () {
const questions = [
{
type: "list",
name: "merge",
message: "Should the xml files be merged in to the <output.xml> file?",
choices: [{
name: "Yes",
value: "true"
}, {
name: "No",
value: "false"
}]
}
];
const answers = yield interaction_1.prompt.question(questions);
return answers.merge === "true" ? true : false;
});
}
scanFolder() {
return __awaiter(this, void 0, void 0, function* () {
const foundFolders = [];
this.scanRecurse(process.cwd(), foundFolders, -1, false, -1);
return foundFolders;
});
}
scanRecurse(dirname, folders, targetFolderParentLevel, uploadFolderIsParent, jarFileParentLevel) {
const dirContent = fs.readdirSync(dirname);
let containsPartRequiredData;
for (const dir of dirContent) {
const fullDir = path.join(dirname, dir);
if (fs.lstatSync(fullDir).isDirectory()) {
if (dir !== "node_modules") {
if (targetFolderParentLevel >= 0) {
targetFolderParentLevel++;
}
if (jarFileParentLevel >= 0) {
jarFileParentLevel++;
}
const isTarget = dir === "target";
if (isTarget) {
targetFolderParentLevel = 0;
}
uploadFolderIsParent = dir === "upload";
const dirContents = fs.readdirSync(fullDir);
if (dirContents.length === 0) {
continue;
}
const containsClassFiles = dirContents.some((dir) => {
return path.parse(dir).ext === ".class";
});
if (dir === "dependency-jars" || (dir === "test-classes" && containsClassFiles)) {
if (containsPartRequiredData) { // If already contains either "dependency-jars" or "test-classes"
const matchLevel = this.calculateMatchLevel(targetFolderParentLevel, uploadFolderIsParent, jarFileParentLevel);
const foundFolder = {
name: path.relative(process.cwd(), fullDir.split(dir)[0]),
path: fullDir.split(dir)[0],
matchLevel: matchLevel
};
if (!folders) {
folders = [foundFolder];
}
else {
folders.push(foundFolder);
}
}
else {
containsPartRequiredData = true;
}
}
else {
this.scanRecurse(fullDir, folders, targetFolderParentLevel, uploadFolderIsParent, jarFileParentLevel);
}
}
}
else {
if (path.parse(dir).ext === ".jar") {
jarFileParentLevel = 0;
}
}
}
}
calculateMatchLevel(targetFolderParentLevel, uploadFolderIsParent, jarFileParentLevel) {
if (jarFileParentLevel === 2) {
if (targetFolderParentLevel === 2) {
if (uploadFolderIsParent) {
return FolderMatchLevel.PerfectMatch;
}
else {
return FolderMatchLevel.NoTargetNoUpload;
}
}
else {
if (uploadFolderIsParent) {
return FolderMatchLevel.NoTargetFolder;
}
else {
return FolderMatchLevel.NoTargetNoUpload;
}
}
}
else {
if (targetFolderParentLevel === 2) {
if (uploadFolderIsParent) {
return FolderMatchLevel.PerfectMatchNoJar;
}
else {
return FolderMatchLevel.NoTargetNoUploadNoJar;
}
}
else {
if (uploadFolderIsParent) {
return FolderMatchLevel.NoTargetFolderNoJar;
}
else {
return FolderMatchLevel.NoTargetNoUploadNoJar;
}
}
}
}
promptFolder(listOfFolders) {
return __awaiter(this, void 0, void 0, function* () {
const shownFolders = listOfFolders.sort((folder1, folder2) => {
if (folder1.matchLevel === folder2.matchLevel) {
return 0;
}
else {
return folder1.matchLevel < folder2.matchLevel ? -1 : 1;
}
});
if (shownFolders.length === 0) {
return yield interaction_1.prompt("We could not find any folders with Appium tests. Please provide the path to them.");
}
const choices = shownFolders.map((folder) => {
return {
name: folder.name,
value: folder.path
};
});
choices.push({
name: "Enter path manually",
value: "manual"
});
const questions = [
{
type: "list",
name: "folderPath",
message: "Pick a folder with the packed Appium tests",
choices: choices
}
];
const answers = yield interaction_1.prompt.question(questions);
if (answers.folderPath === "manual") {
let pathIsValid;
let dirPath;
while (!pathIsValid) {
dirPath = yield interaction_1.prompt(`Please provide the path to the Appium tests.`);
if (dirPath.length === 0) {
pathIsValid = false;
}
else {
pathIsValid = fs_helper_1.directoryExistsSync(path.resolve(dirPath));
}
}
return dirPath;
}
return answers.folderPath;
});
}
}
exports.default = RunAppiumWizardTestCommand;