// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. 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.ManifestHandlerXml = void 0; const fs = require("fs"); const util = require("util"); const uuid_1 = require("uuid"); const xml2js = require("xml2js"); const xmlMethods = require("../xml"); const manifestInfo_1 = require("../manifestInfo"); const manifestHandler_1 = require("./manifestHandler"); const writeFileAsync = util.promisify(fs.writeFile); class ManifestHandlerXml extends manifestHandler_1.ManifestHandler { modifyManifest(guid, displayName) { return __awaiter(this, void 0, void 0, function* () { try { const manifestXml = yield this.parseXmlAsync(); this.setModifiedXmlData(manifestXml.OfficeApp, guid, displayName); return manifestXml; } catch (err) { throw new Error(`Unable to modify xml data for manifest file: ${this.manifestPath}.\n${err}`); } }); } parseManifest() { return __awaiter(this, void 0, void 0, function* () { const xml = yield this.parseXmlAsync(); const manifest = new manifestInfo_1.ManifestInfo(); const officeApp = xml.OfficeApp; if (officeApp) { const defaultSettingsXml = xmlMethods.getXmlElement(officeApp, "DefaultSettings"); manifest.id = xmlMethods.getXmlElementValue(officeApp, "Id"); manifest.allowSnapshot = xmlMethods.getXmlElementValue(officeApp, "AllowSnapshot"); manifest.alternateId = xmlMethods.getXmlElementValue(officeApp, "AlternateId"); manifest.appDomains = xmlMethods.getXmlElementsValue(officeApp, "AppDomains", "AppDomain"); manifest.defaultLocale = xmlMethods.getXmlElementValue(officeApp, "DefaultLocale"); manifest.description = xmlMethods.getXmlElementAttributeValue(officeApp, "Description"); manifest.displayName = xmlMethods.getXmlElementAttributeValue(officeApp, "DisplayName"); manifest.highResolutionIconUrl = xmlMethods.getXmlElementAttributeValue(officeApp, "HighResolutionIconUrl"); manifest.hosts = xmlMethods.getXmlElementsAttributeValue(officeApp, "Hosts", "Host", "Name"); manifest.iconUrl = xmlMethods.getXmlElementAttributeValue(officeApp, "IconUrl"); manifest.officeAppType = xmlMethods.getXmlAttributeValue(officeApp, "xsi:type"); manifest.permissions = xmlMethods.getXmlElementValue(officeApp, "Permissions"); manifest.providerName = xmlMethods.getXmlElementValue(officeApp, "ProviderName"); manifest.supportUrl = xmlMethods.getXmlElementAttributeValue(officeApp, "SupportUrl"); manifest.version = xmlMethods.getXmlElementValue(officeApp, "Version"); manifest.manifestType = manifestInfo_1.ManifestType.XML; if (defaultSettingsXml) { const defaultSettings = new manifestInfo_1.DefaultSettings(); defaultSettings.requestedHeight = xmlMethods.getXmlElementValue(defaultSettingsXml, "RequestedHeight"); defaultSettings.requestedWidth = xmlMethods.getXmlElementValue(defaultSettingsXml, "RequestedWidth"); defaultSettings.sourceLocation = xmlMethods.getXmlElementAttributeValue(defaultSettingsXml, "SourceLocation"); manifest.defaultSettings = defaultSettings; } } return manifest; }); } parseXmlAsync() { return __awaiter(this, void 0, void 0, function* () { // Needed declaration as `this` does not work inside the new Promise expression const fileData = yield this.readFromManifestFile(); const manifestPath = this.manifestPath; return new Promise(function (resolve, reject) { xml2js.parseString(fileData, function (parseError, xml) { if (parseError) { reject(new Error(`Unable to parse the manifest file: ${manifestPath}. \n${parseError}`)); } else { resolve(xml); } }); }); }); } readFromManifestFile() { return __awaiter(this, void 0, void 0, function* () { try { const fileData = yield util.promisify(fs.readFile)(this.manifestPath, { encoding: "utf8", }); return fileData; } catch (err) { throw new Error(`Unable to read data for manifest file: ${this.manifestPath}.\n${err}`); } }); } setModifiedXmlData(xml, guid, displayName) { if (typeof guid !== "undefined") { if (!guid || guid === "random") { guid = (0, uuid_1.v4)(); } xmlMethods.setXmlElementValue(xml, "Id", guid); } if (typeof displayName !== "undefined") { xmlMethods.setXmlElementAttributeValue(xml, "DisplayName", displayName); } } writeManifestData(manifestData) { return __awaiter(this, void 0, void 0, function* () { let xml; try { // Generate xml for the manifest data. const builder = new xml2js.Builder(); xml = builder.buildObject(manifestData); } catch (err) { throw new Error(`Unable to generate xml for the manifest.\n${err}`); } try { // Write the xml back to the manifest file. yield writeFileAsync(this.manifestPath, xml); } catch (err) { throw new Error(`Unable to write to file. ${this.manifestPath} \n${err}`); } }); } } exports.ManifestHandlerXml = ManifestHandlerXml; //# sourceMappingURL=manifestHandlerXml.js.map