Skip to content

Commit

Permalink
feat(cli-utils): Expose generate commands as exported functions (Re-a…
Browse files Browse the repository at this point in the history
…dd) (#207)
  • Loading branch information
kitten authored Apr 15, 2024
1 parent aa7ee38 commit c9c3d0f
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-seas-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gql.tada/cli-utils": minor
---

Expose internal generate commands as functions exported by `@gql.tada/cli-utils` (restoring prior functionality)
4 changes: 4 additions & 0 deletions packages/cli-utils/src/api.ts
Original file line number Diff line number Diff line change
@@ -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';
9 changes: 9 additions & 0 deletions packages/cli-utils/src/commands/generate-output/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command, Option } from 'clipanion';

import type { Options } from './runner';
import { initTTY } from '../../term';
import { run } from './runner';

Expand Down Expand Up @@ -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<void> {
const tty = initTTY({ disableTTY: true });
const result = await tty.start(run(tty, opts));
if (result instanceof Error) {
throw result;
}
}
2 changes: 1 addition & 1 deletion packages/cli-utils/src/commands/generate-output/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions packages/cli-utils/src/commands/generate-persisted/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command, Option } from 'clipanion';

import type { Options } from './runner';
import { initTTY } from '../../term';
import { run } from './runner';

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions packages/cli-utils/src/commands/generate-schema/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion packages/cli-utils/src/commands/generate-schema/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> | undefined;
output: string | undefined;
Expand Down
9 changes: 9 additions & 0 deletions packages/cli-utils/src/commands/turbo/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command, Option } from 'clipanion';

import type { Options } from './runner';
import { initTTY } from '../../term';
import { run } from './runner';

Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion packages/cli-utils/src/commands/turbo/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions packages/cli-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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',
Expand All @@ -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;
16 changes: 12 additions & 4 deletions packages/cli-utils/src/term/tty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,7 +45,7 @@ export interface TTY {
write(input: readonly string[], ...args: readonly string[]): void;
write(...input: readonly string[]): void;

start(outputs: AsyncIterable<ComposeInput>): Promise<string | CLIError>;
start(outputs: AsyncIterable<ComposeInput>, disableInput?: boolean): Promise<string | CLIError>;

mode(...modes: readonly (Mode | PrivateMode)[]): void;
modeOff(...modes: readonly (Mode | PrivateMode)[]): void;
Expand Down Expand Up @@ -78,8 +82,8 @@ function fromReadStream(stream: ReadStream): Source<KeypressEvent> {
});
}

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) {
Expand Down Expand Up @@ -133,7 +137,11 @@ export function initTTY(): TTY {

function start(outputs: AsyncIterable<ComposeInput>): Promise<string | CLIError> {
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 {
Expand Down

0 comments on commit c9c3d0f

Please sign in to comment.