♻️ Remove any to improve code quality

This commit is contained in:
C-3PO 2018-11-16 02:04:09 +01:00
parent 1486384207
commit 171cfd8fd9
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
3 changed files with 47 additions and 20 deletions

View file

@ -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;
};
}

View file

@ -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[];
}

View file

@ -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);
//<RequiredRelease>289</RequiredRelease>
const RequiredRelease = PatchManifest.elements[2];
out.current = Number(RequiredRelease.elements[0].text);
const RequiredRelease = getChild(PatchManifest, 2);
out.current = Number(getContent(RequiredRelease));
//<Releases><Release><Id>0</Id><SHA1>53678f8057e52896a8145dca5c188ab3f24fa55f</SHA1></Release></Releases>
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: [] };
});
//<ReleaseUpdatePaths><ReleaseUpdatePath><From>287</From><To>288</To><ExtraDataItem>...</ExtraDataItem></ReleaseUpdatePath></ReleaseUpdatePaths
const ReleaseUpdatePaths = PatchManifest.elements[8];
ReleaseUpdatePaths.elements.forEach(function(ReleaseUpdatePath: any) {
const from = ReleaseUpdatePath.elements[0].elements[0].text;
const to = ReleaseUpdatePath.elements[1].elements[0].text;
const ReleaseUpdatePaths = getChild(PatchManifest, 8);
getChildren(ReleaseUpdatePaths).forEach(function(ReleaseUpdatePath: xmlJs.Element) {
const from = getContent(getChild(ReleaseUpdatePath, 0));
const to = getContent(getChild(ReleaseUpdatePath, 1));
//Release -1 does not exist but is a valid "from", e.g. for -1to0
if (from !== '-1') {
out.releases[from].to.push(Number(to));
@ -30,5 +46,13 @@ export default function parsePatchManifest(manifestFile: any): IManifest {
out.releases[to].from.push(Number(from));
});
//freeze values to prevent modification
Object.values(out).forEach(function(entry: IManifestRelease) {
Object.freeze(entry.from);
Object.freeze(entry.to);
Object.freeze(entry);
});
Object.freeze(out);
return out;
}