Skip to content

Commit

Permalink
Make Result type generic
Browse files Browse the repository at this point in the history
  • Loading branch information
scotttrinh committed Jan 17, 2025
1 parent 07c9ff9 commit d2fb314
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
20 changes: 5 additions & 15 deletions packages/driver/src/cli.mts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import Debug from "debug";
import which from "which";
import { quote } from "shell-quote";

import { type Result } from "./typeutil";

const debug = Debug("edgedb:cli");

const IS_TTY = process.stdout.isTTY;
Expand All @@ -32,18 +34,6 @@ interface Package {
installref: string;
}

interface Ok {
tag: "Ok";
stdout: string | Buffer;
}

interface Err {
tag: "Err";
error: Error & SpawnSyncReturns<string | Buffer>;
}

type Result = Ok | Err;

debug("Process argv:", process.argv);
// n.b. Using `npx`, the 3rd argument is the script name, unlike
// `node` where the 2nd argument is the script name.
Expand Down Expand Up @@ -259,12 +249,12 @@ function runEdgeDbCli(
args: string[],
pathToCli: string,
execOptions: ExecSyncOptions = { stdio: "inherit" },
): Result {
): Result<string | Buffer, Error & SpawnSyncReturns<string | Buffer>> {
const command = quote([pathToCli, ...args]);
debug(`Running EdgeDB CLI: ${command}`);
try {
const result = execSync(command, execOptions);
return { tag: "Ok", stdout: result };
return { tag: "Ok", value: result };
} catch (error: unknown) {
return {
tag: "Err",
Expand Down Expand Up @@ -412,7 +402,7 @@ function getInstallDir(cliPath: string): string {
debug(" - Failed to get install directory from CLI:", result.error);
throw result.error;
}
const installDir = result.stdout.toString().trim();
const installDir = result.value.toString().trim();
debug(" - Install directory:", installDir);
return installDir;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/driver/src/typeutil.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
export type Mutable<T> = {
-readonly [K in keyof T]: T[K];
};

export interface Ok<T> {
tag: "Ok";
value: T;
}

export interface Err<ErrorT> {
tag: "Err";
error: ErrorT;
}

export type Result<T, ErrorT> = Ok<T> | Err<ErrorT>;

0 comments on commit d2fb314

Please sign in to comment.