Skip to content
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
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/cli/lib/PromptSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export class PromptSession extends BasePromptSession {
return result;
}

protected override async completeAndRun(port?: number) {
protected override async complete() {
await PackageManager.installPackages();
await PackageManager.flushQueue(true);
await start.start({ port });
}

protected override async upgradePackages() {
Expand Down
43 changes: 19 additions & 24 deletions packages/core/prompt/BasePromptSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { InquirerWrapper } from "./InquirerWrapper";

export abstract class BasePromptSession {
protected config: Config;
private _newProjectName: string | null = null;

protected get templateManager(): BaseTemplateManager {
return App.container.get<BaseTemplateManager>(TEMPLATE_MANAGER);
Expand Down Expand Up @@ -72,6 +73,7 @@ export abstract class BasePromptSession {
Util.gitInit(process.cwd(), projectName);
}
// move cwd to project folder
this._newProjectName = projectName;
process.chdir(projectName);
await this.configureAI(framework.id);
}
Expand All @@ -95,8 +97,8 @@ export abstract class BasePromptSession {
}
}

/** Install packages and run project */
protected abstract completeAndRun(port?: number);
/** Install packages */
protected abstract complete(): Promise<void>;

/** Upgrade packages to use private Infragistics feed */
protected abstract upgradePackages();
Expand Down Expand Up @@ -395,7 +397,7 @@ export abstract class BasePromptSession {
name: "action",
message: "Choose an action:",
choices: this.generateActionChoices(context.projectLibrary),
default: "Complete & Run"
default: "Complete and Install packages"
});

runner.clearPending();
Expand Down Expand Up @@ -425,7 +427,7 @@ export abstract class BasePromptSession {
runner.addTask(this.templateSelectedTask("view"));
runner.addTask(run => Promise.resolve(run.resetTasks()));
break;
case "Complete & Run":
case "Complete and Install packages":
const config = ProjectConfig.localConfig();

if (config.project.framework === "angular" &&
Expand All @@ -450,30 +452,23 @@ export abstract class BasePromptSession {
}
}

const defaultPort = config.project.defaultPort;
const port = await this.getUserInput({
type: "input",
name: "port",
message: "Choose app host port:",
default: defaultPort,
validate: (input: string) => {
if (!Number(input)) {
Util.log(""); /* new line */
Util.error(`port should be a number. Input valid port or use the suggested default port`, "red");
return false;
}
return true;
}
});
config.project.defaultPort = parseInt(port, 10);
ProjectConfig.setConfig(config);

await this.completeAndRun(config.project.defaultPort);
await this.complete();
this.showNextSteps();
break;
}
return true;
}

private showNextSteps() {
Util.log("");
Util.log("Next Steps:");
if (this._newProjectName) {
Util.log(` cd ${this._newProjectName}`);
}
Util.log(" ig add start guided mode for adding views to the app");
Util.log(" ig start starts a web server and opens the app in the default browser");
}
Comment on lines +462 to +470

