Skip to content

Commit

Permalink
refactor: make Command.run a simple fn that returns a promise of Result
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsquest committed Nov 15, 2024
1 parent d44ed6e commit 8602678
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,29 @@

import { promises as fs } from "node:fs";
import { sep } from "node:path";
import {
type Composable,
type Result,
composable,
pipe,
success,
} from "composable-functions";
import { type Result, pipe, success } from "composable-functions";

type Command = {
type: string;
run: Composable;
run: () => Promise<Result>;
};

const usageCommand = (errorMessage?: string) =>
composable(async (): Promise<Result> => {
if (errorMessage) {
console.error(errorMessage);
console.error(); // blank line
}
console.log("Usage: npx mediathequeroubaix <command>");
console.log("Commands:");
console.log(" loans: List all loans");
console.log(" usage: Show this usage information");
return success(undefined);
});
const usageCommand = (errorMessage?: string) => async (): Promise<Result> => {
if (errorMessage) {
console.error(errorMessage);
console.error(); // blank line
}
console.log("Usage: npx mediathequeroubaix <command>");
console.log("Commands:");
console.log(" loans: List all loans");
console.log(" usage: Show this usage information");
return success(undefined);
};

const loanCommand = composable(async (): Promise<Result> => {
const loanCommand = async (): Promise<Result> => {
console.error("Not yet implemented");
return success(undefined);
});
};

const getHomeDir = (env: Record<string, string | undefined>): string => {
const home = env.HOME;
Expand Down Expand Up @@ -67,7 +60,7 @@ const printConfig = (config: object): Promise<void> => {
return Promise.resolve();
};

const showConfigCommand = composable(async (): Promise<Result> => {
const showConfigCommand = async (): Promise<Result> => {
const showConfig = pipe(
getHomeDir,
getConfigFilename,
Expand All @@ -77,7 +70,7 @@ const showConfigCommand = composable(async (): Promise<Result> => {
printConfig,
);
return showConfig(process.env);
});
};

const getCommand = (args: string[]): Command => {
if (args.length > 0) {
Expand Down

0 comments on commit 8602678

Please sign in to comment.