Skip to content

Commit

Permalink
refactor: cli help question trim (#9203)
Browse files Browse the repository at this point in the history
* refactor: cli help question trim

* refactor: cleanup

* test: ut

* refactor: improve question string

* refactor: improve question string
  • Loading branch information
jayzhang authored Jul 4, 2023
1 parent 09fb224 commit 12d8de4
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 14 deletions.
6 changes: 3 additions & 3 deletions packages/fx-core/src/core/FxCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Void,
} from "@microsoft/teamsfx-api";
import { DotenvParseOutput } from "dotenv";
import * as path from "path";
import "reflect-metadata";
import { TelemetryReporterInstance } from "../common/telemetry";
import { ILifecycle, LifecycleName } from "../component/configManager/interface";
Expand All @@ -33,17 +34,16 @@ import { envUtil } from "../component/utils/envUtil";
import { metadataUtil } from "../component/utils/metadataUtil";
import { pathUtils } from "../component/utils/pathUtils";
import { settingsUtil } from "../component/utils/settingsUtil";
import { getQuestionsForCreateProjectCliHelp } from "../question/create";
import { CallbackRegistry } from "./callback";
import { LocalCrypto } from "./crypto";
import { environmentManager } from "./environment";
import { InvalidInputError } from "./error";
import { FxCoreV3Implement } from "./FxCoreImplementV3";
import { setTools, TOOLS } from "./globalVars";
import { ErrorHandlerMW } from "./middleware/errorHandler";
import { getQuestionsForCreateProjectV2 } from "./middleware/questionModel";
import { CoreQuestionNames } from "./question";
import { PreProvisionResForVS, VersionCheckRes } from "./types";
import * as path from "path";

export type CoreCallbackFunc = (name: string, err?: FxError, data?: any) => void;