/**
* Get component group from user input
* @param projectLibrary to add component to
Expand Down Expand Up @@ -631,7 +626,7 @@ export abstract class BasePromptSession {
*/
private generateActionChoices(projectLibrary: ProjectLibrary): Array<{}> {
const actionChoices: ChoiceItem[] = [
{ name: "Complete & Run", description: "install packages and run in the default browser" }
{ name: "Complete and Install packages", description: "install packages and show next steps" }
];

/* istanbul ignore next */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class SchematicsPromptSession extends BasePromptSession {
return super.nameIsValid(name, checkFolder);
}

protected completeAndRun(_port?: number) {
protected async complete() {
// TODO?
}

Expand Down
72 changes: 28 additions & 44 deletions spec/unit/PromptSession-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,21 +500,20 @@ describe("Unit - PromptSession", () => {
Promise.resolve("Custom Group 1"), // select a component group
Promise.resolve("Custom Group 1 Component 2"), // select a component
Promise.resolve("Template 1"), // select a template
Promise.resolve("Complete & Run") // finalize the app
Promise.resolve("Complete and Install packages") // finalize the app
);

spyOn(InquirerWrapper, "input").and.returnValues(
Promise.resolve("Template 1 Custom Name"), // enter a custom name for the template, invoked immediately after a template has been selected
Promise.resolve("7777") // choose a port to run the app on
Promise.resolve("Template 1 Custom Name") // enter a custom name for the template, invoked immediately after a template has been selected
);

spyOn(ProjectConfig, "setConfig");
await mockSession.chooseActionLoop(mockProjectLibrary);
expect(mockSession.chooseActionLoop).toHaveBeenCalledTimes(1);
expect(InquirerWrapper.select).toHaveBeenCalledTimes(9);
expect(Util.log).toHaveBeenCalledTimes(3);
expect(Util.log).toHaveBeenCalledTimes(7);
expect(PackageManager.flushQueue).toHaveBeenCalledWith(true);
expect(start.start).toHaveBeenCalledTimes(1);
expect(start.start).not.toHaveBeenCalled();

expect(add.addTemplate).toHaveBeenCalledTimes(1);
expect(InquirerWrapper.input).toHaveBeenCalledWith({
Expand Down Expand Up @@ -573,13 +572,12 @@ describe("Unit - PromptSession", () => {
Promise.resolve("Back"), // return to the previous menu
Promise.resolve("Add scenario"), // attempt to add a scenario
Promise.resolve("Custom Template 1"), // select a template
Promise.resolve("Complete & Run"), // finalize the app
Promise.resolve("Complete and Install packages"), // finalize the app
Promise.resolve("no") // do not use paid angular
);

spyOn(InquirerWrapper, "input").and.returnValues(
Promise.resolve("Custom Template Name"), // enter a custom name for the template, invoked immediately after a template has been selected
Promise.resolve("7777") // choose a port to run the app on
Promise.resolve("Custom Template Name") // enter a custom name for the template, invoked immediately after a template has been selected
);

spyOn(ProjectConfig, "setConfig");
Expand All @@ -588,10 +586,10 @@ describe("Unit - PromptSession", () => {
await mockSession.chooseActionLoop(mockProjectLibrary);
expect(mockSession.chooseActionLoop).toHaveBeenCalledTimes(1);
expect(InquirerWrapper.select).toHaveBeenCalledTimes(5);
expect(InquirerWrapper.input).toHaveBeenCalledTimes(2);
expect(Util.log).toHaveBeenCalledTimes(3);
expect(InquirerWrapper.input).toHaveBeenCalledTimes(1);
expect(Util.log).toHaveBeenCalledTimes(7);
expect(PackageManager.flushQueue).toHaveBeenCalledWith(true);
expect(start.start).toHaveBeenCalledTimes(1);
expect(start.start).not.toHaveBeenCalled();

expect(Util.getAvailableName).toHaveBeenCalledTimes(1);
expect(add.addTemplate).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -698,27 +696,26 @@ describe("Unit - PromptSession", () => {
Promise.resolve("Custom Group 1 Component 2"), // select a component
Promise.resolve("Template 1"), // select a template
Promise.resolve("Choice 1"), // setup extra configuration for the template
Promise.resolve("Complete & Run") // finalize the app
Promise.resolve("Complete and Install packages") // finalize the app
);

spyOn(InquirerWrapper, "checkbox").and.returnValues(
Promise.resolve(["Choice 1"])
);

spyOn(InquirerWrapper, "input").and.returnValues(
Promise.resolve("Template 1 Custom Name"), // enter a custom name for the template, invoked immediately after a template has been selected
Promise.resolve("7777") // choose a port to run the app on
Promise.resolve("Template 1 Custom Name") // enter a custom name for the template, invoked immediately after a template has been selected
);

spyOn(ProjectConfig, "setConfig");
await mockSession.chooseActionLoop(mockProjectLibrary);
expect(mockSession.chooseActionLoop).toHaveBeenCalledTimes(1);
expect(InquirerWrapper.select).toHaveBeenCalledTimes(10);
expect(InquirerWrapper.input).toHaveBeenCalledTimes(2);
expect(InquirerWrapper.input).toHaveBeenCalledTimes(1);
expect(InquirerWrapper.checkbox).toHaveBeenCalledTimes(1);
expect(Util.log).toHaveBeenCalledTimes(3);
expect(Util.log).toHaveBeenCalledTimes(7);
expect(PackageManager.flushQueue).toHaveBeenCalledWith(true);
expect(start.start).toHaveBeenCalledTimes(1);
expect(start.start).not.toHaveBeenCalled();

expect(add.addTemplate).toHaveBeenCalledTimes(1);
expect(InquirerWrapper.checkbox).toHaveBeenCalledWith({
Expand All @@ -729,7 +726,7 @@ describe("Unit - PromptSession", () => {
choices: ["Choice 1", "Choice 2", "Choice 3"]
});
});
it("chooseActionLoop - Complete and Run should update default port", async () => {
it("chooseActionLoop - Complete should install packages and show next steps", async () => {
const mockProject = {
generateConfig: () => Promise.resolve(true)
};
Expand Down Expand Up @@ -760,33 +757,21 @@ describe("Unit - PromptSession", () => {
App.container.set(TEMPLATE_MANAGER, mockTemplate);
const mockSession = new PromptSession();
spyOn(mockSession, "chooseActionLoop").and.callThrough();
spyOn(Util, "log");
spyOn(InquirerWrapper, "select").and.returnValues(
Promise.resolve("Complete & Run"),
Promise.resolve("Complete and Install packages"),
);
spyOn(InquirerWrapper, "input").and.returnValues(
Promise.resolve("7777")
);
mockProjectConfig.project.defaultPort = 7777;
spyOn(PackageManager, "flushQueue").and.returnValue(Promise.resolve());
spyOn(PackageManager, "installPackages").and.returnValue(Promise.resolve());
spyOn(start, "start");
spyOn(ProjectConfig, "setConfig");

await mockSession.chooseActionLoop(mockProjectLibrary);
expect(start.start).toHaveBeenCalledWith({ port: 7777 });
expect(ProjectConfig.setConfig).toHaveBeenCalledWith(mockProjectConfig);

// validate:
spyOn(Util, "log");
spyOn(Util, "error");
const lastCallArgs = (InquirerWrapper.input as jasmine.Spy).calls.mostRecent().args[0];
expect(lastCallArgs.validate).toEqual(jasmine.any(Function));

expect(lastCallArgs.validate("not a number")).toBe(false);
expect(lastCallArgs.validate("1a")).toBe(false);
expect(Util.error).toHaveBeenCalledWith(
"port should be a number. Input valid port or use the suggested default port",
"red");
expect(lastCallArgs.validate("3210")).toBe(true);
expect(Util.error).toHaveBeenCalledTimes(2);
expect(start.start).not.toHaveBeenCalled();
expect(PackageManager.installPackages).toHaveBeenCalledTimes(1);
expect(PackageManager.flushQueue).toHaveBeenCalledWith(true);
expect(Util.log).toHaveBeenCalledWith("Next Steps:");
expect(Util.log).toHaveBeenCalledWith(" ig add start guided mode for adding views to the app");
expect(Util.log).toHaveBeenCalledWith(" ig start starts a web server and opens the app in the default browser");
});
it("chooseActionLoop - should call `upgradePackages` when update angular is true", async () => {
const mockProjectConfig = {
Expand All @@ -804,11 +789,10 @@ describe("Unit - PromptSession", () => {
const mockSession = new PromptSession();
spyOn(mockSession as any, "generateActionChoices").and.returnValues([]);
spyOn(mockSession as any, "getUserInput").and.returnValues(
Promise.resolve("Complete & Run"),
Promise.resolve("yes"),
Promise.resolve(4200)
Promise.resolve("Complete and Install packages"),
Promise.resolve("yes")
);
spyOn(mockSession as any, "completeAndRun").and.returnValues(Promise.resolve());
spyOn(mockSession as any, "complete").and.returnValues(Promise.resolve());
spyOn(upgrade, "upgrade").and.returnValue();

await mockSession.chooseActionLoop({} as any);
Expand Down