diff --git a/src/failWithError.ts b/src/failWithError.ts new file mode 100644 index 0000000..52cc177 --- /dev/null +++ b/src/failWithError.ts @@ -0,0 +1,7 @@ +export default function failWithError(usage: string, msg?: string) { + if (msg !== undefined) { + process.stderr.write(`${msg.trim()}\n`); + } + process.stderr.write(`Usage: ${usage} \n`); + process.exit(1); +} diff --git a/src/getManifest.ts b/src/getManifest.ts index 4153ca7..bb8e447 100644 --- a/src/getManifest.ts +++ b/src/getManifest.ts @@ -1,24 +1,19 @@ +import failWithError from './failWithError'; import IManifest from './interfaces/IManifest'; import { Product } from './interfaces/ISettings'; import getManifest from './ssn/getManifest'; import verifyProductName from './ssn/verify/verifyProductName'; -function failWithError(msg?: string) { - if (msg !== undefined) { - process.stderr.write(msg); - } - process.stderr.write('Usage: node dist/getManifest.js '); - process.exit(1); -} +const failFunction = failWithError.bind(null, 'node dist/getManifest.js '); -if (process.argv.length !== 2) { - failWithError(`Error: Expected 1 argument but ${process.argv.length - 1} arguments were supplied.`); +if (process.argv.length !== 3) { + failFunction(`Error: Expected 1 argument but ${process.argv.length - 2} arguments were supplied.`); } //Check that product name is valid -const product = process.argv[1]; +const product = process.argv[2]; if (!verifyProductName(product)) { - failWithError(`Error: "${product} is not a valid product name.`); + failFunction(`Error: "${product} is not a valid product name.`); } //Get manifest and write output to console diff --git a/src/getSolidpkg.ts b/src/getSolidpkg.ts index 442b2d7..7020f28 100644 --- a/src/getSolidpkg.ts +++ b/src/getSolidpkg.ts @@ -1,34 +1,29 @@ +import failWithError from './failWithError'; import { Product } from './interfaces/ISettings'; import ISolidSimple from './interfaces/ISolidSimple'; import getSolidpkg from './ssn/getSolidpkg'; import verifyProductName from './ssn/verify/verifyProductName'; -function failWithError(msg?: string) { - if (msg !== undefined) { - process.stderr.write(msg); - } - process.stderr.write('Usage: node dist/getSolidpkg.js '); - process.exit(1); -} +const failFunction = failWithError.bind(null, 'node dist/getSolidpkg.js '); -if (process.argv.length !== 4) { - failWithError(`Error: Expected 3 arguments but ${process.argv.length - 1} arguments were supplied.`); +if (process.argv.length !== 5) { + failFunction(`Error: Expected 3 arguments but ${process.argv.length - 2} arguments were supplied.`); } //Check that product name is valid -const product = process.argv[1]; +const product = process.argv[2]; if (!verifyProductName(product)) { - failWithError(`Error: "${product.substring(0, 300)}" is not a valid product name.`); + failFunction(`Error: "${product.substring(0, 300)}" is not a valid product name.`); } //Check that from and to are valid numbers -const from = process.argv[4]; -const to = process.argv[3]; +const from = process.argv[3]; +const to = process.argv[4]; if (!from.match(/^(-1|0|[1-9][0-9]{0,2})$/)) { - failWithError(`Error: from value "${from.substring(0, 300)}" is not a valid integer; it must be in range [-1, 999].`); + failFunction(`Error: 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})$/)) { - failWithError(`Error: to value "${to.substring(0, 300)}" is not a valid integer; it must be in range [0, 999].`); + failFunction(`Error: to value "${to.substring(0, 300)}" is not a valid integer; it must be in range [0, 999].`); } //Get solidpkg and write output to console