🎨 Improved code quality of manifest parser

This commit is contained in:
C-3PO 2018-06-23 21:52:36 +02:00
parent 62ed08c6c3
commit 57c99b88fe
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
3 changed files with 25 additions and 6 deletions

View file

@ -0,0 +1,16 @@
/** A patch manifest, containing a list of releases, how to patch them, and which 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[];
};
};
}

View file

@ -1,6 +1,7 @@
import { TextDecoder } from 'util'; import { TextDecoder } from 'util';
import * as xmlJs from 'xml-js'; import * as xmlJs from 'xml-js';
import getUrlContents from '../cdn/getUrlContents'; import getUrlContents from '../cdn/getUrlContents';
import IManifest from '../interfaces/IManifest';
import { Product } from '../interfaces/ISettings'; import { Product } from '../interfaces/ISettings';
import parsePatchmanifest from '../ssn/parsePatchmanifest'; import parsePatchmanifest from '../ssn/parsePatchmanifest';
import verifyPatchmanifest from '../ssn/verifyPatchmanifest'; import verifyPatchmanifest from '../ssn/verifyPatchmanifest';
@ -10,7 +11,7 @@ import readSsnFile from './readSsnFile';
const Decoder = new TextDecoder('utf-8'); const Decoder = new TextDecoder('utf-8');
export default async function getPatchmanifest(product: Product): Promise<any> { export default async function getPatchmanifest(product: Product): Promise<IManifest> {
//Verify function arguments //Verify function arguments
if (!verifyProductName(product)) { if (!verifyProductName(product)) {
throw new Error(`"${product}" is not a valid product.`); throw new Error(`"${product}" is not a valid product.`);

View file

@ -1,7 +1,8 @@
import IManifest from '../interfaces/IManifest';
/** Receives a JSON-converted version of the manifest.xml file, and returns an easier to read JSON file */ /** Receives a JSON-converted version of the manifest.xml file, and returns an easier to read JSON file */
export default function parsePatchManifest(manifestFile: any): any { export default function parsePatchManifest(manifestFile: any): IManifest {
const out: any = {}; const out: IManifest = { current: -1, releases: {} };
const PatchManifest = manifestFile.elements[0]; const PatchManifest = manifestFile.elements[0];
@ -11,21 +12,22 @@ export default function parsePatchManifest(manifestFile: any): any {
//<Releases><Release><Id>0</Id><SHA1>53678f8057e52896a8145dca5c188ab3f24fa55f</SHA1></Release></Releases> //<Releases><Release><Id>0</Id><SHA1>53678f8057e52896a8145dca5c188ab3f24fa55f</SHA1></Release></Releases>
const Releases = PatchManifest.elements[7]; const Releases = PatchManifest.elements[7];
out.releases = {};
Releases.elements.forEach((Release: any) => { Releases.elements.forEach((Release: any) => {
const id = Release.elements[0].elements[0].text; const id = Release.elements[0].elements[0].text;
const sha1 = Release.elements[1].elements[0].text; const sha1 = Release.elements[1].elements[0].text;
out.releases[id] = { sha1, from: [], to: [] }; out.releases[id] = { sha1, from: [], to: [] };
}); });
//<ReleaseUpdatePaths><ReleaseUpdatePath><From>287</From><To>288</To><ExtraDataItem>...</ExtraDataItem></ReleaseUpdatePath></ReleaseUpdatePaths
const ReleaseUpdatePaths = PatchManifest.elements[8]; const ReleaseUpdatePaths = PatchManifest.elements[8];
ReleaseUpdatePaths.elements.forEach((ReleaseUpdatePath: any) => { ReleaseUpdatePaths.elements.forEach((ReleaseUpdatePath: any) => {
const from = ReleaseUpdatePath.elements[0].elements[0].text; const from = ReleaseUpdatePath.elements[0].elements[0].text;
const to = ReleaseUpdatePath.elements[1].elements[0].text; const to = ReleaseUpdatePath.elements[1].elements[0].text;
//Release -1 does not exist but is a valid "from", e.g. for -1to0
if (from !== '-1') { if (from !== '-1') {
out.releases[from].to.push(to); out.releases[from].to.push(Number(to));
} }
out.releases[to].from.push(from); out.releases[to].from.push(Number(from));
}); });
return out; return out;