|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as util from "util"; |
| 3 | +import * as xml2js from "xml2js"; |
| 4 | +import * as xmlMethods from "./xml"; |
| 5 | +const readFileAsync = util.promisify(fs.readFile); |
| 6 | +const uuid = require('uuid/v1'); |
| 7 | +const writeFileAsync = util.promisify(fs.writeFile); |
| 8 | +type Xml = any; |
| 9 | + |
| 10 | +export class ManifestInfo { |
| 11 | + public id?: string; |
| 12 | + public defaultLocale?: string; |
| 13 | + public description?: string; |
| 14 | + public displayName?: string; |
| 15 | + public officeAppType?: string; |
| 16 | + public providerName?: string; |
| 17 | + public version?: string; |
| 18 | +} |
| 19 | + |
| 20 | +function parseManifest(xml: Xml): ManifestInfo { |
| 21 | + const manifest: ManifestInfo = { }; |
| 22 | + const officeApp = xml.OfficeApp; |
| 23 | + |
| 24 | + manifest.id = xmlMethods.getXmlElementValue(officeApp, "Id"); |
| 25 | + manifest.officeAppType = xmlMethods.getXmlAttributeValue(officeApp, "xsi:type"); |
| 26 | + manifest.defaultLocale = xmlMethods.getXmlElementValue(officeApp, "DefaultLocale"); |
| 27 | + manifest.description = xmlMethods.getXmlElementAttributeValue(officeApp, "Description"); |
| 28 | + manifest.displayName = xmlMethods.getXmlElementAttributeValue(officeApp, "DisplayName"); |
| 29 | + manifest.providerName = xmlMethods.getXmlElementValue(officeApp, "ProviderName"); |
| 30 | + manifest.version = xmlMethods.getXmlElementValue(officeApp, "Version"); |
| 31 | + |
| 32 | + return manifest; |
| 33 | +} |
| 34 | + |
| 35 | +export async function modifyManifestFile(manifestPath: string, guid?: string, displayName?: string): Promise<ManifestInfo> { |
| 36 | + let manifestData: ManifestInfo = {}; |
| 37 | + if (manifestPath) { |
| 38 | + if (!guid && !displayName) { |
| 39 | + throw new Error("You need to specify something to change in the manifest."); |
| 40 | + } else { |
| 41 | + manifestData = await modifyManifestXml(manifestPath, guid, displayName); |
| 42 | + await writeManifestData(manifestPath, manifestData); |
| 43 | + return await readManifestFile(manifestPath); |
| 44 | + } |
| 45 | + } else { |
| 46 | + throw new Error(`Please provide the path to the manifest file.`); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +async function modifyManifestXml(manifestPath: string, guid?: string, displayName?: string): Promise<Xml> { |
| 51 | + try { |
| 52 | + const manifestXml: Xml = await readXmlFromManifestFile(manifestPath); |
| 53 | + setModifiedXmlData(manifestXml.OfficeApp, guid, displayName); |
| 54 | + return manifestXml; |
| 55 | + } catch (err) { |
| 56 | + throw new Error(`Unable to modify xml data for manifest file: ${manifestPath}. \n${err}`); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +async function parseXmlAsync(xmlString: string, manifestPath: string): Promise<Xml> { |
| 61 | + return new Promise(async function(resolve, reject) { |
| 62 | + xml2js.parseString(xmlString, function(parseError, xml) { |
| 63 | + if (parseError) { |
| 64 | + reject(new Error(`Unable to parse the manifest file: ${manifestPath}. \n${parseError}`)); |
| 65 | + } else { |
| 66 | + resolve(xml); |
| 67 | + } |
| 68 | + }); |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +export async function readManifestFile(manifestPath: string): Promise<ManifestInfo> { |
| 73 | + if (manifestPath) { |
| 74 | + const xml = await readXmlFromManifestFile(manifestPath); |
| 75 | + const manifest: ManifestInfo = parseManifest(xml); |
| 76 | + return manifest; |
| 77 | + } else { |
| 78 | + throw new Error(`Please provide the path to the manifest file.`); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +async function readXmlFromManifestFile(manifestPath: string): Promise<Xml> { |
| 83 | + const fileData: string = await readFileAsync(manifestPath, {encoding: "utf8"}); |
| 84 | + const xml = await parseXmlAsync(fileData, manifestPath); |
| 85 | + return xml; |
| 86 | +} |
| 87 | + |
| 88 | +function setModifiedXmlData(xml: any, guid: string | undefined, displayName: string | undefined) { |
| 89 | + if (guid) { |
| 90 | + if (guid === "random") { |
| 91 | + guid = uuid(); |
| 92 | + } |
| 93 | + xmlMethods.setXmlElementValue(xml, "Id", guid); |
| 94 | + } |
| 95 | + |
| 96 | + if (displayName) { |
| 97 | + xmlMethods.setXmlElementAttributeValue(xml, "DisplayName", displayName); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +async function writeManifestData(manifestPath: string, manifestData: any): Promise<void> { |
| 102 | + let xml: Xml; |
| 103 | + |
| 104 | + try { |
| 105 | + // Generate xml for the manifest data. |
| 106 | + const builder = new xml2js.Builder(); |
| 107 | + xml = builder.buildObject(manifestData); |
| 108 | + } catch (err) { |
| 109 | + throw new Error(`Unable to generate xml for the manifest.\n${err}`); |
| 110 | + } |
| 111 | + |
| 112 | + try { |
| 113 | + // Write the xml back to the manifest file. |
| 114 | + await writeFileAsync(manifestPath, xml); |
| 115 | + } catch (err) { |
| 116 | + throw new Error(`Unable to write to file. ${manifestPath} \n${err}`); |
| 117 | + } |
| 118 | +} |
0 commit comments