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

if stdout is not a tty, replace clack.spinner by console.log #1473

Merged
merged 1 commit into from
Jun 28, 2024
Merged
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
6 changes: 2 additions & 4 deletions src/convert.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {existsSync} from "node:fs";
import {utimes, writeFile} from "node:fs/promises";
import {join} from "node:path/posix";
import * as clack from "@clack/prompts";
import wrapAnsi from "wrap-ansi";
import type {ClackEffects} from "./clack.js";
import {CliError} from "./error.js";
import {prepareOutput} from "./files.js";
import {getObservableUiOrigin} from "./observableApiClient.js";
import type {TtyEffects} from "./tty.js";
import {bold, cyan, faint, inverse, link, reset, defaultEffects as ttyEffects} from "./tty.js";
import {bold, cyan, defaultEffects as defaultTtyEffects, faint, inverse, link, reset} from "./tty.js";

export interface ConvertEffects extends TtyEffects {
clack: ClackEffects;
Expand All @@ -19,8 +18,7 @@ export interface ConvertEffects extends TtyEffects {
}

const defaultEffects: ConvertEffects = {
...ttyEffects,
clack,
...defaultTtyEffects,
async prepareOutput(outputPath: string): Promise<void> {
await prepareOutput(outputPath);
},
Expand Down
2 changes: 0 additions & 2 deletions src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {basename, dirname, join, normalize} from "node:path/posix";
import {setTimeout as sleep} from "node:timers/promises";
import {fileURLToPath} from "node:url";
import {promisify} from "node:util";
import * as clack from "@clack/prompts";
import he from "he";
import untildify from "untildify";
import wrapAnsi from "wrap-ansi";
Expand All @@ -25,7 +24,6 @@ export interface CreateEffects extends TtyEffects {

const defaultEffects: CreateEffects = {
...defaultTtyEffects,
clack,
sleep,
async mkdir(outputPath: string, options): Promise<void> {
await mkdir(outputPath, options);
Expand Down
2 changes: 0 additions & 2 deletions src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {createHash} from "node:crypto";
import type {Stats} from "node:fs";
import {readFile, stat} from "node:fs/promises";
import {join} from "node:path/posix";
import * as clack from "@clack/prompts";
import wrapAnsi from "wrap-ansi";
import type {BuildEffects, BuildManifest, BuildOptions} from "./build.js";
import {FileBuildEffects, build} from "./build.js";
Expand Down Expand Up @@ -73,7 +72,6 @@ const defaultEffects: DeployEffects = {
...defaultAuthEffects,
getDeployConfig,
setDeployConfig,
clack,
logger: console,
input: process.stdin,
output: process.stdout,
Expand Down
2 changes: 0 additions & 2 deletions src/observableApiAuth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os from "node:os";
import * as clack from "@clack/prompts";
import type {ClackEffects} from "./clack.js";
import {commandInstruction, commandRequiresAuthenticationMessage} from "./commandInstruction.js";
import {CliError, isHttpError} from "./error.js";
Expand Down Expand Up @@ -30,7 +29,6 @@ export interface AuthEffects extends ConfigEffects, TtyEffects {
export const defaultEffects: AuthEffects = {
...defaultConfigEffects,
...defaultTtyEffects,
clack,
getObservableApiKey,
setObservableApiKey,
exitSuccess: () => process.exit(0)
Expand Down
16 changes: 16 additions & 0 deletions src/tty.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {isatty} from "node:tty";
import * as clack from "@clack/prompts";
import type {ClackEffects} from "./clack.js";
import type {Logger} from "./logger.js";

export const reset = color(0, 0);
Expand All @@ -22,12 +24,26 @@ function color(code: number, reset: number): TtyColor {
}

export interface TtyEffects {
clack: ClackEffects;
isTty: boolean;
logger: Logger;
outputColumns: number;
}

const noSpinner = () => ({
start(msg?: string) {
console.log(msg);
},
stop(msg?: string, code?: number) {
console.log(msg, code ?? "");
},
message(msg?: string) {
console.log(msg);
}
});

export const defaultEffects: TtyEffects = {
clack: process.stdout.isTTY ? clack : {...clack, spinner: noSpinner},
isTty: isatty(process.stdin.fd),
logger: console,
outputColumns: Math.min(80, process.stdout.columns ?? 80)
Expand Down
2 changes: 2 additions & 0 deletions test/tty-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from "node:assert";
import * as clack from "@clack/prompts";
import type {TtyEffects} from "../src/tty.js";
import {blue, bold, green, hangingIndentLog, red} from "../src/tty.js";

Expand Down Expand Up @@ -50,6 +51,7 @@ describe("hangingIndentLog", () => {
});

const noopEffects: TtyEffects = {
clack,
isTty: true,
logger: {log() {}, warn() {}, error() {}},
outputColumns: 80
Expand Down