From 9e0bb0bae880dfe791ca86a55f969c6f2b4d87a8 Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Wed, 11 Dec 2019 12:15:22 -0800 Subject: [PATCH] fix(*): throw when no html/out file specified --- src/main.js | 8 ++++++-- test/spec.js | 24 ++++++++++++++---------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/main.js b/src/main.js index ebda42d..ac31140 100644 --- a/src/main.js +++ b/src/main.js @@ -90,6 +90,10 @@ function parseArgs(cmdParams) { } } + if (!inputFile || !outputFile) { + throw new Error("required: --html, --out"); + } + // Normalize paths assets = assets.map(normalizePath); rootDirs = rootDirs.map(normalizeDirPath); @@ -112,12 +116,12 @@ function main(params, read = fs.readFileSync, write = fs.writeFileSync, timestam const body = findElementByName(document, 'body'); if (!body) { - throw ('No tag found in HTML document'); + throw new Error('No tag found in HTML document'); } const head = findElementByName(document, 'head'); if (!head) { - throw ('No tag found in HTML document'); + throw new Error('No tag found in HTML document'); } function removeRootPath(p) { diff --git a/test/spec.js b/test/spec.js index 4470f37..6016d39 100644 --- a/test/spec.js +++ b/test/spec.js @@ -159,26 +159,30 @@ describe("modules", () => { }); describe('parseArgs', () => { - const REQUIRE_PARAMS = ["--out", "./foo"]; + const REQUIRE_PARAMS = ["--out", "foo.html", "--html", "in.html"]; - it('should accept a single --out', () => { - const {outputFile} = parseArgs(["--out", "./foo"]); + it('should accept a single --out and --html', () => { + const {outputFile, inputFile} = parseArgs(["--out", "./foo.html", "--html", "index.html"]); - expect(outputFile).toBe("./foo"); + expect(outputFile).toBe("./foo.html"); + expect(inputFile).toBe("./index.html"); }); it('should throw with multiple --out', () => { - expect(() => parseArgs(["--out", "./foo", "./bar"])).toThrow(); + expect(() => parseArgs(["--html", "validhtml", "--out", "./foo", "./bar"])).toThrowError("Unknown arg: ./bar"); }); - it('should accept a single --html', () => { - const {inputFile} = parseArgs([...REQUIRE_PARAMS, "--html", "./foo"]); + it('should throw with multiple --html', () => { + expect(() => parseArgs(["--out", "foo", , "--html", "./foo", "./bar"])).toThrowError("Unknown arg: ./bar"); + }); - expect(inputFile).toBe("./foo"); + it('should throw with unknown arg', () => { + expect(() => parseArgs(["--badparam"])).toThrowError("Unknown arg: --badparam"); }); - it('should throw with multiple --html', () => { - expect(() => parseArgs([...REQUIRE_PARAMS, "--html", "./foo", "./bar"])).toThrow(); + it('should throw with no --out and --html', () => { + expect(() => parseArgs(["--out", "out"])).toThrowError("required: --html, --out"); + expect(() => parseArgs(["--html", "in"])).toThrowError("required: --html, --out"); }); it('should accept multiple assets', () => {