From 334b52f4c60bdbab95d05b36e68a316141c055ae Mon Sep 17 00:00:00 2001 From: C-3PO Date: Fri, 9 Nov 2018 02:23:01 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Check=20for=20new=20manifest=20vers?= =?UTF-8?q?ion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 30 +++++++++++++++++++++++++----- src/interfaces/IDatabaseRow.ts | 7 ++----- src/interfaces/IProductData.ts | 6 ++++++ src/model/model.ts | 13 +++++++++++-- 4 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 src/interfaces/IProductData.ts diff --git a/src/index.ts b/src/index.ts index 8e4ecfd..8485c22 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,16 +1,36 @@ -import { getSolidpkg, Product } from 'ssn'; -import IDatabaseRow from './interfaces/IDatabaseRow'; +import { getSolidpkg, Product, getManifest } from 'ssn'; +import IProductData from "./interfaces/IProductData"; import * as model from './model/model'; import * as database from './model/database'; +import writeLog from './logger/writeLog'; //create lock file to prevent conflicts? const newPatches: any[] = []; -model.init().then(function(result: any) { - console.log(result); +(async function() { + const productData: { [key in Product]: IProductData } = await model.init(); + const productDataEntries = Object.entries(productData) as Array<[Product, IProductData]>; + + await Promise.all(productDataEntries.map(async ([product, { manifest, http}]) => { + //Check for new manifest version + const { current } = await getManifest(product); + if (current < manifest) { + writeLog(`Product ${product} now has a smaller manifest version ${current}, down from ${manifest}.`); + return; + } + if (current > manifest) { + writeLog(`New manifest version: ${current} instead of ${manifest}`); + model.updateManifestVersion(product, current); + } + + //Check for new HTTP version + //TODO + })); + database.exit(); -}); +}()) + /*//for each product, check new versions Object.entries(products).forEach(([product, { manifestVersion, httpVersion }]: [string, IDatabaseRow]) => { diff --git a/src/interfaces/IDatabaseRow.ts b/src/interfaces/IDatabaseRow.ts index f6ae5d5..679a20a 100644 --- a/src/interfaces/IDatabaseRow.ts +++ b/src/interfaces/IDatabaseRow.ts @@ -1,10 +1,7 @@ import { Product } from "ssn"; +import IProductData from "./IProductData"; -export default interface IDatabaseRow { +export default interface IDatabaseRow extends IProductData { /** Name of the product. */ product: Product; - /** Latest release number of this product according to the manifest. */ - manifest: number; - /** Latest release number of this product available from the patch server. */ - http: number; } diff --git a/src/interfaces/IProductData.ts b/src/interfaces/IProductData.ts new file mode 100644 index 0000000..bad1115 --- /dev/null +++ b/src/interfaces/IProductData.ts @@ -0,0 +1,6 @@ +export default interface IProductData { + /** Latest release number of this product according to the manifest. */ + manifest: number; + /** Latest release number of this product available from the patch server. */ + http: number; +} diff --git a/src/model/model.ts b/src/model/model.ts index 10ed3d2..120ee97 100644 --- a/src/model/model.ts +++ b/src/model/model.ts @@ -4,10 +4,15 @@ import products from './products'; import writeLog from '../logger/writeLog'; import createStatement from './createStatement'; import IDatabaseRow from '../interfaces/IDatabaseRow'; +import IProductData from '../interfaces/IProductData'; const state: any = {}; -export async function init() { +function escapeProduct(product: Product) { + return String(product).replace(/'/g, '\\\''); +} + +export async function init(): Promise<{ [key in Product]: IProductData }> { await database.init(); //Only create table if it does not exist yet @@ -28,8 +33,12 @@ export async function init() { //If row does not exist, INSERT into table await Promise.all(products.filter((product) => state[product] === undefined).map(function(product) { state[product] = { http: -1, manifest: -1 }; - return database.query(`INSERT INTO \`ssn_versions\` (\`product\`, \`manifest\`, \`http\`) VALUES ('${product.replace(/'/g, '\\\'')}', '-1', '-1');`) + return database.query(`INSERT INTO \`ssn_versions\` (\`product\`, \`manifest\`, \`http\`) VALUES ('${escapeProduct(product)}', '-1', '-1');`) })); return state; } + +export function updateManifestVersion(product: Product, newVersion: number) { + return database.query(`UPDATE \`ssn_versions\` SET \`manifest\` = '${Number(newVersion)}' WHERE \`product\` = '${escapeProduct(product)}' COLLATE utf8mb4_bin;`); +}