rework generate to take flags instead of subcommands
Signed-off-by: Aramis <sennyeyaramis@gmail.com>
This commit is contained in:
@@ -6,8 +6,8 @@
|
||||
|
||||
The following commands now live under the `package` namespace,
|
||||
|
||||
- `schema openapi generate` is now `package schema openapi generate server`
|
||||
- `schema openapi generate-client` is now `package schema openapi generate client`
|
||||
- `schema openapi generate` is now `package schema openapi generate --server`
|
||||
- `schema openapi generate-client` is now `package schema openapi generate --client-package`
|
||||
- `schema openapi init` is now `package schema openapi init`
|
||||
|
||||
And these commands live under the new `repo` namespace,
|
||||
@@ -16,4 +16,4 @@ And these commands live under the new `repo` namespace,
|
||||
- `schema openapi test` is now `repo schema openapi test`
|
||||
- `schema openapi verify` is now `repo schema openapi verify`
|
||||
|
||||
This also reworks the `package schema openapi generate client` to accept only an output directory as the input directory can now be inferred.
|
||||
The `package schema openapi generate` now supports defining both `--server` and `--client-package` to generate both at once.This update also reworks the `--client-package` flag to accept only an output directory as the input directory can now be inferred.
|
||||
|
||||
@@ -45,7 +45,7 @@ You should create a new folder, `src/schema` in your backend plugin to store you
|
||||
|
||||
## Generating a typed express router from a spec
|
||||
|
||||
Run `yarn backstage-repo-tools package schema openapi generate server <plugin-directory>`. This will create an `openapi.generated.ts` file in the `src/schema` directory that contains the OpenAPI schema as well as a generated express router with types. You should add this command to your `package.json` for future use.
|
||||
Run `yarn backstage-repo-tools package schema openapi generate --server` from the directory with your plugin. This will create an `openapi.generated.ts` file in the `src/schema` directory that contains the OpenAPI schema as well as a generated express router with types. You should add this command to your `package.json` for future use and you can combine both the server generation and the client generation below like so, `yarn backstage-repo-tools package schema openapi generate --server --client-package <clientPackageDirectory>`
|
||||
|
||||
Use it like so, update your `router.ts` or `createRouter.ts` file with the following content,
|
||||
|
||||
@@ -63,7 +63,7 @@ export async function createRouter(
|
||||
|
||||
## Generating a typed client from a spec
|
||||
|
||||
From your current backend plugin directory, run `yarn backstage-repo-tools package schema openapi generate client --output-package <plugin-client-directory>`. `<plugin-client-directory>` is a new directory and npm package that you should create. The general pattern is `plugins/<plugin-name>-client` or if you want to co-locate this with your other shared types, use `plugins/<plugin-name>-common`. You should add this command to your `package.json` for future use.
|
||||
From your current backend plugin directory, run `yarn backstage-repo-tools package schema openapi generate --client-package <plugin-client-directory>`. `<plugin-client-directory>` is a new directory and npm package that you should create. The general pattern is `plugins/<plugin-name>-client` or if you want to co-locate this with your other shared types, use `plugins/<plugin-name>-common`. You should add this command to your `package.json` for future use.
|
||||
|
||||
The generated client will have a directory `src/generated` that exports a `DefaultApiClient` class and all generated types. You can use the client like so,
|
||||
|
||||
|
||||
@@ -85,40 +85,17 @@ Options:
|
||||
|
||||
Commands:
|
||||
init
|
||||
generate [command]
|
||||
generate [options]
|
||||
help [command]
|
||||
```
|
||||
|
||||
### `backstage-repo-tools package schema openapi generate`
|
||||
|
||||
```
|
||||
Usage: backstage-repo-tools package schema openapi generate [options] [command] [command]
|
||||
|
||||
Options:
|
||||
-h, --help
|
||||
|
||||
Commands:
|
||||
server
|
||||
client [options]
|
||||
help [command]
|
||||
```
|
||||
|
||||
### `backstage-repo-tools package schema openapi generate client`
|
||||
|
||||
```
|
||||
Usage: backstage-repo-tools package schema openapi generate client [options]
|
||||
|
||||
Options:
|
||||
--output-package <pathToPackage>
|
||||
-h, --help
|
||||
```
|
||||
|
||||
### `backstage-repo-tools package schema openapi generate server`
|
||||
|
||||
```
|
||||
Usage: backstage-repo-tools package schema openapi generate server [options]
|
||||
Usage: backstage-repo-tools package schema openapi generate [options]
|
||||
|
||||
Options:
|
||||
--client-package [package]
|
||||
-h, --help
|
||||
```
|
||||
|
||||
|
||||
@@ -44,35 +44,19 @@ function registerPackageCommand(program: Command) {
|
||||
),
|
||||
);
|
||||
|
||||
const generateCommand = openApiCommand
|
||||
.command('generate [command]')
|
||||
.description(
|
||||
'Commands for generating various things from an OpenAPI spec.',
|
||||
);
|
||||
|
||||
generateCommand
|
||||
.command('server')
|
||||
.description(
|
||||
'Generates an express server stub using the OpenAPI schema for typings.',
|
||||
)
|
||||
.action(
|
||||
lazy(() =>
|
||||
import('./package/schema/openapi/generate/server').then(m => m.command),
|
||||
),
|
||||
);
|
||||
|
||||
generateCommand
|
||||
.command('client')
|
||||
.description(
|
||||
'Generates a client that can interact with your backend plugin using types from your OpenAPI schema.',
|
||||
)
|
||||
.requiredOption(
|
||||
'--output-package <pathToPackage>',
|
||||
openApiCommand
|
||||
.command('generate')
|
||||
.option(
|
||||
'--client-package [package]',
|
||||
'Top-level path to where the client should be generated, ie packages/catalog-client.',
|
||||
)
|
||||
.option('--server')
|
||||
.description(
|
||||
'Command to generate a client and/or a server stub from an OpenAPI spec.',
|
||||
)
|
||||
.action(
|
||||
lazy(() =>
|
||||
import('./package/schema/openapi/generate/client').then(m => m.command),
|
||||
import('./package/schema/openapi/generate').then(m => m.command),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -87,11 +87,7 @@ async function generate(outputDirectory: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function command({
|
||||
outputPackage,
|
||||
}: {
|
||||
outputPackage: string;
|
||||
}): Promise<void> {
|
||||
export async function command(outputPackage: string): Promise<void> {
|
||||
try {
|
||||
await generate(outputPackage);
|
||||
console.log(
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2024 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import chalk from 'chalk';
|
||||
import { OptionValues } from 'commander';
|
||||
import { command as generateClient } from './client';
|
||||
import { command as generateServer } from './server';
|
||||
|
||||
export async function command(opts: OptionValues) {
|
||||
if (!opts.clientPackage && !opts.server) {
|
||||
console.log(
|
||||
chalk.red('Either --client-package or --server must be defined.'),
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
if (opts.clientPackage) {
|
||||
await generateClient(opts.clientPackage);
|
||||
}
|
||||
if (opts.server) {
|
||||
await generateServer();
|
||||
}
|
||||
}
|
||||
@@ -43,8 +43,7 @@
|
||||
"prepack": "backstage-cli package prepack",
|
||||
"postpack": "backstage-cli package postpack",
|
||||
"clean": "backstage-cli package clean",
|
||||
"generate-server": "backstage-repo-tools package schema openapi generate server",
|
||||
"generate-client": "backstage-repo-tools package schema openapi generate client --output-package packages/catalog-client"
|
||||
"generate": "backstage-repo-tools package schema openapi generate --server --client-package packages/catalog-client"
|
||||
},
|
||||
"dependencies": {
|
||||
"@backstage/backend-common": "workspace:^",
|
||||
|
||||
@@ -40,7 +40,7 @@
|
||||
"prepack": "backstage-cli package prepack",
|
||||
"postpack": "backstage-cli package postpack",
|
||||
"clean": "backstage-cli package clean",
|
||||
"generate-server": "backstage-repo-tools package schema openapi generate server"
|
||||
"generate": "backstage-repo-tools package schema openapi generate --server"
|
||||
},
|
||||
"dependencies": {
|
||||
"@backstage/backend-common": "workspace:^",
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
"postpack": "backstage-cli package postpack",
|
||||
"clean": "backstage-cli package clean",
|
||||
"start": "backstage-cli package start",
|
||||
"generate-server": "backstage-repo-tools package schema openapi generate server"
|
||||
"generate": "backstage-repo-tools package schema openapi generate --server"
|
||||
},
|
||||
"dependencies": {
|
||||
"@backstage/backend-common": "workspace:^",
|
||||
|
||||
Reference in New Issue
Block a user