fix error UX

Signed-off-by: aramissennyeydd <aramis.sennyey@doordash.com>
This commit is contained in:
aramissennyeydd
2024-10-15 21:16:39 -04:00
parent abc3e3475c
commit 879e4cb45a
5 changed files with 59 additions and 15 deletions
@@ -103,7 +103,10 @@ async function generate(
export async function command(
outputPackage: string,
clientAdditionalProperties?: string,
abortSignal?: AbortController,
{
abortSignal,
isWatch = false,
}: { abortSignal?: AbortController; isWatch?: boolean } = {},
): Promise<void> {
try {
await generate(outputPackage, clientAdditionalProperties, abortSignal);
@@ -115,7 +118,14 @@ export async function command(
console.debug('Server generation aborted.');
return;
}
console.log(chalk.red(`Client generation failed:`));
console.log(err);
if (isWatch) {
console.log(chalk.red(`Client generation failed:`));
console.group();
console.log(chalk.red(err.message));
console.groupEnd();
} else {
console.log(chalk.red(`Client generation failed.`));
console.log(chalk.red(err.message));
}
}
}
@@ -18,7 +18,10 @@ import { OptionValues } from 'commander';
import { command as generateClient } from './client';
import { command as generateServer } from './server';
import chokidar from 'chokidar';
import { getPathToCurrentOpenApiSpec } from '../../../../../lib/openapi/helpers';
import {
getPathToCurrentOpenApiSpec,
loadAndValidateOpenApiYaml,
} from '../../../../../lib/openapi/helpers';
import { debounce } from 'lodash';
import { block } from '../../../../../lib/runner';
@@ -31,18 +34,24 @@ export async function command(opts: OptionValues) {
}
const sharedCommand = async (abortSignal?: AbortController) => {
const resolvedOpenapiPath = await getPathToCurrentOpenApiSpec();
await loadAndValidateOpenApiYaml(resolvedOpenapiPath);
const promises = [];
const options = {
isWatch: opts.watch,
abortSignal,
};
if (opts.clientPackage) {
promises.push(
generateClient(
opts.clientPackage,
opts.clientAdditionalProperties,
abortSignal,
options,
),
);
}
if (opts.server) {
promises.push(generateServer(abortSignal));
promises.push(generateServer(options));
}
await Promise.all(promises);
};
@@ -75,6 +84,8 @@ export async function command(opts: OptionValues) {
'Watching for changes in OpenAPI spec. Press Ctrl+C to stop.',
);
});
debouncedCommand();
await block();
} catch (err) {
console.error(chalk.red('Error: ', err));
@@ -61,7 +61,13 @@ export const createOpenApiRouter = async (
}
}
export async function command(abortSignal?: AbortController): Promise<void> {
export async function command({
abortSignal,
isWatch = false,
}: {
abortSignal?: AbortController;
isWatch?: boolean;
}): Promise<void> {
try {
await generate(abortSignal);
console.log(chalk.green('Generated server files.'));
@@ -70,7 +76,14 @@ export async function command(abortSignal?: AbortController): Promise<void> {
console.debug('Server generation aborted.');
return;
}
console.log(chalk.red(`OpenAPI server stub generation failed.`));
console.log(err.message);
if (isWatch) {
console.log(chalk.red(`Server generation failed:`));
console.group();
console.log(chalk.red(err.message));
console.groupEnd();
} else {
console.log(chalk.red(err.message));
console.log(chalk.red(`OpenAPI server stub generation failed.`));
}
}
}
@@ -15,12 +15,10 @@
*/
import fs from 'fs-extra';
import YAML from 'js-yaml';
import { isEqual, cloneDeep } from 'lodash';
import { isEqual } from 'lodash';
import { join } from 'path';
import chalk from 'chalk';
import { relative as relativePath, resolve as resolvePath } from 'path';
import Parser from '@apidevtools/swagger-parser';
import { runner } from '../../../../lib/runner';
import { paths as cliPaths } from '../../../../lib/paths';
import {
@@ -28,7 +26,10 @@ import {
TS_SCHEMA_PATH,
YAML_SCHEMA_PATH,
} from '../../../../lib/openapi/constants';
import { getPathToOpenApiSpec } from '../../../../lib/openapi/helpers';
import {
getPathToOpenApiSpec,
loadAndValidateOpenApiYaml,
} from '../../../../lib/openapi/helpers';
async function verify(directoryPath: string) {
let openapiPath = '';
@@ -38,8 +39,7 @@ async function verify(directoryPath: string) {
// Unable to find spec at path.
return;
}
const yaml = YAML.load(await fs.readFile(openapiPath, 'utf8'));
await Parser.validate(cloneDeep(yaml) as any);
const yaml = await loadAndValidateOpenApiYaml(openapiPath);
const schemaPath = join(directoryPath, TS_SCHEMA_PATH);
if (!(await fs.pathExists(schemaPath))) {
@@ -18,6 +18,10 @@ import { pathExists } from 'fs-extra';
import { paths } from '../paths';
import { YAML_SCHEMA_PATH } from './constants';
import { resolve } from 'path';
import YAML from 'js-yaml';
import { cloneDeep } from 'lodash';
import Parser from '@apidevtools/swagger-parser';
import fs from 'fs-extra';
export const getPathToFile = async (directory: string, filename: string) => {
return resolve(directory, filename);
@@ -41,3 +45,9 @@ export const getPathToOpenApiSpec = async (directory: string) => {
export const getPathToCurrentOpenApiSpec = async () => {
return await assertExists(await getRelativePathToFile(YAML_SCHEMA_PATH));
};
export async function loadAndValidateOpenApiYaml(path: string) {
const yaml = YAML.load(await fs.readFile(path, 'utf8'));
await Parser.validate(cloneDeep(yaml) as any);
return yaml;
}