Skip to content

Commit

Permalink
fix: cli create new capability dynamic choices (#9861)
Browse files Browse the repository at this point in the history
* fix: cli create new capability dynamic choices

* fix: adjust options

* test: ut
  • Loading branch information
jayzhang authored Sep 6, 2023
1 parent 4b45d6c commit 1744565
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 36 deletions.
31 changes: 14 additions & 17 deletions packages/cli/src/commands/models/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,52 @@ import {
CLICommand,
CLICommandOption,
CLIContext,
CLIStringOption,
Platform,
err,
ok,
} from "@microsoft/teamsfx-api";
import {
CapabilityOptions,
CliQuestionName,
CreateProjectInputs,
CreateProjectOptions,
QuestionNames,
isCopilotPluginEnabled,
CliQuestionName,
copilotPluginOptionIds,
} from "@microsoft/teamsfx-core";
import chalk from "chalk";
import { assign } from "lodash";
import * as path from "path";
import * as uuid from "uuid";
import { getFxCore } from "../../activate";
import { logger } from "../../commonlib/logger";
import { TelemetryEvent, TelemetryProperty } from "../../telemetry/cliTelemetryEvents";
import { createSampleCommand } from "./createSample";
import * as path from "path";

function filterOptionsIfNotCopilotPlugin(options: CLICommandOption[]) {
function adjustOptions(options: CLICommandOption[]) {
if (!isCopilotPluginEnabled()) {
// filter out copilot-plugin in capability question
const capability = options.find(
(c: CLICommandOption) => c.name === CliQuestionName.Capability
) as CLIStringOption;
if (capability.choices) {
capability.choices = capability.choices.filter(
(c: string) => !copilotPluginOptionIds.includes(c)
);
}

//skip copilot plugin options if copilot plugin is not enabled
const copilotPluginQuestionNames = [
QuestionNames.ApiSpecLocation.toString(),
QuestionNames.OpenAIPluginDomain.toString(),
QuestionNames.ApiOperation.toString(),
];

options = options.filter((option) => !copilotPluginQuestionNames.includes(option.name));
}
for (const option of options) {
if (option.type === "string" && option.name === CliQuestionName.Capability) {
// use dynamic options for capability question
option.choices = CapabilityOptions.all({ platform: Platform.CLI }).map((o) => o.id);
break;
}
}
return options;
}

export function getCreateCommand(): CLICommand {
return {
name: "new",
description: "Create a new Microsoft Teams application.",
options: [...filterOptionsIfNotCopilotPlugin(CreateProjectOptions)],
options: [...adjustOptions(CreateProjectOptions)],
examples: [
{
command: "teamsfx new -c notification -t timer-functions -l typescript -n myapp -i false",
Expand Down
9 changes: 7 additions & 2 deletions packages/fx-core/src/question/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ export class CapabilityOptions {
];
}

/**
* static capability list, which does not depend on any feature flags
*/
static staticAll(inputs?: Inputs): OptionItem[] {
const capabilityOptions = [
...CapabilityOptions.bots(inputs),
Expand All @@ -422,17 +425,19 @@ export class CapabilityOptions {
return capabilityOptions;
}

/**
* dynamic capability list, which depends on feature flags
*/
static all(inputs?: Inputs): OptionItem[] {
// teamsfx list templates
const capabilityOptions = [
...CapabilityOptions.bots(inputs),
...CapabilityOptions.tabs(),
...CapabilityOptions.mes(),
CapabilityOptions.SearchMe(),
];
if (isCopilotPluginEnabled()) {
capabilityOptions.push(...CapabilityOptions.copilotPlugins());
}

return capabilityOptions;
}

Expand Down
17 changes: 0 additions & 17 deletions packages/fx-core/tests/question/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,6 @@ describe("scaffold question", () => {
questions.push(question.name);
await callFuncs(question, inputs);
if (question.name === QuestionNames.Capabilities) {
const select = question as SingleSelectQuestion;
const staticOptions = select.staticOptions;
assert.isTrue(staticOptions.length === 16);
const options = await select.dynamicOptions!(inputs);
assert.isTrue(options.length === 12);
return ok({ type: "success", result: CapabilityOptions.notificationBot().id });
} else if (question.name === QuestionNames.BotTrigger) {
return ok({ type: "success", result: NotificationTriggerOptions.appService().id });
Expand Down Expand Up @@ -636,17 +631,8 @@ describe("scaffold question", () => {
if (question.name === QuestionNames.Runtime) {
return ok({ type: "success", result: RuntimeOptions.DotNet().id });
} else if (question.name === QuestionNames.Capabilities) {
const select = question as SingleSelectQuestion;
const options = await select.dynamicOptions!(inputs);
assert.isTrue(options.length === 12);
return ok({ type: "success", result: CapabilityOptions.notificationBot().id });
} else if (question.name === QuestionNames.BotTrigger) {
const select = question as SingleSelectQuestion;
const options = await select.dynamicOptions!(inputs);
assert.deepEqual(options, [
NotificationTriggerOptions.appServiceForVS(),
...NotificationTriggerOptions.functionsTriggers(),
]);
return ok({ type: "success", result: NotificationTriggerOptions.appServiceForVS().id });
} else if (question.name === QuestionNames.ProgrammingLanguage) {
return ok({ type: "success", result: "javascript" });
Expand Down Expand Up @@ -863,9 +849,6 @@ describe("scaffold question", () => {
questions.push(question.name);
await callFuncs(question, inputs);
if (question.name === QuestionNames.Capabilities) {
const select = question as SingleSelectQuestion;
const options = await select.dynamicOptions!(inputs);
assert.isTrue(options.length === 15);
return ok({ type: "success", result: CapabilityOptions.copilotPluginNewApi().id });
} else if (question.name === QuestionNames.ProgrammingLanguage) {
return ok({ type: "success", result: "javascript" });
Expand Down

0 comments on commit 1744565

Please sign in to comment.