Skip to content

Commit 33b5a36

Browse files
authored
feat!: Refactor/Simplify cli (#268)
* feat!: Replace custom prompts with clark This is simplify the stack by removing the dependencies to react, ink and apollo. Breaking change: `--pr` without pull request number is not supported * chore: Replace rollup with tsup * docs: Apply eslint warnings * Fix imports
1 parent 6bea932 commit 33b5a36

33 files changed

+13089
-46074
lines changed

README.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
1. **Initialize the generator**
3434

3535
```bash
36-
$ npx @openapi-codegen/cli init
36+
npx @openapi-codegen/cli init
3737
```
3838

3939
<img style="max-width: 400px" src="https://user-images.githubusercontent.com/271912/194000679-5a4501b8-5fc0-430c-9217-028bf91a5dcd.gif">
@@ -43,7 +43,7 @@
4343
2. **Start Generation**
4444

4545
```bash
46-
$ npx openapi-codegen gen {namespace}
46+
npx openapi-codegen gen {namespace}
4747
```
4848
After the code generation is done, you will notice the following files:
4949

@@ -55,7 +55,7 @@
5555
&nbsp;
5656

5757
> **Warning**
58-
>
58+
>
5959
> If `{namespace}Fetcher.ts` or `{namespace}Context.tsx` already exist in the output folder, they will not be replaced. However, `{namespace}Components.tsx` and `{namespace}Schemas.ts` will be re-generated each time based on the Open API spec file provided.
6060
6161
3. **Configure the Fetcher** (optional)
@@ -71,9 +71,11 @@
7171
If during generator setup you picked `> React Query components`, then you will need to install and configure React Query in order for the generated React hooks to work properly:
7272

7373
- Install the library
74-
```bash
75-
npm i @tanstack/react-query
76-
```
74+
75+
```bash
76+
npm i @tanstack/react-query
77+
```
78+
7779
- Wire up the `QueryClient` as described [here](https://tanstack.com/query/v4/docs/adapters/react-query).
7880

7981
## Philosophy
@@ -161,17 +163,14 @@ And since this generated from the specs, everything is safe at build time!
161163
The only thing you need to manage is the configuration.
162164
Everything is typed and self-documented, but just in case, you can find here example configuration below:
163165
164-
165166
### Example Config
167+
166168
```ts
167169
// openapi-codegen.config.ts
168170
import { defineConfig } from "@openapi-codegen/cli";
169171
import {
170172
generateSchemaTypes,
171173
generateReactQueryComponents,
172-
/* generateExpressControllers, */
173-
/* generateRestfulReactComponents, */
174-
/* ... */
175174
} from "@openapi-codegen/typescript";
176175
177176
export default defineConfig({
@@ -207,54 +206,71 @@ export default defineConfig({
207206
```
208207
209208
### Plugins
209+
210210
the `@openapi-codegen/cli` supports these generator plugins:
211+
211212
#### **generateSchemaTypes** (frontend/backend)
213+
212214
generate all schema types for your specification:
215+
213216
```ts
214217
const { schemasFiles } = await generateSchemaTypes(context, {
215218
/* config */
216219
});
217220
```
221+
218222
output: `{namespace}Schemas.ts`
219223
220224
#### **generateFetchers** (frontend)
221-
generate all fetchers with types for your specification *needs schemafiles*
225+
226+
generate all fetchers with types for your specification _needs schemafiles_
227+
222228
```ts
223229
await generateFetchers(context, {
224230
/* config */
225231
schemasFiles,
226232
});
227233
```
234+
228235
output: `{namespace}Fetchers.ts`
229236
230237
#### **generateReactQueryComponents** (frontend)
238+
231239
generate all React Query Components for useQuery() and useMutation()
240+
232241
```ts
233242
await generateReactQueryComponents(context, {
234243
/* config*/
235244
schemasFiles,
236245
});
237246
```
247+
238248
output: `{namespace}Components.ts`
249+
239250
#### **generateReactQueryFunctions** (frontend)
251+
240252
generate all React Query Functions used for e.g. React-Router 6.6.0+ loader functions
253+
241254
```ts
242255
await generateReactQueryFunctions(context, {
243256
filenamePrefix,
244257
schemasFiles,
245258
});
246259
```
260+
247261
output: `{namespace}Functions.ts`
248262
249263
example usage in react-route-loader:
264+
250265
```ts
251266
export const routeLoader = (queryClient: QueryClient) =>
252267
async ({ params }: MyParams) =>
253268
await queryClient.fetchQuery(...getYourQueryNameQuery({}), {
254269
/*options*/
255270
})
256271
```
257-
*more infos: https://reactrouter.com/en/main/guides/data-libs*
272+
273+
_more infos: https://reactrouter.com/en/main/guides/data-libs_
258274
259275
260276
You can import any generator into the `to` section, those can be the ones provided by this project or your own custom ones. You have full control of what you are generating!

cli/README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Getting started
44

5-
1. Add a `openapi-codegen.config.ts` at the root of your project
5+
1. Add a `openapi-codegen.config.ts` at the root of your project or run `npx @openapi-codegen/cli init`
66

77
```ts
88
// openapi-codegen.config.ts
@@ -56,7 +56,21 @@ export default defineConfig({
5656
3. Run the generator (`example` is the config key defined in the step 1)
5757

5858
```bash
59-
$ yarn gen example
59+
$ npm run gen example
6060
```
6161

6262
4. Start playing! 🥳
63+
64+
## How to test the cli locally
65+
66+
1. Build the CLI (watch mode)
67+
68+
```bash
69+
npm run watch
70+
```
71+
72+
1. Start working working (new terminal)
73+
74+
```bash
75+
./lib/cli.js init --dry-run
76+
```

cli/codegen.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

cli/package.json

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,56 +24,43 @@
2424
],
2525
"license": "MIT",
2626
"scripts": {
27-
"build": "rollup -c",
28-
"prepublishOnly": "npm run build",
29-
"gen": "graphql-codegen --config codegen.yml -r dotenv/config"
27+
"watch": "tsup src/index.ts src/cli.ts --outDir lib --format esm --watch",
28+
"prebuild": "tsc --emitDeclarationOnly --declaration -p tsconfig.package.json",
29+
"build": "tsup src/index.ts src/cli.ts --outDir lib --format esm",
30+
"prepublishOnly": "npm run build"
3031
},
3132
"files": [
3233
"lib",
3334
"README.md"
3435
],
3536
"dependencies": {
36-
"@apollo/client": "^3.5.10",
37+
"@clack/prompts": "^0.9.1",
38+
"@octokit/request": "^9.2.0",
3739
"@swc/core": "^1.2.118",
3840
"case": "^1.6.3",
39-
"chalk": "^5.0.0",
4041
"cli-highlight": "^2.1.11",
41-
"clipanion": "^3.2.0-rc.10",
42+
"clipanion": "^4.0.0-rc.4",
4243
"fs-extra": "^10.0.0",
4344
"got": "^12.0.0",
4445
"got-fetch": "^5.1.1",
45-
"graphql": "^15.8.0",
46-
"ink": "^5.0.0",
4746
"js-yaml": "^4.1.0",
4847
"openapi3-ts": "^2.0.1",
4948
"prettier": "^3.2.5",
50-
"rxjs": "^7.5.4",
5149
"slash": "^4.0.0",
5250
"swagger2openapi": "^7.0.8",
5351
"tslib": "^2.3.1",
54-
"typanion": "^3.7.1",
52+
"typanion": "^3.14.0",
5553
"typescript": "4.8.2"
5654
},
5755
"devDependencies": {
58-
"@graphql-codegen/cli": "^2.6.2",
59-
"@graphql-codegen/typescript": "^2.4.5",
60-
"@graphql-codegen/typescript-operations": "^2.3.2",
61-
"@graphql-codegen/typescript-react-apollo": "^3.2.8",
62-
"@rollup/plugin-commonjs": "^22.0.0",
63-
"@rollup/plugin-json": "^4.1.0",
64-
"@rollup/plugin-node-resolve": "^13.3.0",
65-
"@rollup/plugin-typescript": "^8.3.2",
6656
"@swc-node/register": "^1.4.0",
6757
"@types/fs-extra": "^9.0.13",
6858
"@types/js-yaml": "^4.0.3",
6959
"@types/nock": "^11.1.0",
7060
"@types/prettier": "^3.0.0",
71-
"@types/react": "^18.3.2",
7261
"@types/slash": "^3.0.0",
7362
"nock": "^13.2.1",
7463
"react": "^18.3.1",
75-
"rollup-plugin-auto-external": "^2.0.0",
76-
"rollup-plugin-internal": "^1.0.4",
77-
"rollup-plugin-preserve-shebang": "^1.0.1"
64+
"tsup": "^8.3.5"
7865
}
7966
}

cli/rollup.config.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

cli/src/commands/GenerateCommand.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import slash from "slash";
1010
import { Config, FromOptions, Namespace } from "../types";
1111
import { getOpenAPISourceFile } from "../core/getOpenAPISourceFile.js";
1212
import { parseOpenAPISourceFile } from "../core/parseOpenAPISourceFile.js";
13-
import { Prompt } from "../prompts/Prompt.js";
13+
import { getGithubToken } from "../utils/getGithubToken";
14+
import { resolveGithubPullRequest } from "../utils/resolveGithubPullRequest";
1415

1516
const __filename = fileURLToPath(import.meta.url);
1617

@@ -69,7 +70,6 @@ export class GenerateCommand extends Command {
6970
description: "[source=github] Select a specific pull-request as ref",
7071
env: "OPENAPI_CODEGEN_GITHUB_PULL_REQUEST",
7172
validator: t.isNumber(),
72-
tolerateBoolean: true,
7373
});
7474

7575
static paths = [["gen"], ["generate"], Command.Default];
@@ -214,13 +214,11 @@ export class GenerateCommand extends Command {
214214
const config = configs[this.namespace];
215215
const options = this.getFromOptions(config);
216216
if (options.source === "github" && this.pullRequest) {
217-
const prompt = new Prompt();
218-
const token = await prompt.githubToken();
219-
const pullRequest = await prompt.githubPullRequest({
220-
...options,
217+
const token = await getGithubToken();
218+
const pullRequest = await resolveGithubPullRequest({
219+
pullRequestNumber: this.pullRequest,
220+
options,
221221
token,
222-
pullRequestNumber:
223-
typeof this.pullRequest === "number" ? this.pullRequest : undefined,
224222
});
225223

226224
options.ref = pullRequest.ref;

0 commit comments

Comments
 (0)