Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CLI wrapper install quieter #1177

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 37 additions & 24 deletions packages/driver/src/cli.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env node
import { execSync, type ExecSyncOptions } from "node:child_process";
import {
execSync,
type SpawnSyncReturns,
type ExecSyncOptions,
} from "node:child_process";
import { createWriteStream } from "node:fs";
import * as os from "node:os";
import * as fs from "node:fs/promises";
Expand All @@ -12,6 +16,8 @@ import Debug from "debug";
import which from "which";
import { quote } from "shell-quote";

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

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

const IS_TTY = process.stdout.isTTY;
Expand Down Expand Up @@ -61,11 +67,16 @@ async function main(args: string[]) {
null;

if (cliLocation === null) {
throw Error("Failed to find or install EdgeDB CLI.");
console.error("Failed to find or install EdgeDB CLI.");
process.exit(1);
}

const result = runEdgeDbCli(args, cliLocation);
if (result.tag === "Err") {
process.exit(result.error.status);
}

try {
runEdgeDbCli(args, cliLocation);
if (cliLocation !== maybeCachedCliLocation) {
debug("CLI location not cached.");
debug(` - Cached location: ${maybeCachedCliLocation}`);
Expand All @@ -75,17 +86,7 @@ async function main(args: string[]) {
debug("Cache updated.");
}
} catch (err) {
if (
typeof err === "object" &&
err !== null &&
"status" in err &&
typeof err.status === "number"
) {
process.exit(err.status);
} else {
console.error(err);
}

console.error(err);
process.exit(1);
}

Expand Down Expand Up @@ -214,11 +215,12 @@ async function selfInstallFromTempCli(): Promise<string | null> {
debug("Self-installing EdgeDB CLI...");
// n.b. need -y because in the Vercel build container, $HOME and euid-obtained
// home are different, and the CLI installation requires this as confirmation
const cmd = ["_self_install", "-y"];
if (!IS_TTY) {
cmd.push("--quiet");
const cmd = ["_self_install", "-y", "--quiet"];
const result = runEdgeDbCli(cmd, TEMPORARY_CLI_PATH);
if (result.tag === "Err") {
debug(" - CLI self-installation failed with error:", result.error);
return null;
}
runEdgeDbCli(cmd, TEMPORARY_CLI_PATH);
debug(" - CLI self-installed successfully.");
return getCliLocationFromCache();
}
Expand Down Expand Up @@ -247,10 +249,18 @@ function runEdgeDbCli(
args: string[],
pathToCli: string,
execOptions: ExecSyncOptions = { stdio: "inherit" },
) {
): Result<string | Buffer, Error & SpawnSyncReturns<string | Buffer>> {
const command = quote([pathToCli, ...args]);
debug(`Running EdgeDB CLI: ${command}`);
return execSync(command, execOptions);
try {
const result = execSync(command, execOptions);
return { tag: "Ok", value: result };
} catch (error: unknown) {
return {
tag: "Err",
error: error as Error & SpawnSyncReturns<string | Buffer>,
};
}
}

async function findPackage(): Promise<Package> {
Expand Down Expand Up @@ -385,11 +395,14 @@ function getBaseDist(arch: string, platform: string, libc = ""): string {

function getInstallDir(cliPath: string): string {
debug("Getting install directory for CLI path:", cliPath);
const installDir = runEdgeDbCli(["info", "--get", "install-dir"], cliPath, {
const result = runEdgeDbCli(["info", "--get", "install-dir"], cliPath, {
stdio: "pipe",
})
.toString()
.trim();
});
if (result.tag === "Err") {
debug(" - Failed to get install directory from CLI:", result.error);
throw result.error;
}
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>;
Loading