appcenter-cli
Version:
Command line tool for Visual Studio App Center
107 lines (106 loc) • 4.77 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EspressoPreparer = void 0;
const path = require("path");
const pglob = require("../../../util/misc/promisfied-glob");
const pfs = require("../../../util/misc/promisfied-fs");
class EspressoPreparer {
constructor(artifactsDir, buildDir, testApkPath, include) {
if (!artifactsDir) {
throw new Error("Argument --artifacts-dir is required");
}
if (include && include.length) {
throw new Error("Argument --include cannot be used for Espresso");
}
this.buildDir = buildDir;
this.artifactsDir = artifactsDir;
this.testApkPath = testApkPath;
}
validateEitherBuildDirOrTestApkPath() {
if (this.buildDir && this.testApkPath) {
throw new Error("You must not specify both build dir and test apk path.");
}
if (!(this.buildDir || this.testApkPath)) {
throw new Error("Either --artifacts-dir, --build-dir or --test-apk-path must be specified");
}
}
prepare() {
return __awaiter(this, void 0, void 0, function* () {
this.validateEitherBuildDirOrTestApkPath();
if (this.testApkPath) {
if (!(yield pfs.fileExists(this.testApkPath))) {
throw new Error(`File not found for test apk path: "${this.testApkPath}"`);
}
yield pfs.cpFile(this.testApkPath, path.join(this.artifactsDir, path.basename(this.testApkPath)));
}
else {
yield this.validateBuildDir();
yield pfs.cpDir(this.buildDir, this.artifactsDir);
}
const manifestPath = path.join(this.artifactsDir, "manifest.json");
const manifest = yield this.createEspressoManifest();
const manifestJson = JSON.stringify(manifest, null, 1);
yield pfs.writeFile(manifestPath, manifestJson);
return manifestPath;
});
}
validateBuildDir() {
return __awaiter(this, void 0, void 0, function* () {
yield this.validateBuildDirExists();
yield this.validateTestApkExists();
});
}
validateBuildDirExists() {
return __awaiter(this, void 0, void 0, function* () {
if (!(yield pfs.directoryExists(this.buildDir))) {
throw new Error(`Espresso build directory "${this.buildDir}" doesn't exist`);
}
});
}
validateTestApkExists() {
return __awaiter(this, void 0, void 0, function* () {
yield this.detectTestApkPathFromBuildDir();
});
}
detectTestApkPathFromBuildDir() {
return __awaiter(this, void 0, void 0, function* () {
const apkPattern = path.join(this.buildDir, "*androidTest.apk");
const files = yield pglob.glob(apkPattern);
if (files.length === 0) {
throw new Error(`An apk with name matching "*androidTest.apk" was not found inside directory inside build directory "${this.buildDir}"`);
}
else if (files.length >= 2) {
throw new Error(`Multiple apks with name matching "*androidTest.apk" were found inside build directory "${this.buildDir}". A unique match is required.`);
}
else {
const apkPath = files[files.length - 1];
return apkPath;
}
});
}
createEspressoManifest() {
return __awaiter(this, void 0, void 0, function* () {
const apkFullPath = this.testApkPath ? this.testApkPath : yield this.detectTestApkPathFromBuildDir();
const apkArtifactsPath = path.basename(apkFullPath);
const result = {
schemaVersion: "1.0.0",
files: [apkArtifactsPath],
testFramework: {
name: "espresso",
data: {},
},
};
return result;
});
}
}
exports.EspressoPreparer = EspressoPreparer;