From 9ea67cdbb66d88e21b370e1869882dcc0bf3152f Mon Sep 17 00:00:00 2001 From: C-3PO Date: Sun, 21 Oct 2018 04:13:32 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20arg=20parser,=20improv=20erro?= =?UTF-8?q?r=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- package-lock.json | 21 ++++++++++++++++++--- package.json | 4 +++- src/failWithError.ts | 2 +- src/getManifest.ts | 3 ++- src/getPatchZip.ts | 38 ++++++++++++++++++++++++++++++++++++++ src/getReleaseZip.ts | 30 ------------------------------ src/getSolidpkg.ts | 3 ++- 8 files changed, 65 insertions(+), 38 deletions(-) create mode 100644 src/getPatchZip.ts delete mode 100644 src/getReleaseZip.ts diff --git a/README.md b/README.md index 9f2fd2e..bd87329 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ complete -W 'assets_swtor_de_de assets_swtor_en_us assets_swtor_fr_fr assets_swt ```bash node dist/getManifest.js assets_swtor_main node dist/getSolidPkg.js assets_swtor_main 126 127 -node dist/getReleaseZip.js assets_swtor_main 126 127 +node dist/getPatchZip.js --product assets_swtor_main --from 126 --to 127 node dist/installPatch.js assets_swtor_main 126 127 swtor_install assets_swtor_main 126 127 ``` diff --git a/package-lock.json b/package-lock.json index c7d77bb..3e86d40 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,21 @@ "integrity": "sha512-3TUHC3jsBAB7qVRGxT6lWyYo2v96BMmD2PTcl47H25Lu7UXtFH/2qqmKiVrnel6Ne//0TFYf6uvNX+HW2FRkLQ==", "dev": true }, + "@types/yargs": { + "version": "12.0.1", + "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-12.0.1.tgz", + "integrity": "sha512-UVjo2oH79aRNcsDlFlnQ/iJ67Jd7j6uSg7jUJP/RZ/nUjAh5ElmnwlD5K/6eGgETJUgCHkiWn91B8JjXQ6ubAw==", + "dev": true + }, + "@types/yargs-parser": { + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/@types/yargs-parser/-/yargs-parser-11.0.0.tgz", + "integrity": "sha512-ZfKjiy9zRHQWWRiQLeushSHE2IgHJ8U/OaABsNodkuufZoWED7Plz6egREZiE8dfrTAnrD8Rw/kUh2S6Iu1W0w==", + "dev": true, + "requires": { + "@types/yargs": "*" + } + }, "camelcase": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.0.0.tgz", @@ -25,15 +40,15 @@ "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" }, "ssn": { - "version": "git+https://git.jedipedia.net/swtor/ssn.git#f56f0c85e101fb8550536c1fbaf10a1c2431b783", + "version": "git+https://git.jedipedia.net/swtor/ssn.git#9a8a85bd59348089525373255aab87b5216d7eb1", "from": "git+https://git.jedipedia.net/swtor/ssn.git", "requires": { - "ssn-installer": "git+https://git.jedipedia.net/swtor/ssn-installer.git#01926cfe97a3166cff46a2061dda519f9c262d40", + "ssn-installer": "git+https://git.jedipedia.net/swtor/ssn-installer.git#9a5db3aa4eaa7e6b01a26de47151c2dbe6520c82", "xml-js": "^1.6.8" } }, "ssn-installer": { - "version": "git+https://git.jedipedia.net/swtor/ssn-installer.git#01926cfe97a3166cff46a2061dda519f9c262d40", + "version": "git+https://git.jedipedia.net/swtor/ssn-installer.git#9a5db3aa4eaa7e6b01a26de47151c2dbe6520c82", "from": "git+https://git.jedipedia.net/swtor/ssn-installer.git" }, "xml-js": { diff --git a/package.json b/package.json index 801c64e..6e01994 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ }, "main": "index.js", "scripts": { + "preinstall": "rm -rf node_modules && rm -f package-lock.json", "start": "rm -rf dist && tsc", "test": "echo \"Error: no test specified\" && exit 1" }, @@ -16,6 +17,7 @@ "yargs-parser": "^11.0.0" }, "devDependencies": { - "@types/node": "^10.12.0" + "@types/node": "^10.12.0", + "@types/yargs-parser": "^11.0.0" } } diff --git a/src/failWithError.ts b/src/failWithError.ts index 6520bd0..5dde098 100644 --- a/src/failWithError.ts +++ b/src/failWithError.ts @@ -1,6 +1,6 @@ export default function failWithError(usage: string, msg?: string) { if (msg !== undefined) { - process.stderr.write(`Error: ${msg.trim()}\n`); + process.stderr.write(`Error: ${String(msg).trim()}\n`); } process.stderr.write(`Usage: ${usage} \n`); process.exit(1); diff --git a/src/getManifest.ts b/src/getManifest.ts index 9a56178..6725d7d 100644 --- a/src/getManifest.ts +++ b/src/getManifest.ts @@ -16,5 +16,6 @@ if (!verifyProductName(product)) { //Get manifest and write output to stdout getManifest(product as Product).then((output: IManifest) => { process.stdout.write(JSON.stringify(output)); - //process.exit(0); +}).catch((error) => { + failFunction(error); }); diff --git a/src/getPatchZip.ts b/src/getPatchZip.ts new file mode 100644 index 0000000..d60fb42 --- /dev/null +++ b/src/getPatchZip.ts @@ -0,0 +1,38 @@ +import { getPatchZip, ISsnFileEntry, Product, verifyProductName } from 'ssn'; +import * as yargsParser from 'yargs-parser'; +import failWithError from './failWithError'; + +const failFunction = failWithError.bind(null, 'node dist/getPatchZip.js --product --from --to '); + +const args = yargsParser(process.argv.slice(2), { string: ['product', 'from', 'to'] }); + +//Check that product name is valid +if (args.product === undefined) { + failFunction(`product is a required argument but it was not specified.`); +} +if (!verifyProductName(args.product)) { + failFunction(`"${args.product.substring(0, 300)}" is not a valid product name.`); +} + +//Check that from is a valid number +if (args.from === undefined) { + failFunction(`from is a required argument but it was not specified.`); +} +if (!args.from.match(/^(-1|0|[1-9][0-9]{0,2})$/)) { + failFunction(`from value "${args.from.substring(0, 300)}" is not a valid integer; it must be in range [-1, 999].`); +} + +//Check that to is a valid number +if (args.to === undefined) { + failFunction(`to is a required argument but it was not specified.`); +} +if (!args.to.match(/^(0|[1-9][0-9]{0,2})$/)) { + failFunction(`to value "${args.to.substring(0, 300)}" is not a valid integer; it must be in range [0, 999].`); +} + +//Get the .zip file from this patch and write output to stdout +getPatchZip(args.product as Product, Number(args.from), Number(args.to)).then((output: ISsnFileEntry[]) => { + process.stdout.write(JSON.stringify(output)); +}).catch((error) => { + failFunction(error); +}); diff --git a/src/getReleaseZip.ts b/src/getReleaseZip.ts deleted file mode 100644 index 3a5b5a5..0000000 --- a/src/getReleaseZip.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { getReleaseZip, ISsnFileEntry, Product, verifyProductName } from 'ssn'; -import failWithError from './failWithError'; - -const failFunction = failWithError.bind(null, 'node dist/getSolidpkgZip.js '); - -if (process.argv.length !== 5) { - failFunction(`Expected 3 arguments but ${process.argv.length - 2} arguments were supplied.`); -} - -//Check that product name is valid -const product = process.argv[2]; -if (!verifyProductName(product)) { - failFunction(`"${product.substring(0, 300)}" is not a valid product name.`); -} - -//Check that from and to are valid numbers -const from = process.argv[3]; -const to = process.argv[4]; -if (!from.match(/^(-1|0|[1-9][0-9]{0,2})$/)) { - failFunction(`from value "${from.substring(0, 300)}" is not a valid integer; it must be in range [-1, 999].`); -} -if (!to.match(/^(0|[1-9][0-9]{0,2})$/)) { - failFunction(`to value "${to.substring(0, 300)}" is not a valid integer; it must be in range [0, 999].`); -} - -//Get the .zip file from this release and write output to stdout -getReleaseZip(product as Product, Number(from), Number(to)).then((output: ISsnFileEntry[]) => { - process.stdout.write(JSON.stringify(output)); - //process.exit(0); -}); diff --git a/src/getSolidpkg.ts b/src/getSolidpkg.ts index 7ff200f..6b172ba 100644 --- a/src/getSolidpkg.ts +++ b/src/getSolidpkg.ts @@ -26,5 +26,6 @@ if (!to.match(/^(0|[1-9][0-9]{0,2})$/)) { //Get solidpkg and write output to stdout getSolidpkg(product as Product, Number(from), Number(to)).then((output: ISolidSimple) => { process.stdout.write(JSON.stringify(output)); - //process.exit(0); +}).catch((error) => { + failFunction(error); });