Add arg parser, improv error handling

This commit is contained in:
C-3PO 2018-10-21 04:13:32 +02:00
parent e6ac909bb0
commit 9ea67cdbb6
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
8 changed files with 65 additions and 38 deletions

View file

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

View file

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

38
src/getPatchZip.ts Normal file
View file

@ -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 <product> --from <from> --to <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);
});

View file

@ -1,30 +0,0 @@
import { getReleaseZip, ISsnFileEntry, Product, verifyProductName } from 'ssn';
import failWithError from './failWithError';
const failFunction = failWithError.bind(null, 'node dist/getSolidpkgZip.js <product> <from> <to>');
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);
});

View file

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