Skip to content

Commit

Permalink
fix(*): throw when no html/out file specified
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Dec 11, 2019
1 parent 4f0a8dd commit 9e0bb0b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -112,12 +116,12 @@ function main(params, read = fs.readFileSync, write = fs.writeFileSync, timestam

const body = findElementByName(document, 'body');
if (!body) {
throw ('No <body> tag found in HTML document');
throw new Error('No <body> tag found in HTML document');
}

const head = findElementByName(document, 'head');
if (!head) {
throw ('No <head> tag found in HTML document');
throw new Error('No <head> tag found in HTML document');
}

function removeRootPath(p) {
Expand Down
24 changes: 14 additions & 10 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 9e0bb0b

Please sign in to comment.