Skip to content

Commit

Permalink
fix(cli-utils): Add missing --force-ts-format option to `generate-o…
Browse files Browse the repository at this point in the history
…utput`
  • Loading branch information
kitten committed Apr 17, 2024
1 parent e288b00 commit dd7f989
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-brooms-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gql.tada/cli-utils": patch
---

Add missing argument to `generate-output` command to force output the `.ts` format instead.
6 changes: 6 additions & 0 deletions packages/cli-utils/src/commands/generate-output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { run } from './runner';
export class GenerateOutputCommand extends Command {
static paths = [['generate-output'], ['generate', 'output']];

forceTSFormat = Option.Boolean('--force-ts-format', false, {
description: 'Forces the `.ts` output format when the output is piped',
hidden: true,
});

disablePreprocessing = Option.Boolean('--disable-preprocessing', false, {
description:
'Disables pre-processing, which is an internal introspection format generated ahead of time',
Expand All @@ -25,6 +30,7 @@ export class GenerateOutputCommand extends Command {
const tty = initTTY();
const result = await tty.start(
run(tty, {
forceTSFormat: this.forceTSFormat,
disablePreprocessing: this.disablePreprocessing,
output: this.output,
tsconfig: this.tsconfig,
Expand Down
12 changes: 10 additions & 2 deletions packages/cli-utils/src/commands/generate-output/logger.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import * as t from '../../term';

import { hint, code } from '../shared/logger';
export * from '../shared/logger';

export function summary() {
return t.text([
export function summary(showHint?: boolean) {
let out = t.text([
t.cmd(t.CSI.Style, t.Style.BrightGreen),
`${t.Icons.Tick} Introspection output was generated successfully\n`,
]);
if (showHint) {
out += hint(
`The pipe output was generated in the ${code('.d.ts')} format.\n` +
`For the ${code('.ts')} format, pass the ${code('--force-ts-format')} argument.\n`
);
}
return out;
}
28 changes: 17 additions & 11 deletions packages/cli-utils/src/commands/generate-output/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { writeOutput } from '../shared';
import * as logger from './logger';

export interface Options {
forceTSFormat?: boolean;
disablePreprocessing: boolean;
output: string | undefined;
tsconfig: string | undefined;
Expand Down Expand Up @@ -45,16 +46,6 @@ export async function* run(tty: TTY, opts: Options): AsyncIterable<ComposeInput>
throw logger.externalError('Failed to load introspection.', error);
}

let contents: string;
try {
contents = outputIntrospectionFile(minifyIntrospection(introspection), {
fileType: opts.output || pluginConfig.tadaOutputLocation || '.d.ts',
shouldPreprocess: !opts.disablePreprocessing,
});
} catch (error) {
throw logger.externalError('Could not generate introspection output', error);
}

let destination: WriteTarget;
if (!opts.output && tty.pipeTo) {
destination = tty.pipeTo;
Expand All @@ -76,11 +67,26 @@ export async function* run(tty: TTY, opts: Options): AsyncIterable<ComposeInput>
);
}

let contents: string;
try {
contents = outputIntrospectionFile(minifyIntrospection(introspection), {
fileType:
destination && typeof destination === 'string'
? destination
: opts.forceTSFormat
? '.ts'
: '.d.ts',
shouldPreprocess: !opts.disablePreprocessing,
});
} catch (error) {
throw logger.externalError('Could not generate introspection output', error);
}

try {
await writeOutput(destination, contents);
} catch (error) {
throw logger.externalError('Something went wrong while writing the introspection file', error);
}

yield logger.summary();
yield logger.summary(!opts.forceTSFormat && typeof destination !== 'string');
}
6 changes: 5 additions & 1 deletion packages/cli-utils/src/commands/shared/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import * as t from '../../term';

export function indent(text: string, indent: string) {
if (text.includes('\n')) {
return text.split('\n').join(t.text([t.Chars.Newline, indent]));
const out = text
.trim()
.split('\n')
.join(t.text([t.Chars.Newline, indent]));
return text.endsWith('\n') ? out + '\n' : out;
} else {
return text;
}
Expand Down

0 comments on commit dd7f989

Please sign in to comment.