diff --git a/src/interfaces/IManifest.ts b/src/interfaces/IManifest.ts index 7bdca8e..32ab0d7 100644 --- a/src/interfaces/IManifest.ts +++ b/src/interfaces/IManifest.ts @@ -1,16 +1,11 @@ +import IManifestRelease from './IManifestRelease'; + /** A patch manifest, containing a list of releases, how to patch them, and which one the current release is. */ export default interface IManifest { /** Current release. */ current: number; /** List of releases. */ releases: { - [s: string]: { - /** SHA1 hash of this release. */ - sha1: string; - /** List of releases from where we can patch to this release. */ - from: number[]; - /** List of releases that this release can be patched to. */ - to: number[]; - }; + [s: string]: IManifestRelease; }; } diff --git a/src/interfaces/IManifestRelease.ts b/src/interfaces/IManifestRelease.ts new file mode 100644 index 0000000..59115bd --- /dev/null +++ b/src/interfaces/IManifestRelease.ts @@ -0,0 +1,8 @@ +export default interface IManifestRelease { + /** SHA1 hash of this release. */ + sha1: string; + /** List of releases from where we can patch to this release. */ + from: number[]; + /** List of releases that this release can be patched to. */ + to: number[]; +} diff --git a/src/ssn/reader/parsePatchmanifest.ts b/src/ssn/reader/parsePatchmanifest.ts index 0dac18d..d34d351 100644 --- a/src/ssn/reader/parsePatchmanifest.ts +++ b/src/ssn/reader/parsePatchmanifest.ts @@ -1,28 +1,44 @@ +import * as xmlJs from 'xml-js'; import IManifest from '../../interfaces/IManifest'; +import IManifestRelease from '../../interfaces/IManifestRelease'; + +/** Given an xmlJs element, returns the child elements, or an empty array if it has no children. */ +function getChildren(ele: xmlJs.Element): xmlJs.Element[] { + return ele.elements !== undefined ? ele.elements : []; +} +/** Given an xmlJs element, returns the child at the given index. Throws an error if that element does not exist. */ +function getChild(ele: xmlJs.Element, index: number): xmlJs.Element { + return (ele.elements as xmlJs.Element[])[index] as xmlJs.Element; +} +/** Given an xmlJs element, returns the text contents. */ +function getContent(ele: xmlJs.Element): string { + const childElements = getChildren(ele); + return (childElements.length > 0) ? String(childElements[0].text) : ''; +} /** Receives a JSON-converted version of the manifest.xml file, and returns an easier to read JSON file */ -export default function parsePatchManifest(manifestFile: any): IManifest { +export default function parsePatchManifest(manifestFile: xmlJs.Element): IManifest { const out: IManifest = { current: -1, releases: {} }; - const PatchManifest = manifestFile.elements[0]; + const PatchManifest = getChild(manifestFile, 0); //289 - const RequiredRelease = PatchManifest.elements[2]; - out.current = Number(RequiredRelease.elements[0].text); + const RequiredRelease = getChild(PatchManifest, 2); + out.current = Number(getContent(RequiredRelease)); //053678f8057e52896a8145dca5c188ab3f24fa55f - const Releases = PatchManifest.elements[7]; - Releases.elements.forEach(function(Release: any) { - const id = Release.elements[0].elements[0].text; - const sha1 = Release.elements[1].elements[0].text; + const Releases = getChild(PatchManifest, 7); + getChildren(Releases).forEach(function(Release: xmlJs.Element) { + const id = getContent(getChild(Release, 0)); + const sha1 = getContent(getChild(Release, 1)); out.releases[id] = { sha1, from: [], to: [] }; }); //287288...