Skip to content

Commit

Permalink
feat: Remove app configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Mar 9, 2024
1 parent 390c749 commit c521af9
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 101 deletions.
4 changes: 0 additions & 4 deletions config.json

This file was deleted.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "montehall",
"version": "1.4.14",
"version": "1.5.0",
"description": "A Monte Carlo machine for the Monty Hall Problem",
"author": "Omar Boukli-Hacene",
"license": "MIT",
Expand Down
14 changes: 14 additions & 0 deletions src/app-params.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*!
Copyright (c) Omar Boukli-Hacene. All rights reserved.
Licensed under an MIT-style license.
SPDX-License-Identifier: MIT
*/

export const appParams = {
name: "montehall",
description: "Montehall: A Monte Carlo Machine for the Monty Hall Problem",
version: "1.5.0",
numGamesToSimulate: 16384,
numbersFilePath: "./data/numbers.txt",
};
64 changes: 14 additions & 50 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,31 @@ SPDX-License-Identifier: MIT

import { EOL } from "node:os";
import process from "node:process";
import { URL } from "node:url";

import { Command, InvalidArgumentError, Option } from "commander";
import { IPackageJson } from "package-json-type";

import { appParams } from "./app-params.mjs";
import {
AppConfig,
GameSummaryCallback,
SetupOptions,
monteCarloMachine,
} from "./montehall.mjs";
import {
gameSimulatorFactory,
getConfig,
RandomNumberProviderType,
rngFactory,
toErrString,
} from "./util.mjs";
import { gameSummaryFormatter } from "./formatters/game-summary-formatter.mjs";
import { simulationSummaryFormatter } from "./formatters/simulation-summary-formatter.mjs";

const pkgFileName = new URL("../package.json", import.meta.url);
const configFileName = new URL("../config.json", import.meta.url);

function buildCliCommand(
pkgInfo: IPackageJson,
defaultNumGames: number,
): Command {
function buildCliCommand(defaultNumGames: number): Command {
const program = new Command();

return program
.name(pkgInfo.name ?? "")
.description("Montehall: A Monte Carlo Machine for the Monty Hall Problem")
.version(
pkgInfo.version ?? "",
"-V, --version",
"Output version information",
)
.name(appParams.name)
.description(appParams.description)
.version(appParams.version, "-V, --version", "Output version information")
.helpOption("-h, --help", "Output usage information")
.option(
"-g, --games <number>",
Expand Down Expand Up @@ -93,35 +80,14 @@ function buildCliCommand(
/**
* CLI app entry point.
*/
async function main() {
let pkgInfo: IPackageJson;
let appConfig: AppConfig;

try {
const pkgInfoPromise = getConfig<IPackageJson>(pkgFileName);
const appConfigPromise = getConfig<AppConfig>(configFileName);

[pkgInfo, appConfig] = await Promise.all([
pkgInfoPromise,
appConfigPromise,
]);
} catch {
process.stderr.write(
`Could not read configuration. Check the "${pkgFileName.toString()}" and the "${configFileName.toString()}" files.${EOL}`,
);

return 1;
}

const defaultNumGames = Number(appConfig.numGamesToSimulate ?? 0);

const options = buildCliCommand(pkgInfo, defaultNumGames).opts();
function main() {
const options = buildCliCommand(appParams.numGamesToSimulate).opts();

const numGames = options.games as number;
const isPrudentPlayer = options.wise as boolean;

const numbersFilePath: string =
(options.tableFile as string) ?? appConfig.numbersFilePath ?? "";
(options.tableFile as string) ?? appParams.numbersFilePath;
if (options.random === "table") {
if (numbersFilePath === "") {
process.stdout.write(`Random number table file not specified.${EOL}`);
Expand Down Expand Up @@ -180,11 +146,9 @@ async function main() {
return 0;
}

main()
.then((x) => {
process.exitCode = x;
})
.catch((e) => {
process.stderr.write(`${toErrString(e)}${EOL}`);
process.exitCode = 1;
});
try {
process.exitCode = main();
} catch (e) {
process.stderr.write(`${toErrString(e)}${EOL}`);
process.exitCode = 1;
}
6 changes: 0 additions & 6 deletions src/montehall.mts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ export interface MonteCarloMachineResult {
readonly numSimulations: number;
}

export interface AppConfig {
readonly numGamesToSimulate: number;

readonly numbersFilePath: string;
}

export { generalSimulator } from "./general-simulator.mjs";

export { monteCarloMachine } from "./monte-carlo-machine.mjs";
Expand Down
19 changes: 0 additions & 19 deletions src/util.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ Licensed under an MIT-style license.
SPDX-License-Identifier: MIT
*/

import { PathLike } from "node:fs";
import { FileHandle, readFile } from "node:fs/promises";

import {
GameSimulator,
RandomNumberGenerator,
Expand All @@ -21,22 +18,6 @@ import { tableRng } from "./random/table.mjs";

export type RandomNumberProviderType = "basic" | "advanced" | "table";

/**
* Deserialize configuration JSON UTF-8 file.
*
* @param filename Path name of JSON file to deserialize
* @returns Configuration object
*/
export async function getConfig<T>(
filename: PathLike | FileHandle,
): Promise<T> {
const content = await readFile(filename, {
encoding: "utf8",
});

return JSON.parse(content) as T;
}

/**
* Create a random number provider.
*
Expand Down
20 changes: 1 addition & 19 deletions src/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ SPDX-License-Identifier: MIT

import { describe, expect, it } from "@jest/globals";

import { AppConfig } from "./montehall.mjs";

import {
gameSimulatorFactory,
getConfig,
rngFactory,
toErrString,
} from "./util.mjs";
import { gameSimulatorFactory, rngFactory, toErrString } from "./util.mjs";

describe("Function toErrString", () => {
it("should convert base Error message to string", () => {
Expand Down Expand Up @@ -54,17 +47,6 @@ describe("Function toErrString", () => {
});
});

describe("Function getConfig", () => {
it("should return valid object", async () => {
const appConfig = await getConfig<AppConfig>("config.json");

expect(appConfig).toStrictEqual({
numGamesToSimulate: 16384,
numbersFilePath: "./data/numbers.txt",
});
});
});

describe("Function rngFactory", () => {
it("when rngType is advanced should return a function", () => {
const rng = rngFactory("advanced");
Expand Down

0 comments on commit c521af9

Please sign in to comment.