UNPKG

7.18 kBJavaScriptView Raw
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license.
3var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5 return new (P || (P = Promise))(function (resolve, reject) {
6 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9 step((generator = generator.apply(thisArg, _arguments || [])).next());
10 });
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13const fs = require("fs");
14const util = require("util");
15const uuid = require("uuid");
16const xml2js = require("xml2js");
17const xmlMethods = require("./xml");
18const readFileAsync = util.promisify(fs.readFile);
19const writeFileAsync = util.promisify(fs.writeFile);
20class DefaultSettings {
21}
22class ManifestInfo {
23}
24exports.ManifestInfo = ManifestInfo;
25function parseManifest(xml) {
26 const manifest = new ManifestInfo();
27 const officeApp = xml.OfficeApp;
28 if (officeApp) {
29 const defaultSettingsXml = xmlMethods.getXmlElement(officeApp, "DefaultSettings");
30 manifest.id = xmlMethods.getXmlElementValue(officeApp, "Id");
31 manifest.allowSnapshot = xmlMethods.getXmlElementValue(officeApp, "AllowSnapshot");
32 manifest.alternateId = xmlMethods.getXmlElementValue(officeApp, "AlternateId");
33 manifest.appDomains = xmlMethods.getXmlElementsValue(officeApp, "AppDomains", "AppDomain");
34 manifest.defaultLocale = xmlMethods.getXmlElementValue(officeApp, "DefaultLocale");
35 manifest.description = xmlMethods.getXmlElementAttributeValue(officeApp, "Description");
36 manifest.displayName = xmlMethods.getXmlElementAttributeValue(officeApp, "DisplayName");
37 manifest.highResolutionIconUrl = xmlMethods.getXmlElementAttributeValue(officeApp, "HighResolutionIconUrl");
38 manifest.hosts = xmlMethods.getXmlElementsAttributeValue(officeApp, "Hosts", "Host", "Name");
39 manifest.iconUrl = xmlMethods.getXmlElementAttributeValue(officeApp, "IconUrl");
40 manifest.officeAppType = xmlMethods.getXmlAttributeValue(officeApp, "xsi:type");
41 manifest.permissions = xmlMethods.getXmlElementValue(officeApp, "Permissions");
42 manifest.providerName = xmlMethods.getXmlElementValue(officeApp, "ProviderName");
43 manifest.supportUrl = xmlMethods.getXmlElementAttributeValue(officeApp, "SupportUrl");
44 manifest.version = xmlMethods.getXmlElementValue(officeApp, "Version");
45 if (defaultSettingsXml) {
46 const defaultSettings = new DefaultSettings();
47 defaultSettings.requestedHeight = xmlMethods.getXmlElementValue(defaultSettingsXml, "RequestedHeight");
48 defaultSettings.requestedWidth = xmlMethods.getXmlElementValue(defaultSettingsXml, "RequestedWidth");
49 defaultSettings.sourceLocation = xmlMethods.getXmlElementAttributeValue(defaultSettingsXml, "SourceLocation");
50 manifest.defaultSettings = defaultSettings;
51 }
52 }
53 return manifest;
54}
55function modifyManifestFile(manifestPath, guid, displayName) {
56 return __awaiter(this, void 0, void 0, function* () {
57 let manifestData = {};
58 if (manifestPath) {
59 if (guid === undefined && displayName === undefined) {
60 throw new Error("You need to specify something to change in the manifest.");
61 }
62 else {
63 manifestData = yield modifyManifestXml(manifestPath, guid, displayName);
64 yield writeManifestData(manifestPath, manifestData);
65 return yield readManifestFile(manifestPath);
66 }
67 }
68 else {
69 throw new Error(`Please provide the path to the manifest file.`);
70 }
71 });
72}
73exports.modifyManifestFile = modifyManifestFile;
74function modifyManifestXml(manifestPath, guid, displayName) {
75 return __awaiter(this, void 0, void 0, function* () {
76 try {
77 const manifestXml = yield readXmlFromManifestFile(manifestPath);
78 setModifiedXmlData(manifestXml.OfficeApp, guid, displayName);
79 return manifestXml;
80 }
81 catch (err) {
82 throw new Error(`Unable to modify xml data for manifest file: ${manifestPath}. \n${err}`);
83 }
84 });
85}
86function parseXmlAsync(xmlString, manifestPath) {
87 return __awaiter(this, void 0, void 0, function* () {
88 return new Promise(function (resolve, reject) {
89 return __awaiter(this, void 0, void 0, function* () {
90 xml2js.parseString(xmlString, function (parseError, xml) {
91 if (parseError) {
92 reject(new Error(`Unable to parse the manifest file: ${manifestPath}. \n${parseError}`));
93 }
94 else {
95 resolve(xml);
96 }
97 });
98 });
99 });
100 });
101}
102function readManifestFile(manifestPath) {
103 return __awaiter(this, void 0, void 0, function* () {
104 if (manifestPath) {
105 const xml = yield readXmlFromManifestFile(manifestPath);
106 const manifest = parseManifest(xml);
107 return manifest;
108 }
109 else {
110 throw new Error(`Please provide the path to the manifest file.`);
111 }
112 });
113}
114exports.readManifestFile = readManifestFile;
115function readXmlFromManifestFile(manifestPath) {
116 return __awaiter(this, void 0, void 0, function* () {
117 const fileData = yield readFileAsync(manifestPath, { encoding: "utf8" });
118 const xml = yield parseXmlAsync(fileData, manifestPath);
119 return xml;
120 });
121}
122function setModifiedXmlData(xml, guid, displayName) {
123 if (typeof (guid) !== "undefined") {
124 if (!guid || guid === "random") {
125 guid = uuid();
126 }
127 xmlMethods.setXmlElementValue(xml, "Id", guid);
128 }
129 if (typeof (displayName) !== "undefined") {
130 xmlMethods.setXmlElementAttributeValue(xml, "DisplayName", displayName);
131 }
132}
133function writeManifestData(manifestPath, manifestData) {
134 return __awaiter(this, void 0, void 0, function* () {
135 let xml;
136 try {
137 // Generate xml for the manifest data.
138 const builder = new xml2js.Builder();
139 xml = builder.buildObject(manifestData);
140 }
141 catch (err) {
142 throw new Error(`Unable to generate xml for the manifest.\n${err}`);
143 }
144 try {
145 // Write the xml back to the manifest file.
146 yield writeFileAsync(manifestPath, xml);
147 }
148 catch (err) {
149 throw new Error(`Unable to write to file. ${manifestPath} \n${err}`);
150 }
151 });
152}
153//# sourceMappingURL=manifestInfo.js.map
\No newline at end of file