From e93190c72317d7fca0ea39bdbea47a0d0edbf396 Mon Sep 17 00:00:00 2001 From: C-3PO Date: Thu, 21 Jun 2018 14:17:21 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20config=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 9 ++++++ package-lock.json | 13 +++++++++ package.json | 3 ++ src/config.ts | 67 +++++++++++++++++++++++++++++++++++++++++++ src/enums/releases.ts | 22 ++++++++++++++ src/index.ts | 10 +++++++ tsconfig.json | 4 +-- 7 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 .editorconfig create mode 100644 package-lock.json create mode 100644 src/config.ts create mode 100644 src/enums/releases.ts diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..74fb9e0 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true +end_of_line = lf diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..eb5cfdf --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "patcher", + "requires": true, + "lockfileVersion": 1, + "dependencies": { + "@types/node": { + "version": "10.3.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.3.4.tgz", + "integrity": "sha512-YMLlzdeNnAyLrQew39IFRkMacAR5BqKGIEei9ZjdHsIZtv+ZWKYTu1i7QJhetxQ9ReXx8w5f+cixdHZG3zgMQA==", + "dev": true + } + } +} diff --git a/package.json b/package.json index 107a0e4..380a664 100644 --- a/package.json +++ b/package.json @@ -4,5 +4,8 @@ "scripts": { "start": "tsc && node dist/index.js", "test": "echo \"Error: no test specified\" && exit 1" + }, + "devDependencies": { + "@types/node": "^10.3.4" } } diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..113ae0f --- /dev/null +++ b/src/config.ts @@ -0,0 +1,67 @@ +interface Settings { + [key: string]: any, + product?: 'assets_swtor_de_de' | 'assets_swtor_en_us' | 'assets_swtor_fr_fr' | 'assets_swtor_main' | 'assets_swtor_test_de_de' | 'assets_swtor_test_en_us' | 'assets_swtor_test_fr_fr' | 'assets_swtor_test_main' | 'eualas' | 'movies_de_de' | 'movies_en_us' | 'movies_fr_fr' | 'patcher2014' | 'patcher2017' | 'retailclient_betatest' | 'retailclient_cstraining' | 'retailclient_liveeptest' | 'retailclient_publictest' | 'retailclient_squadron157' | 'retailclient_swtor' + release?: number, //TODO: allow 'current', but how will we know what current version is? + from?: number, + outputType?: 'info' | 'file', //whether to just show JSON information, or actually write files into a directory +} + +const allowedProducts: Settings["product"][] = ['assets_swtor_de_de', 'assets_swtor_en_us', 'assets_swtor_fr_fr', 'assets_swtor_main', 'assets_swtor_test_de_de', 'assets_swtor_test_en_us', 'assets_swtor_test_fr_fr', 'assets_swtor_test_main', 'eualas', 'movies_de_de', 'movies_en_us', 'movies_fr_fr', 'patcher2014', 'patcher2017', 'retailclient_betatest', 'retailclient_cstraining', 'retailclient_liveeptest', 'retailclient_publictest', 'retailclient_squadron157', 'retailclient_swtor']; + +const settings: Settings = {}; + +/** Sets the given setting to the given value. Throws an error if key is invalid, or value doesn't match the key. */ +export const set = (key: string, value: any) => { + switch (key) { + case 'product': + //TODO: need to verify input (one of allowed products) + if (typeof value !== 'string') { throw new Error(`product must be a string but it's a "${typeof value}".`); } + if (!allowedProducts.includes(value as Settings["product"])) { throw new Error(`"${value}" is not an allowed product.`); } + settings.product = value as Settings["product"]; + break; + case 'release': + //verify input (must be a number >=0, and >settings.from) + if (typeof value !== 'number') { throw new Error(`release must be a number but it's a "${typeof value}".`); } + if ((value | 0) !== value) { throw new Error(`release must be an integer but it's ${value}.`); } + if (value < 0) { throw new Error(`release must be a non-negative integer but it's ${value}.`); } + if (settings.from !== undefined && value <= settings.from ) { throw new Error(`release must be bigger than from but ${value} is not bigger than ${settings.from}.`); } + settings.release = value; + break; + case 'from': + //TODO: need to verify input (it's a number >=-1 and = settings.release ) { throw new Error(`from must be smaller than release but ${value} is not smaller than ${settings.release}.`); } + settings.from = value; + break; + case 'outputType': + //need to verify input (it's info or file) + if (typeof value !== 'string') { throw new Error(`outputType must be a string but it's a "${typeof value}".`); } + if (value !== 'info' && value !== 'file') { throw new Error(`outputType must be "info" or "file" but it's "${value}".`); } + settings.outputType = value; + break; + default: + throw new Error(`The configuration setting ${key} does not exist.`); + } +}; + +/** Verify that all required settings were set. Throws an error if not. */ +export const verify = () => { + if (settings.product === undefined) { + throw new Error('No product set.'); + } + if (settings.release === undefined) { + throw new Error('No release set.'); + } + if (settings.from === undefined) { + throw new Error('No from set.'); + } + if (settings.outputType === undefined) { + throw new Error('No outputType set.'); + } + return true; +}; + +/** Gets the value for the given setting. */ +export const get = (key: string) => settings[key]; diff --git a/src/enums/releases.ts b/src/enums/releases.ts new file mode 100644 index 0000000..16e545f --- /dev/null +++ b/src/enums/releases.ts @@ -0,0 +1,22 @@ +export const enum Releases { + 'assets_swtor_de_de', + 'assets_swtor_en_us', + 'assets_swtor_fr_fr', + 'assets_swtor_main', + 'assets_swtor_test_de_de', + 'assets_swtor_test_en_us', + 'assets_swtor_test_fr_fr', + 'assets_swtor_test_main', + 'eualas', + 'movies_de_de', + 'movies_en_us', + 'movies_fr_fr', + 'patcher2014', + 'patcher2017', + 'retailclient_betatest', + 'retailclient_cstraining', + 'retailclient_liveeptest', + 'retailclient_publictest', + 'retailclient_squadron157', + 'retailclient_swtor', +}; diff --git a/src/index.ts b/src/index.ts index adc9537..5f2a154 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,15 @@ +import * as config from './config'; + console.log('Hello world!'); +//TODO: read arguments from command-line instead: process.argv +config.set('product', 'assets_swtor_en_us'); +config.set('release', 120); +config.set('from', -1); +config.set('outputType', 'info'); +//config.set('output', '/home/c3po/swtor-test/'); +config.verify(); + /*TODO: - Configuration (which patch to download) Need to install patch X for product Y, given install path Z and optionally previous version A already installed in path B. diff --git a/tsconfig.json b/tsconfig.json index 09dd67a..358f9e7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ //settings: "baseUrl": ".", "inlineSourceMap": true, - "module": "ES2015", + "module": "commonjs", "moduleResolution": "node", "newLine": "lf", "outDir": "dist", @@ -13,7 +13,7 @@ ] }, "strict": true, - "target": "es2018", + "target": "es2017", }, "include": [ "src/**/*"