From 92039b27bebbcd772f56709d7786035a01000bfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Fri, 28 Jun 2024 17:06:55 +0200 Subject: [PATCH] pass clack as part of the ttYEffects --- src/convert.ts | 6 ++---- src/create.ts | 2 -- src/deploy.ts | 2 -- src/observableApiAuth.ts | 2 -- src/tty.ts | 16 ++++++++++++++++ test/tty-test.ts | 2 ++ 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/convert.ts b/src/convert.ts index 8c86339f4..26696d803 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -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; @@ -19,8 +18,7 @@ export interface ConvertEffects extends TtyEffects { } const defaultEffects: ConvertEffects = { - ...ttyEffects, - clack, + ...defaultTtyEffects, async prepareOutput(outputPath: string): Promise { await prepareOutput(outputPath); }, diff --git a/src/create.ts b/src/create.ts index 6e081d7d4..ae1d2ca20 100644 --- a/src/create.ts +++ b/src/create.ts @@ -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"; @@ -25,7 +24,6 @@ export interface CreateEffects extends TtyEffects { const defaultEffects: CreateEffects = { ...defaultTtyEffects, - clack, sleep, async mkdir(outputPath: string, options): Promise { await mkdir(outputPath, options); diff --git a/src/deploy.ts b/src/deploy.ts index cbdfac7e5..a9a3bc15b 100644 --- a/src/deploy.ts +++ b/src/deploy.ts @@ -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"; @@ -73,7 +72,6 @@ const defaultEffects: DeployEffects = { ...defaultAuthEffects, getDeployConfig, setDeployConfig, - clack, logger: console, input: process.stdin, output: process.stdout, diff --git a/src/observableApiAuth.ts b/src/observableApiAuth.ts index 2b0f55845..b765378a3 100644 --- a/src/observableApiAuth.ts +++ b/src/observableApiAuth.ts @@ -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"; @@ -30,7 +29,6 @@ export interface AuthEffects extends ConfigEffects, TtyEffects { export const defaultEffects: AuthEffects = { ...defaultConfigEffects, ...defaultTtyEffects, - clack, getObservableApiKey, setObservableApiKey, exitSuccess: () => process.exit(0) diff --git a/src/tty.ts b/src/tty.ts index 2cdc025dc..89ef732c1 100644 --- a/src/tty.ts +++ b/src/tty.ts @@ -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); @@ -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) diff --git a/test/tty-test.ts b/test/tty-test.ts index 119036a03..00f4a7853 100644 --- a/test/tty-test.ts +++ b/test/tty-test.ts @@ -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"; @@ -50,6 +51,7 @@ describe("hangingIndentLog", () => { }); const noopEffects: TtyEffects = { + clack, isTty: true, logger: {log() {}, warn() {}, error() {}}, outputColumns: 80