Skip to content

refactor(cli-utils): Refactor CLI entrypoint and remove exports #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 13, 2024
Merged
5 changes: 5 additions & 0 deletions .changeset/funny-boxes-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gql.tada/cli-utils": major
---

Add stdout-piping support in some commands and adjust command arguments for consistency.
86 changes: 23 additions & 63 deletions packages/cli-utils/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,13 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

## sade
## clipanion

The MIT License (MIT)

Copyright (c) Luke Edwards <luke.edwards05@gmail.com> (https://lukeed.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
MIT, See license at: https://github.com/arcanis/clipanion

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
## typanion

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
MIT, See license at: https://github.com/arcanis/typanion

## json5

Expand Down Expand Up @@ -132,30 +116,6 @@ The above copyright notice and this permission notice shall be included in all c

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

## mri

The MIT License (MIT)

Copyright (c) Luke Edwards <luke.edwards05@gmail.com> (lukeed.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

## wonka

MIT License
Expand Down Expand Up @@ -206,7 +166,7 @@ THE SOFTWARE.

## @clack/core

MIT, Copyright (c) 0no.co <hi@0no.co>
MIT, Copyright (c) Nate Moore

## picocolors

Expand Down Expand Up @@ -298,24 +258,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

## which

The ISC License

Copyright (c) Isaac Z. Schlueter and Contributors

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

## shebang-command

The MIT License (MIT)
Expand All @@ -340,6 +282,24 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

## which

The ISC License

Copyright (c) Isaac Z. Schlueter and Contributors

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

## isexe

The ISC License
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@
"devDependencies": {
"@clack/prompts": "^0.7.0",
"@types/node": "^20.11.0",
"clipanion": "4.0.0-rc.3",
"execa": "^8.0.1",
"json5": "^2.2.3",
"rollup": "^4.9.4",
"sade": "^1.8.1",
"semiver": "^1.1.0",
"typanion": "^3.14.0",
"type-fest": "^4.10.2",
"typescript": "^5.3.3",
"wonka": "^6.3.4"
Expand Down
24 changes: 24 additions & 0 deletions packages/cli-utils/src/commands/check/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as t from 'typanion';
import { Command, Option } from 'clipanion';
import { run } from './runner';

export class CheckCommand extends Command {
static paths = [['check']];

failOnWarn = Option.Boolean('--fail-on-warn,-w', false, {
description: 'Triggers an error and a non-zero exit code if any warnings have been reported',
});

minSeverity =
Option.String('--level,-l', {
description: 'The minimum severity of diagnostics to display (info, warn, error)',
validator: t.isOneOf([t.isLiteral('info'), t.isLiteral('warn'), t.isLiteral('error')]),
}) || 'error';

async execute() {
await run({
failOnWarn: this.failOnWarn,
minSeverity: this.minSeverity,
});
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Project, ts } from 'ts-morph';
import { init, getGraphQLDiagnostics } from '@0no-co/graphqlsp/api';
import path from 'path';
import { getTsConfig } from '../tsconfig';
import type { GraphQLSPConfig } from '../lsp';
import { getGraphQLSPConfig } from '../lsp';
import { load, resolveTypeScriptRootDir } from '@gql.tada/internal';
import { createPluginInfo } from '../ts/project';
import path from 'node:path';

import type { GraphQLSPConfig } from '../../lsp';
import { getGraphQLSPConfig } from '../../lsp';
import { getTsConfig } from '../../tsconfig';
import { createPluginInfo } from '../../ts/project';

type Severity = 'error' | 'warn' | 'info';
const severities: Severity[] = ['error', 'warn', 'info'];

export interface FormattedDisplayableDiagnostic {
severity: Severity;
message: string;
Expand All @@ -17,18 +19,12 @@ export interface FormattedDisplayableDiagnostic {
file: string | undefined;
}

export type CheckOptions = {
/**
* Exit with a non-zero code if there are any warnings.
*/
exitOnWarn: boolean;
/**
* The minimum severity to report for.
*/
export interface Options {
failOnWarn: boolean | undefined;
minSeverity: Severity;
};
}

export async function check(opts: CheckOptions) {
export async function run(opts: Options) {
const tsConfig = await getTsConfig();
if (!tsConfig) {
return;
Expand Down Expand Up @@ -75,7 +71,7 @@ export async function check(opts: CheckOptions) {
: ``;
// eslint-disable-next-line no-console
console.log(`${errorReport}${warningsReport}${suggestionsReport}`);
if (errorDiagnostics.length || (opts.exitOnWarn && warnDiagnostics.length)) {
if (errorDiagnostics.length || (opts.failOnWarn && warnDiagnostics.length)) {
process.exit(1);
} else {
process.exit(0);
Expand Down
12 changes: 12 additions & 0 deletions packages/cli-utils/src/commands/doctor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Command } from 'clipanion';
import { initTTY } from '../../term';
import { run } from './runner';

export class DoctorCommand extends Command {
static paths = [['doctor']];

async execute() {
const result = await initTTY().start(run());
return typeof result === 'object' ? result.exit : 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pipe, interval, map } from 'wonka';

import * as t from '../term';
import * as t from '../../term';

export function code(text: string) {
return t.text`${t.cmd(t.CSI.Style, t.Style.Underline)}${text}${t.cmd(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ import type { TsConfigJson } from 'type-fest';
import { resolveTypeScriptRootDir } from '@gql.tada/internal';
import { existsSync } from 'node:fs';

import { initTTY } from '../term';
import * as logger from '../loggers/check';
import type { ComposeInput } from '../../term';
import * as logger from './logger';

// NOTE: Currently, most tasks in this command complete too quickly
// We slow them down to make the CLI output easier to follow along to
const delay = (ms = 700) =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});
const delay = (ms = 700) => {
if (process.env.CI) {
return Promise.resolve();
} else {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
};

const enum Messages {
TITLE = 'Doctor',
Expand All @@ -31,11 +36,7 @@ const MINIMUM_VERSIONS = {
lsp: '1.0.0',
};

export async function executeTadaDoctor() {
await initTTY().start(run());
}

async function* run() {
export async function* run(): AsyncIterable<ComposeInput> {
yield logger.title(Messages.TITLE, Messages.DESCRIPTION);
yield logger.runningTask(Messages.CHECK_TS_VERSION);
await delay();
Expand Down
22 changes: 0 additions & 22 deletions packages/cli-utils/src/commands/generate-output.ts

This file was deleted.

30 changes: 30 additions & 0 deletions packages/cli-utils/src/commands/generate-output/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Command, Option } from 'clipanion';

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

export class GenerateOutputCommand extends Command {
static paths = [['generate-output'], ['generate', 'output']];

disablePreprocessing = Option.Boolean('--disable-preprocessing', false, {
description:
'Disables pre-processing, which is an internal introspection format generated ahead of time',
});

tsconfig = Option.String('--tsconfig,-c', {
description: 'Specify the `tsconfig.json` used to read, unless `--output` is passed.',
});

output = Option.String('--output,-o', {
description:
'Specifies where to output the file to.\tDefault: The `tadaOutputLocation` configuration option',
});

async execute() {
await run(initTTY(), {
disablePreprocessing: this.disablePreprocessing,
output: this.output,
tsconfig: this.tsconfig,
});
}
}
Loading
Loading