From c9c3d0f190c49d1237fdfda4ac3c4421c34efd85 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Mon, 15 Apr 2024 16:36:50 +0100 Subject: [PATCH] feat(cli-utils): Expose generate commands as exported functions (Re-add) (#207) --- .changeset/lazy-seas-hunt.md | 5 +++++ packages/cli-utils/src/api.ts | 4 ++++ .../src/commands/generate-output/index.ts | 9 +++++++++ .../src/commands/generate-output/runner.ts | 2 +- .../src/commands/generate-persisted/index.ts | 9 +++++++++ .../src/commands/generate-persisted/runner.ts | 2 +- .../src/commands/generate-schema/index.ts | 9 +++++++++ .../src/commands/generate-schema/runner.ts | 2 +- packages/cli-utils/src/commands/turbo/index.ts | 9 +++++++++ packages/cli-utils/src/commands/turbo/runner.ts | 2 +- packages/cli-utils/src/index.ts | 9 +++++++-- packages/cli-utils/src/term/tty.ts | 16 ++++++++++++---- 12 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 .changeset/lazy-seas-hunt.md create mode 100644 packages/cli-utils/src/api.ts diff --git a/.changeset/lazy-seas-hunt.md b/.changeset/lazy-seas-hunt.md new file mode 100644 index 00000000..8b887f79 --- /dev/null +++ b/.changeset/lazy-seas-hunt.md @@ -0,0 +1,5 @@ +--- +"@gql.tada/cli-utils": minor +--- + +Expose internal generate commands as functions exported by `@gql.tada/cli-utils` (restoring prior functionality) diff --git a/packages/cli-utils/src/api.ts b/packages/cli-utils/src/api.ts new file mode 100644 index 00000000..7ee2e54f --- /dev/null +++ b/packages/cli-utils/src/api.ts @@ -0,0 +1,4 @@ +export { generateOutput } from './commands/generate-output'; +export { generatePersisted } from './commands/generate-persisted'; +export { generateSchema } from './commands/generate-schema'; +export { generateTurbo } from './commands/turbo'; diff --git a/packages/cli-utils/src/commands/generate-output/index.ts b/packages/cli-utils/src/commands/generate-output/index.ts index 3930e92f..f00fa079 100644 --- a/packages/cli-utils/src/commands/generate-output/index.ts +++ b/packages/cli-utils/src/commands/generate-output/index.ts @@ -1,5 +1,6 @@ import { Command, Option } from 'clipanion'; +import type { Options } from './runner'; import { initTTY } from '../../term'; import { run } from './runner'; @@ -32,3 +33,11 @@ export class GenerateOutputCommand extends Command { return process.exitCode || (typeof result === 'object' ? result.exit : 0); } } + +export async function generateOutput(opts: Options): Promise { + const tty = initTTY({ disableTTY: true }); + const result = await tty.start(run(tty, opts)); + if (result instanceof Error) { + throw result; + } +} diff --git a/packages/cli-utils/src/commands/generate-output/runner.ts b/packages/cli-utils/src/commands/generate-output/runner.ts index a1c44a59..64e841a3 100644 --- a/packages/cli-utils/src/commands/generate-output/runner.ts +++ b/packages/cli-utils/src/commands/generate-output/runner.ts @@ -15,7 +15,7 @@ import type { WriteTarget } from '../shared'; import { writeOutput } from '../shared'; import * as logger from './logger'; -interface Options { +export interface Options { disablePreprocessing: boolean; output: string | undefined; tsconfig: string | undefined; diff --git a/packages/cli-utils/src/commands/generate-persisted/index.ts b/packages/cli-utils/src/commands/generate-persisted/index.ts index 11381a55..4ff38447 100644 --- a/packages/cli-utils/src/commands/generate-persisted/index.ts +++ b/packages/cli-utils/src/commands/generate-persisted/index.ts @@ -1,5 +1,6 @@ import { Command, Option } from 'clipanion'; +import type { Options } from './runner'; import { initTTY } from '../../term'; import { run } from './runner'; @@ -32,3 +33,11 @@ export class GeneratePersisted extends Command { return process.exitCode || (typeof result === 'object' ? result.exit : 0); } } + +export async function generatePersisted(opts: Options) { + const tty = initTTY({ disableTTY: true }); + const result = await tty.start(run(tty, opts)); + if (result instanceof Error) { + throw result; + } +} diff --git a/packages/cli-utils/src/commands/generate-persisted/runner.ts b/packages/cli-utils/src/commands/generate-persisted/runner.ts index 7a3ed6e9..7ee5e163 100644 --- a/packages/cli-utils/src/commands/generate-persisted/runner.ts +++ b/packages/cli-utils/src/commands/generate-persisted/runner.ts @@ -8,7 +8,7 @@ import type { WriteTarget } from '../shared'; import { writeOutput } from '../shared'; import * as logger from './logger'; -interface Options { +export interface Options { tsconfig: string | undefined; output: string | undefined; failOnWarn: boolean; diff --git a/packages/cli-utils/src/commands/generate-schema/index.ts b/packages/cli-utils/src/commands/generate-schema/index.ts index 06858fe5..b9005a9d 100644 --- a/packages/cli-utils/src/commands/generate-schema/index.ts +++ b/packages/cli-utils/src/commands/generate-schema/index.ts @@ -1,6 +1,7 @@ import * as t from 'typanion'; import { Command, Option } from 'clipanion'; +import type { Options } from './runner'; import { initTTY } from '../../term'; import { run } from './runner'; @@ -55,3 +56,11 @@ export class GenerateSchema extends Command { return process.exitCode || (typeof result === 'object' ? result.exit : 0); } } + +export async function generateSchema(opts: Options) { + const tty = initTTY({ disableTTY: true }); + const result = await tty.start(run(tty, opts)); + if (result instanceof Error) { + throw result; + } +} diff --git a/packages/cli-utils/src/commands/generate-schema/runner.ts b/packages/cli-utils/src/commands/generate-schema/runner.ts index 30caee23..3607852e 100644 --- a/packages/cli-utils/src/commands/generate-schema/runner.ts +++ b/packages/cli-utils/src/commands/generate-schema/runner.ts @@ -9,7 +9,7 @@ import type { WriteTarget } from '../shared'; import { writeOutput } from '../shared'; import * as logger from './logger'; -interface Options { +export interface Options { input: string; headers: Record | undefined; output: string | undefined; diff --git a/packages/cli-utils/src/commands/turbo/index.ts b/packages/cli-utils/src/commands/turbo/index.ts index 21fec4c9..4784af74 100644 --- a/packages/cli-utils/src/commands/turbo/index.ts +++ b/packages/cli-utils/src/commands/turbo/index.ts @@ -1,5 +1,6 @@ import { Command, Option } from 'clipanion'; +import type { Options } from './runner'; import { initTTY } from '../../term'; import { run } from './runner'; @@ -32,3 +33,11 @@ export class TurboCommand extends Command { return process.exitCode || (typeof result === 'object' ? result.exit : 0); } } + +export async function generateTurbo(opts: Options) { + const tty = initTTY({ disableTTY: true }); + const result = await tty.start(run(tty, opts)); + if (result instanceof Error) { + throw result; + } +} diff --git a/packages/cli-utils/src/commands/turbo/runner.ts b/packages/cli-utils/src/commands/turbo/runner.ts index 791a79d0..89a11748 100644 --- a/packages/cli-utils/src/commands/turbo/runner.ts +++ b/packages/cli-utils/src/commands/turbo/runner.ts @@ -10,7 +10,7 @@ import * as logger from './logger'; const PREAMBLE_IGNORE = ['/* eslint-disable */', '/* prettier-ignore */'].join('\n') + '\n'; -interface Options { +export interface Options { failOnWarn: boolean; tsconfig: string | undefined; output: string | undefined; diff --git a/packages/cli-utils/src/index.ts b/packages/cli-utils/src/index.ts index 8fd40f33..5710c438 100644 --- a/packages/cli-utils/src/index.ts +++ b/packages/cli-utils/src/index.ts @@ -1,4 +1,5 @@ import { Cli } from 'clipanion'; +import * as api from './api'; import { CheckCommand } from './commands/check/index'; import { DoctorCommand } from './commands/doctor/index'; @@ -8,7 +9,7 @@ import { GenerateSchema } from './commands/generate-schema/index'; import { InitCommand } from './commands/init/index'; import { TurboCommand } from './commands/turbo/index'; -function main() { +async function _main() { const cli = new Cli({ binaryVersion: process.env.npm_package_version || '0.0.0', binaryLabel: 'gql.tada CLI', @@ -23,7 +24,11 @@ function main() { cli.register(InitCommand); cli.register(TurboCommand); - cli.runExit(process.argv.slice(2)); + await cli.runExit(process.argv.slice(2)); } +type MainFn = typeof _main & typeof api; +const main = Object.assign(_main, api) as MainFn; + +export * from './api'; export default main; diff --git a/packages/cli-utils/src/term/tty.ts b/packages/cli-utils/src/term/tty.ts index 0c376cec..0698fce5 100644 --- a/packages/cli-utils/src/term/tty.ts +++ b/packages/cli-utils/src/term/tty.ts @@ -21,6 +21,10 @@ import { text, compose } from './write'; import { cmd, _setColor, CSI, Mode, PrivateMode } from './csi'; import { isGithubCI } from './github'; +export interface TTYParams { + disableTTY?: boolean; +} + export interface KeypressEvent { data?: string; sequence: string; @@ -41,7 +45,7 @@ export interface TTY { write(input: readonly string[], ...args: readonly string[]): void; write(...input: readonly string[]): void; - start(outputs: AsyncIterable): Promise; + start(outputs: AsyncIterable, disableInput?: boolean): Promise; mode(...modes: readonly (Mode | PrivateMode)[]): void; modeOff(...modes: readonly (Mode | PrivateMode)[]): void; @@ -78,8 +82,8 @@ function fromReadStream(stream: ReadStream): Source { }); } -export function initTTY(): TTY { - let isTTY = process.env.TERM !== 'dumb' && !process.env.CI; +export function initTTY(params: TTYParams = {}): TTY { + let isTTY = process.env.TERM !== 'dumb' && !process.env.CI && !params.disableTTY; let pipeTo: WriteStream | null = null; let output: WriteStream = process.stdout; if (isGithubCI) { @@ -133,7 +137,11 @@ export function initTTY(): TTY { function start(outputs: AsyncIterable): Promise { const write = (input: string | CLIError) => output.write('' + input); - return pipe(compose(outputs), onPush(write), takeUntil(cancelSource), toPromise); + if (params.disableTTY) { + return pipe(compose(outputs), onPush(write), toPromise); + } else { + return pipe(compose(outputs), onPush(write), takeUntil(cancelSource), toPromise); + } } function mode(...modes: readonly (Mode | PrivateMode)[]): void {