Skip to content

Commit

Permalink
fail earlier if dest dir does not exist, fix tools/gen.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
david-crespo committed May 6, 2024
1 parent 92c9369 commit 6991f43
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions oxide-openapi-gen-ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion oxide-openapi-gen-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oxide/openapi-gen-ts",
"version": "0.1.12",
"version": "0.1.13",
"description": "OpenAPI client generator used to generate Oxide TypeScript SDK",
"keywords": [
"oxide",
Expand Down
17 changes: 14 additions & 3 deletions oxide-openapi-gen-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { generateMSWHandlers } from "./client/msw-handlers";
import { generateTypeTests } from "./client/type-tests";
import { generateZodValidators } from "./client/zodValidators";
import { resolve } from "node:path";
import { existsSync } from "node:fs";
import parseArgs from "minimist";

function helpAndExit(msg?: string): never {
Expand Down Expand Up @@ -47,17 +48,27 @@ function parseFeatures(featuresArg: string | undefined) {
helpAndExit(`Unrecognized feature '${feature}'.`);
}
}

const validated = features as Feature[];

return {
zod: features.includes("zod"),
msw: features.includes("msw"),
typetests: features.includes("typetests"),
zod: validated.includes("zod"),
msw: validated.includes("msw"),
typetests: validated.includes("typetests"),
};
}

async function generate(specFile: string, destDir: string, features: Features) {
// destination directory is resolved relative to CWD
const destDirAbs = resolve(process.cwd(), destDir);

if (!existsSync(destDirAbs)) {
throw new Error(`Error: destination directory does not exist.
Argument given: ${destDirAbs}
Resolved path: ${destDirAbs}
`);
}

const rawSpec = await SwaggerParser.parse(specFile);
if (!("openapi" in rawSpec) || !rawSpec.openapi.startsWith("3.0")) {
throw new Error("Only OpenAPI 3.0 is currently supported");
Expand Down
4 changes: 2 additions & 2 deletions tools/gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ SPEC_FILE="./spec.json"
# TODO: we could get rid of this DL if a test didn't rely on it
curl --fail "$SPEC_URL" -o $SPEC_FILE

npx tsx "$ROOT_DIR/oxide-openapi-gen-ts/src/index.ts" $SPEC_FILE $DEST_DIR
npx prettier --write --log-level error "$DEST_DIR"
npx tsx "$ROOT_DIR/oxide-openapi-gen-ts/src/index.ts" $SPEC_FILE $DEST_DIR --features zod,msw,typetests
npx prettier@3.2.5 --write --log-level error "$DEST_DIR"

0 comments on commit 6991f43

Please sign in to comment.