ssn-tools/src/getPatchZip.ts
2018-10-21 04:52:36 +02:00

40 lines
1.5 KiB
JavaScript

#!/usr/bin/env node
import { getPatchZip, ISsnFileEntry, Product, verifyProductName } from 'ssn';
import * as yargsParser from 'yargs-parser';
import failWithError from './funcs/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);
});