Expand Down Expand Up @@ -186,7 +186,7 @@ export class FxCore {
): Promise<Result<QTreeNode | undefined, FxError>> {
inputs.stage = Stage.getQuestions;
if (stage === Stage.create) {
return await getQuestionsForCreateProjectV2(inputs);
return ok(getQuestionsForCreateProjectCliHelp() as QTreeNode);
}
return ok(undefined);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/fx-core/src/core/FxCoreImplementV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import { pathUtils } from "../component/utils/pathUtils";
import { FileNotFoundError, InvalidProjectError, UserCancelError } from "../error/common";
import { NoNeedUpgradeError } from "../error/upgrade";
import { YamlFieldMissingError } from "../error/yml";
import { getQuestionsForCreateProjectNew } from "../question/create";
import { getQuestionsForCreateProject } from "../question/create";
import { checkPermission, grantPermission, listCollaborator } from "./collaborator";
import { InvalidInputError, ObjectIsUndefinedError } from "./error";
import { TOOLS } from "./globalVars";
Expand Down Expand Up @@ -126,7 +126,7 @@ export class FxCoreV3Implement {
return await method.call(this, func, inputs);
}

@hooks([ErrorHandlerMW, QuestionMW(getQuestionsForCreateProjectNew)])
@hooks([ErrorHandlerMW, QuestionMW(getQuestionsForCreateProject)])
async createProject(inputs: Inputs): Promise<Result<string, FxError>> {
const context = createContextV3();
if (inputs.teamsAppFromTdp) {
Expand Down
60 changes: 52 additions & 8 deletions packages/fx-core/src/question/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ import { isPersonalApp, needBotCode } from "../component/driver/teamsApp/utils/u
import { StaticTab } from "../component/driver/teamsApp/interfaces/appdefinitions/staticTab";
import { Utils } from "../component/generator/spfx/utils/utils";
import semver from "semver";
import { cloneDeep } from "lodash";

export enum QuestionNames {
Scratch = "scratch",
SctatchYes = "scratch-yes",
AppName = "app-name",
Folder = "folder",
ProgrammingLanguage = "programming-language",
Expand All @@ -58,12 +60,14 @@ export enum QuestionNames {
OfficeAddinManifest = "addin-project-manifest",
OfficeAddinTemplate = "addin-template-select",
OfficeAddinHost = "addin-host",
OfficeAddinImport = "addin-import",
SkipAppName = "skip-app-name",
Samples = "samples",
ReplaceContentUrl = "replaceContentUrl",
ReplaceWebsiteUrl = "replaceWebsiteUrl",
ReplaceBotIds = "replaceBotIds",
SafeProjectName = "safeProjectName",
RepalceTabUrl = "tdp-tab-url",
}

export class ScratchOptions {
Expand Down Expand Up @@ -423,7 +427,7 @@ function capabilityQuestion(): SingleSelectQuestion {
case ProjectTypeOptions.outlookAddin().id:
return getLocalizedString("core.createProjectQuestion.projectType.outlookAddin.title");
default:
return "";
return getLocalizedString("core.createCapabilityQuestion.titleNew");
}
},
type: "singleSelect",
Expand Down Expand Up @@ -458,7 +462,7 @@ function capabilityQuestion(): SingleSelectQuestion {
];
}
},
placeholder: getLocalizedString("core.getCreateNewOrFromSampleQuestion.placeholder"),
placeholder: getLocalizedString("core.createCapabilityQuestion.placeholder"),
forgetLastValue: true,
skipSingleOption: true,
};
Expand Down Expand Up @@ -1157,7 +1161,7 @@ export const createProjectQuestion: IQTreeNode = {
children: [
{
condition: { equals: ScratchOptions.yes().id },
data: { type: "group" },
data: { type: "group", name: QuestionNames.SctatchYes },
children: [
{
condition: (inputs: Inputs) =>
Expand Down Expand Up @@ -1206,7 +1210,8 @@ export const createProjectQuestion: IQTreeNode = {
{
// office addin import sub-tree
condition: { equals: CapabilityOptions.officeAddinImport().id },
data: { type: "group" },
data: { type: "group", name: QuestionNames.OfficeAddinImport },

children: [
{
data: {
Expand Down Expand Up @@ -1248,7 +1253,7 @@ export const createProjectQuestion: IQTreeNode = {
{
condition: (inputs: Inputs) =>
inputs.teamsAppFromTdp && isPersonalApp(inputs.teamsAppFromTdp),
data: { type: "group" },
data: { type: "group", name: QuestionNames.RepalceTabUrl },
children: [
{
condition: (inputs: Inputs) => {
Expand Down Expand Up @@ -1294,8 +1299,47 @@ export const createProjectQuestion: IQTreeNode = {
],
};

export async function getQuestionsForCreateProjectNew(): Promise<
Result<IQTreeNode | undefined, FxError>
> {
export function getQuestionsForCreateProject(): Result<IQTreeNode, FxError> {
return ok(createProjectQuestion);
}

export function getQuestionsForCreateProjectCliHelp(): IQTreeNode {
const node = cloneDeep(createProjectQuestion);
trimQuestionTreeForCliHelp(node, [
QuestionNames.Runtime,
QuestionNames.ProjectType,
QuestionNames.SkipAppName,
QuestionNames.OfficeAddinImport,
QuestionNames.OfficeAddinHost,
QuestionNames.RepalceTabUrl,
QuestionNames.ReplaceBotIds,
QuestionNames.Samples,
]);
const subTree = pickSubTree(node, QuestionNames.SctatchYes);
return subTree!;
}

function trimQuestionTreeForCliHelp(node: IQTreeNode, deleteNames: string[]): void {
if (node.children) {
node.children = node.children.filter(
(child) => !child.data.name || !deleteNames.includes(child.data.name)
);
for (const child of node.children) {
trimQuestionTreeForCliHelp(child, deleteNames);
}
}
}

function pickSubTree(node: IQTreeNode, name: string): IQTreeNode | undefined {
if (node.data.name === name) {
return node;
}
let found;
if (node.children) {
for (const child of node.children) {
found = pickSubTree(child, name);
if (found) return found;
}
}
return undefined;
}
43 changes: 42 additions & 1 deletion packages/fx-core/tests/core/FxCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import {
Func,
FxError,
IQTreeNode,
Inputs,
LogProvider,
Ok,
Platform,
QTreeNode,
Result,
Stage,
SystemError,
Expand Down Expand Up @@ -45,7 +47,6 @@ import {
TabSPFxItem,
} from "../../src/component/constants";
import { coordinator } from "../../src/component/coordinator";
import "../../src/component/driver/aad/update";
import { UpdateAadAppDriver } from "../../src/component/driver/aad/update";
import { AddWebPartDriver } from "../../src/component/driver/add/addWebPart";
import { DriverContext } from "../../src/component/driver/interface/commonArgs";
Expand Down Expand Up @@ -74,6 +75,7 @@ import {
} from "../../src/error/common";
import { NoNeedUpgradeError } from "../../src/error/upgrade";
import { MockTools, deleteFolder, randomAppName } from "./utils";
import { QuestionNames } from "../../src/question/create";

const tools = new MockTools();

Expand Down Expand Up @@ -1320,4 +1322,43 @@ describe("isEnvFile", async () => {
assert.isTrue(res.value);
}
});

describe("getQuestions", async () => {
const sandbox = sinon.createSandbox();
afterEach(() => {
sandbox.restore();
});
it("happy path", async () => {
const core = new FxCore(tools);
const res = await core.getQuestions(Stage.create, { platform: Platform.CLI_HELP });
assert.isTrue(res.isOk());
if (res.isOk()) {
const node = res.value;
const names: string[] = [];
collectNodeNames(node!, names);
assert.deepEqual(names, [
"capabilities",
"bot-host-type-trigger",
"spfx-solution",
"spfx-install-latest-package",
"spfx-framework-type",
"spfx-webpart-name",
"spfx-folder",
"programming-language",
"folder",
"app-name",
]);
}
});
function collectNodeNames(node: IQTreeNode, names: string[]) {
if (node.data.type !== "group") {
names.push(node.data.name);
}
if (node.children) {
for (const child of node.children) {
collectNodeNames(child, names);
}
}
}
});
});

0 comments on commit 12d8de4

Please sign in to comment.