Skip to content

Commit

Permalink
feat(config): Improvements to fillInOpts
Browse files Browse the repository at this point in the history
  • Loading branch information
jcowman2 committed Dec 19, 2018
1 parent 80f0246 commit 266c946
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/get-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const fillInOpts = (
userOpts: RecursivePartial<LoadedConfiguration>
): LoadedConfiguration => {
if (userOpts.game === undefined || userOpts.game.name === undefined) {
throw new RegalError("game.name must be defined");
throw new RegalError("game.name must be defined.");
}

if (userOpts.bundler === undefined) {
Expand All @@ -79,7 +79,11 @@ export const fillInOpts = (
c.input = {};
}
if (c.input.ts === undefined) {
c.input.ts = true;
if (c.input.file !== undefined && c.input.file.endsWith("js")) {
c.input.ts = false;
} else {
c.input.ts = true;
}
}
if (c.input.file === undefined) {
const inputFile = c.input.ts ? "index.ts" : "index.js";
Expand Down
54 changes: 54 additions & 0 deletions test/unit/get-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ describe("Get Config", () => {
});

describe("fillInOpts", () => {
it("Errors if game.name is not defined", () => {
expect(() => fillInOpts(process.cwd(), {})).toThrow(
"RegalError: game.name must be defined."
);
});

it("Fills in all default values if nothing is specified", () => {
expect(
fillInOpts(process.cwd(), {
Expand All @@ -130,5 +136,53 @@ describe("Get Config", () => {
}
});
});

it("Does not fill in values if they're specified", () => {
const config: LoadedConfiguration = {
game: {
name: "My Cool Game",
author: "Joe Cowman",
options: {
seed: "bloop"
}
},
bundler: {
input: {
ts: true,
file: "start.ts"
},
output: {
file: "out.js",
bundle: BundleType.STANDARD,
format: ModuleFormat.ESM,
minify: true
}
}
};

const configCopy: LoadedConfiguration = JSON.parse(
JSON.stringify(config)
);

expect(fillInOpts(process.cwd(), configCopy)).toEqual(config);
});

it("input.ts defaults to false if input.file is specified as ending with .js", () => {
expect(
fillInOpts(process.cwd(), {
game: { name: "foo" },
bundler: { input: { file: "index.js" } }
}).bundler.input.ts
).toBe(false);
});

it("input.file defaults to .js extension if input.ts is specified as false", () => {
expect(
fillInOpts(process.cwd(), {
game: { name: "bar" },
bundler: { input: { ts: false } }
}).bundler.input.file
).toBe(path.join(process.cwd(), "src", "index.js"));
});
});
});

0 comments on commit 266c946

Please sign in to comment.