8000 Accept theme type as a positional argument in create-keycloak-theme by edewit · Pull Request #54228 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
8000
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
8000
Diff view
Diff view
23 changes: 18 additions & 5 deletions js/apps/create-keycloak-theme/README.md
8000
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,33 @@ Create a new Keycloak ui project based on a template
## Usage

```bash
npm create keycloak-theme <name> [options]
npm create keycloak-theme <name> [type]
```

### Arguments

* `<name>` name of the project (required)
* `[type]` the type of ui to be created: `account` or `admin` (prompted if omitted)

### Options

* `-t, --type <name>` the type of ui to be created either account or admin (currently only account is supported)
* `-t, --type <type>` same as the `[type]` argument

When using `npm create`, prefer the positional `[type]` form. npm treats `-t` as its own `--tag` flag, so pass `--type` after `--` if you use the option form. If neither is provided, the CLI asks which type to create.

### Example

```bash
npm create keycloak-theme my-project -t account
npm create keycloak-theme my-project admin
```

Or with the option form:

```bash
npm create keycloak-theme my-project -- --type admin
```

This will create a new project called `my-project` with an account ui based on the template from the quickstarts repo.
This will create a new project called `my-project` with an admin ui based on the template from the quickstarts repo.
After the project is created, the following commands can be used to start the server and open the ui in a browser:

```bash
Expand All @@ -35,4 +48,4 @@ Then open the ui in a browser:

```bash
open http://localhost:8080
```
```
108 changes: 85 additions & 23 deletions js/apps/create-keycloak-theme/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Mustache from "mustache";
import { mkdtemp } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve, dirname } from "node:path";
import { createInterface } from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";
import { simpleGit } from "simple-git";
import { fileURLToPath } from "url";

Expand All @@ -17,43 +19,104 @@ const packageJson = JSON.parse(
),
);

const TYPES = ["account", "admin"];

function parseType(value) {
if (!TYPES.includes(value)) {
throw new InvalidArgumentError("It should be either account or admin");
}
return value;
}

async function promptType() {
const rl = createInterface({ input, output });
const closed = new Promise((_, reject) => {
rl.once("close", () => {
reject(
new Error(
"Input closed before a theme type was selected. Pass the type as an argument, for example: npm create keycloak-theme my-theme admin",
),
);
});
});
try {
for (;;) {
const answer = (
await Promise.race([
rl.question(
`Select a type (${TYPES.map((t) => chalk.green(t)).join(" / ")}): `,
),
closed,
])
)
.trim()
.toLowerCase();

if (TYPES.includes(answer)) {
return answer;
}

console.log(
chalk.red(`Invalid type. Choose one of: ${TYPES.join(", ")}`),
);
}
} finally {
rl.close();
}
}

function main() {
new Command(packageJson.name)
.version(packageJson.version)
.description(packageJson.description)
.arguments("<name>")
.usage(`${chalk.green("<name>")} [options]`)
.argument("<name>", "name of the project")
// Optional positional type: `npm create` treats `-t` as its own `--tag`
// flag, so `npm create keycloak-theme my-theme -t admin` arrives here as
// `my-theme admin`.
.argument("[type]", "account or admin", parseType)
.usage(`${chalk.green("<name>")} ${chalk.green("[type]")} [options]`)
.option(
"-t, --type <name>",
"the type of ui to be created either `account` or `admin` ",
(value) => {
if (value !== "account" && value !== "admin") {
throw new InvalidArgumentError(
"It should be either account or admin",
);
}
return value;
},
"-t, --type <type>",
"the type of ui to be created either `account` or `admin`",
parseType,
)
.action(async (name, options) => {
const type = options.type || "account";
console.log(
`Creating a new ${chalk.green(name)} project of type ${chalk.green(type)}`,
);
.action(async (name, typeArg, options) => {
try {
const type = options.type || typeArg || (await promptType());
console.log(
`Creating a new ${chalk.green(name)} project of type ${chalk.green(type)}`,
);

await createProject(name, type);
done(name);
await createProject(name, type);
done(name);
} catch (error) {
console.error(chalk.red(error instanceof Error ? error.message : error));
process.exitCode = 1;
}
})
.on("--help", () => {
console.log();
console.log(` Only ${chalk.green("<name>")} is required.`);
console.log(
` If ${chalk.green("[type]")} / ${chalk.blue("--type")} is omitted, you will be prompted.`,
);
console.log();
console.log(` Type ${chalk.blue("--type")} can be one of:`);
console.log(
` Type can be passed as ${chalk.green("[type]")} or ${chalk.blue("--type")}:`,
);
console.log(
` - ${chalk.green("account")} for an account ui based ui`,
);
console.log(` - ${chalk.green("admin")} for an admin ui based ui`);
console.log();
console.log(" Examples:");
console.log(
` ${chalk.cyan("npm create keycloak-theme my-theme admin")}`,
);
console.log(
` ${chalk.cyan("npm create keycloak-theme my-theme -- --type admin")}`,
);
console.log();
})
.parse(process.argv);
}
Expand All @@ -67,9 +130,8 @@ function cloneQuickstart(type) {
"--single-branch": undefined,
"--branch": "main",
})
.then(() =>
resolve(join(dir, `extension/extend-${type}-console-node`)),
);
.then(() => resolve(join(dir, `extension/extend-${type}-console-node`)))
.catch(reject);
});
});
}
Expand Down
Loading
0