Skip to content

Commit

Permalink
feat: Add spinners for better UX (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen authored Sep 22, 2024
1 parent da50569 commit 5055896
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
5 changes: 3 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "@denosaurs/typefetch",
"version": "0.0.21",
"version": "0.0.22",
"exports": {
".": "./main.ts"
},
"imports": {
"@denosaurs/wait": "jsr:@denosaurs/wait@^0.2",
"@std/cli": "jsr:@std/cli@^1",
"@std/http": "jsr:@std/http@^1",
"@std/path": "jsr:@std/path@^1",
"@std/yaml": "jsr:@std/yaml@^1",
"@std/http": "jsr:@std/http@^1",
"openapi-types": "npm:[email protected]",
"ts-morph": "npm:[email protected]"
},
Expand Down
12 changes: 9 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parseArgs } from "@std/cli";
import * as yaml from "@std/yaml";
import { wait } from "@denosaurs/wait";

import { ModuleDeclarationKind, Project } from "ts-morph";

Expand Down Expand Up @@ -77,24 +78,28 @@ if (import.meta.main) {
Deno.exit(1);
}

const importSpinner = wait("Resolving schema").start();
const input = args._[0] as string;
const output = resolve(args.output);

let openapi;
try {
importSpinner.text = "Trying to import OpenAPI schema as JSON";
openapi = (await import(input, { with: { type: "json" } })).default;
} catch (error) {
importSpinner.text = "Trying to import OpenAPI schema as YAML";
try {
// Try to import the OpenAPI schema as YAML
openapi = yaml.parse(await (await fetch(input)).text());
} catch {
console.error(`Failed to load OpenAPI schema from ${input}`);
importSpinner.fail(`Failed to load OpenAPI schema from ${input}`);
console.group();
console.error(error);
console.groupEnd();
Deno.exit(1);
}
}
importSpinner.succeed("Schema resolved");

const options = {
baseUrls: args["base-urls"]?.split(","),
Expand Down Expand Up @@ -148,7 +153,8 @@ if (import.meta.main) {
addPathsObject(global, openapi, openapi.paths, options);
addComponents(source, openapi, openapi.components);

source.formatText({ indentSize: 2 });

const saveSpinner = wait("Saving generated definitions").start();
source.formatText({ indentSize: 2, ensureNewLineAtEndOfFile: true });
await source.save();
saveSpinner.succeed("Definitions saved");
}
14 changes: 14 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { OpenAPI } from "./types/openapi.ts";

import { empty, isOk, notEmpty, resolveRef } from "./utils/mod.ts";
import { pascalCase } from "./utils/case/pascal_case.ts";
import { wait } from "@denosaurs/wait";

export const statusCodes = Object.values(STATUS_CODE) as number[];

Expand Down Expand Up @@ -234,9 +235,15 @@ export function addPathsObject(
paths: OpenAPI.PathsObject,
options: Options,
) {
const spinner = wait("Adding OpenAPI paths").start();

for (const [pattern, item] of Object.entries(paths)) {
spinner.text = `Generating ${pattern}...`;
spinner.render();
addPathItemObject(global, document, pattern, item, options);
}

spinner.succeed("OpenAPI paths added");
}

export function addParameterObjects(
Expand Down Expand Up @@ -678,11 +685,16 @@ export function addComponents(
document: OpenAPI.Document,
components: OpenAPI.ComponentsObject,
) {
const spinner = wait("Adding OpenAPI components").start();

if (notEmpty(components.schemas)) {
source.addTypeAliases(
Object.entries<OpenAPI.SchemaObject | OpenAPI.ReferenceObject>(
components.schemas,
).map(([name, schema]) => {
spinner.text = `Adding ${name}...`;
spinner.render();

const doc: Pick<JSDocStructure, "description" | "tags"> = {};

if ("deprecated" in schema && schema.deprecated === true) {
Expand Down Expand Up @@ -739,4 +751,6 @@ export function addComponents(
}),
);
}

spinner.succeed("OpenAPI components added");
}

0 comments on commit 5055896

Please sign in to comment.