office-addin-manifest
Version:
Read and modify Office Add-in manifest files.
159 lines • 8.12 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ManifestHandlerXml = void 0;
const fs_1 = __importDefault(require("fs"));
const util_1 = __importDefault(require("util"));
const uuid_1 = require("uuid");
const xml2js_1 = __importDefault(require("xml2js"));
const xmlMethods = __importStar(require("../xml"));
const manifestInfo_1 = require("../manifestInfo");
const manifestHandler_1 = require("./manifestHandler");
const writeFileAsync = util_1.default.promisify(fs_1.default.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_1.default.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_1.default.promisify(fs_1.default.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_1.default.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
;