UNPKG

appcenter-cli

Version:

Command line tool for Visual Studio App Center

91 lines (90 loc) 4.57 kB
"use strict"; 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.TestManifestReader = void 0; const test_manifest_1 = require("./test-manifest"); const path_resolver_1 = require("./path-resolver"); const fs = require("fs"); const path = require("path"); class TestManifestReader { constructor(pathResolver) { if (!pathResolver) { throw new Error("Argument pathResolver is required"); } this.pathResolver = pathResolver; } static readFromFile(filePath) { return __awaiter(this, void 0, void 0, function* () { const workspace = path.dirname(filePath); const resolver = new path_resolver_1.PathResolver(workspace); const readerInstance = new TestManifestReader(resolver); const json = TestManifestReader.readJsonFromFile(filePath); return yield readerInstance.readManifest(json); }); } static readJsonFromFile(filePath) { const json = fs.readFileSync(filePath, "utf8"); return JSON.parse(json); } readManifest(json) { return __awaiter(this, void 0, void 0, function* () { const files = yield this.readTestFiles(json.files); const applicationFile = json.applicationFile ? yield this.readApplicationFile(json.applicationFile) : null; return new test_manifest_1.TestManifest(json.schemaVersion, json.cliVersion, applicationFile, files, new test_manifest_1.TestFrameworkData(json.testFramework.name, json.testFramework.data)); }); } readTestFiles(json) { return __awaiter(this, void 0, void 0, function* () { const filePatterns = json.filter((f) => typeof f === "string"); const fileDescriptions = json.filter((f) => typeof f !== "string"); return yield (yield this.readFilePatterns(filePatterns)).concat(yield this.readFileDescriptions(fileDescriptions)); }); } readApplicationFile(pattern) { return __awaiter(this, void 0, void 0, function* () { const inputFile = yield this.resolveSinglePathPattern(pattern); return yield test_manifest_1.TestRunFile.create(path.join(this.pathResolver.workspace, inputFile), path.basename(inputFile), "app-file"); }); } readFilePatterns(patterns) { return __awaiter(this, void 0, void 0, function* () { const filePaths = yield this.pathResolver.resolve(patterns); return yield Promise.all(filePaths.map((relativePath) => { const fullPath = path.join(this.pathResolver.workspace, relativePath); return test_manifest_1.TestRunFile.create(fullPath, relativePath, "test-file"); })); }); } readFileDescriptions(descriptions) { return __awaiter(this, void 0, void 0, function* () { return yield Promise.all(descriptions.map((d) => this.readFileDescription(d))); }); } readFileDescription(description) { return __awaiter(this, void 0, void 0, function* () { const inputFile = yield this.resolveSinglePathPattern(description.sourcePath); return yield test_manifest_1.TestRunFile.create(path.join(this.pathResolver.workspace, inputFile), description.targetPath, "test-file"); }); } resolveSinglePathPattern(pattern) { return __awaiter(this, void 0, void 0, function* () { const filePaths = yield this.pathResolver.resolve(pattern); if (filePaths.length > 1) { throw new Error(`Pattern ${pattern} resolved to more than one file`); } else if (filePaths.length === 0) { throw new Error(`Pattern ${pattern} did not resolve to any existing file`); } return filePaths[0]; }); } } exports.TestManifestReader = TestManifestReader;