Skip to content

Commit

Permalink
feat(nx-quarkus): add dynamic prompt to fetch quarkus extensions list
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Nx `v15.8.x` is now the minimum required version to run the plugin

We now leverage Nx's new `NX_INTERACTIVE` environment variable to check whether we are running in interactive mode (normal cli) or not.
When true, we automatically fetch `Quarkus` extensions and present them in an **autocomplete** prompt with **multi-select** support,
so you can easily select which ones you want to include in your project.
  • Loading branch information
tinesoft committed Mar 15, 2023
1 parent 6d9f3e4 commit e937649
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/nx-quarkus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ Every Nx plugin relies on the underlying Nx Workspace/DevKit it runs on. This ta

| Plugin Version | Nx Workspace version
| -------------- | --------------------
| `>=v5.x.x` | `>=v15.8.x`
| `>=v4.x.x` | `>=v15.x.x`
| `>=v2.1.x` | `>=v13.8.x`
| `>=v2.x.x` | `>=v12.6.x`
Expand Down
4 changes: 2 additions & 2 deletions packages/nx-quarkus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"typings": "./src/index.d.ts",
"peerDependencies": {
"@nrwl/workspace": ">=15.0.0",
"@nrwl/devkit": ">=15.0.0"
"@nrwl/workspace": ">=15.8.0",
"@nrwl/devkit": ">=15.8.0"
}
}
19 changes: 19 additions & 0 deletions packages/nx-quarkus/recipes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Recipes

Some helpful recipes to help you best use the plugin.

## Adding Quarkus extensions

When running the `@nxrocks/nx-quarkus:project` generator from a non-interactive mode (like from `Nx Console`), we cannot automatically fetch and present to you, the list
of `Quarkus` extensions (as we do in interactive, CLI mode). This is due to a limitation in Nx API, which does not support (yet?), such asynchronous prompts.

You will need to fetch and enter the extensions ids manually:

1. Go to [https://code.quarkus.io/api/extensions](https://code.quarkus.io/api/extensions)
![Extract of Quarkus extensions](images/quarkus-extensions-list.png)

> * Each `id` or `shortName`(when not empty) is the "id" of a Quarkus extension
> * For example, `resteasy-reactive` for `io.quarkus:quarkus-resteasy-reactive`, `io.quarkus:quarkus-rest-client-reactive-jsonb` for `io.quarkus:quarkus-rest-client-reactive-jsonb`, etc.
2. In Nx Console UI, enter the extensions ids you want to use, separated by a comma
![Adding extensions in Nx Console](images/nx-console-add-extensions.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion packages/nx-quarkus/src/generators/project/generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Tree, addProjectConfiguration, } from '@nrwl/devkit';
import { ProjectGeneratorOptions } from './schema';
import { normalizeOptions, generateQuarkusProject, addMavenPublishPlugin, addFormattingWithSpotless } from './lib';
import { normalizeOptions, generateQuarkusProject, addMavenPublishPlugin, addFormattingWithSpotless, promptQuarkusExtensions } from './lib';
import { addPluginToNxJson, BuilderCommandAliasType, NX_QUARKUS_PKG, } from '@nxrocks/common';


Expand Down Expand Up @@ -33,6 +33,8 @@ export async function projectGenerator(tree: Tree, options: ProjectGeneratorOpti
tags: normalizedOptions.parsedTags,
});

await promptQuarkusExtensions(normalizedOptions);

await generateQuarkusProject(tree, normalizedOptions);

addMavenPublishPlugin(tree, normalizedOptions);
Expand Down
3 changes: 2 additions & 1 deletion packages/nx-quarkus/src/generators/project/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { generateQuarkusProject } from './generate-quarkus-project';
export { addFormattingWithSpotless } from './add-formatting-with-spotless';
export { addMavenPublishPlugin } from './add-maven-publish-plugin';
export { normalizeOptions } from './normalize-options';
export { normalizeOptions } from './normalize-options';
export { promptQuarkusExtensions } from './prompt-quarkus-extensions';
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { NormalizedSchema } from "../schema";
import { prompt } from 'enquirer';
import { fetchQuarkusExtensions } from "../../../utils/quarkus-utils";
import { logger } from "@nrwl/devkit";


export async function promptQuarkusExtensions(options: NormalizedSchema) {

if (options.extensions === undefined && process.env.NX_INTERACTIVE === 'true') {

logger.info(`⏳ Fetching Quarkus extensions list. Please wait...`);

const extensions = (await fetchQuarkusExtensions(options)).map(e => e.id);

options.projectExtensions = await prompt({
name: 'extensions',
message: 'What extensions would you like to use? (type something to filter, press [space] to multi-select)',
type: 'autocomplete',
choices: extensions,
multiple: true,
}).then((a) => a['extensions']);
}
}
4 changes: 1 addition & 3 deletions packages/nx-quarkus/src/generators/project/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@
"x-prompt": "Skip code formatting support(using Spotless plugin)?"
},
"extensions": {
"description": "Extensions to use in the project(comma-separated). Possible ids can be found at https://code.quarkus.io/api/extensions",
"description": "Extensions to use in the project(comma-separated). Follow this guide https://t.ly/S6sk for more instructions on how to proceed.",
"type": "string",
"default": " ",
"x-prompt": "What extensions would you like to use (comma separated)?\n👉 Go to https://code.quarkus.io/api/extensions to get the ids needed here",
"x-priority": "important"
},
"quarkusInitializerUrl": {
Expand Down
18 changes: 17 additions & 1 deletion packages/nx-quarkus/src/utils/quarkus-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { NormalizedSchema } from '../generators/project/schema';
import { isMavenProject, checkProjectBuildFileContains, isGradleProject, BuilderCommandAliasType, hasGradleProject, hasMavenProject, runBuilderCommand } from '@nxrocks/common';
import { isMavenProject, checkProjectBuildFileContains, isGradleProject, BuilderCommandAliasType, hasGradleProject, hasMavenProject, runBuilderCommand, getCommonHttpHeaders, NX_QUARKUS_PKG } from '@nxrocks/common';

import { MAVEN_BUILDER, GRADLE_BUILDER } from '../core/constants';
import { ProjectConfiguration } from '@nrwl/devkit';
import fetch from 'node-fetch';

const getBuilder = (cwd: string) => {
if (hasMavenProject(cwd)) return MAVEN_BUILDER;
Expand All @@ -15,6 +16,15 @@ const getBuilder = (cwd: string) => {

export const DEFAULT_QUARKUS_INITIALIZR_URL = 'https://code.quarkus.io';

export interface QuarkusExtension {
"id": string,
"shortName": string,
"version":string,
"name": string,
"description": string,
"category": string
}

export function runQuarkusPluginCommand(
commandAlias: BuilderCommandAliasType,
params: string[],
Expand Down Expand Up @@ -54,3 +64,9 @@ export function isQuarkusProject(project: ProjectConfiguration): boolean {
return false;
}

export async function fetchQuarkusExtensions(options: NormalizedSchema): Promise<QuarkusExtension[]> {
const response = await fetch(`${options.quarkusInitializerUrl}/api/extensions`, getCommonHttpHeaders(NX_QUARKUS_PKG, options.proxyUrl));

return (await response.json()) ?? [] ;

}

0 comments on commit e937649

Please sign in to comment.