Skip to content

Commit

Permalink
pass clack as part of the ttYEffects
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Jun 28, 2024
1 parent d3c069b commit 92039b2
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
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

0 comments on commit 92039b2

Please sign in to comment.