Add config module

This commit is contained in:
C-3PO 2018-06-21 14:17:21 +02:00
parent 9b0db464ef
commit e93190c723
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
7 changed files with 126 additions and 2 deletions

9
.editorconfig Normal file
View file

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

13
package-lock.json generated Normal file
View file

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

View file

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

67
src/config.ts Normal file
View file

@ -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 <setting.release)
if (typeof value !== 'number') { throw new Error(`from must be a number but it's a "${typeof value}".`); }
if ((value | 0) !== value) { throw new Error(`from must be an integer but it's ${value}.`); }
if (value < -1) { throw new Error(`from must be a non-negative integer or -1, but it's ${value}.`); }
if (settings.release !== undefined && value >= 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];

22
src/enums/releases.ts Normal file
View file

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

View file

@ -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.

View file

@ -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/**/*"