Skip to content

Commit ebdbcb3

Browse files
authored
Merge pull request #1669 from IgniteUI/copilot/refactor-template-manager-usage
refactor: use TemplateManager from App container
2 parents 1c6f4a2 + 726fe2e commit ebdbcb3

22 files changed

Lines changed: 166 additions & 154 deletions

packages/cli/lib/PromptSession.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,9 @@ import { default as add } from "./commands/add";
77
import { configure as aiConfigure } from "./commands/ai-config";
88
import { default as start } from "./commands/start";
99
import { default as upgrade } from "./commands/upgrade";
10-
import { TemplateManager } from "./TemplateManager";
1110

1211
export class PromptSession extends BasePromptSession {
1312

14-
constructor(templateManager: TemplateManager) {
15-
super(templateManager);
16-
}
17-
1813
public static async chooseTerm() {
1914
const answer = await InquirerWrapper.input({
2015
default: null,
@@ -39,7 +34,6 @@ export class PromptSession extends BasePromptSession {
3934

4035
let projLibrary: ProjectLibrary;
4136
let theme: string;
42-
add.templateManager = this.templateManager as TemplateManager;
4337
this.config = ProjectConfig.getConfig();
4438
const defaultProjName = "IG Project";
4539

@@ -101,7 +95,6 @@ export class PromptSession extends BasePromptSession {
10195
}
10296

10397
protected async upgradePackages() {
104-
upgrade.templateManager = this.templateManager as TemplateManager;
10598
await upgrade.upgrade({ skipInstall: true, _: ["upgrade"], $0: "upgrade" });
10699
}
107100

packages/cli/lib/cli.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,9 @@ export async function run(args = null) {
3131
App.initialize();
3232

3333
const templateManager = new TemplateManager();
34-
// TODO: Refactor all code to use TemplateManager from the App container:
3534
App.container.set(TEMPLATE_MANAGER, templateManager);
3635

3736
newCommand.addChoices(templateManager.getFrameworkIds());
38-
newCommand.templateManager = templateManager;
39-
add.templateManager = templateManager;
40-
build.templateManager = templateManager;
41-
start.templateManager = templateManager;
42-
generate.templateManager = templateManager;
43-
list.templateManager = templateManager;
44-
upgrade.templateManager = templateManager;
4537

4638
const registeredCommands: CommandType[] = [
4739
newCommand, add, build, start, generate, config, doc, test, list, upgrade, mcp, aiConfig
@@ -74,7 +66,7 @@ export async function run(args = null) {
7466
} else {
7567
Util.log("Starting Step by step mode.", "green");
7668
Util.log("For available commands, stop this execution and use --help.", "green");
77-
const prompts = new PromptSession(templateManager);
69+
const prompts = new PromptSession();
7870
prompts.start();
7971
}
8072
}

packages/cli/lib/commands/add.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2-
AddTemplateArgs, GoogleAnalytics, PackageManager,
3-
ProjectConfig, ProjectLibrary, Template, Util
2+
AddTemplateArgs, App, BaseTemplateManager, GoogleAnalytics, PackageManager,
3+
ProjectConfig, ProjectLibrary, TEMPLATE_MANAGER, Template, Util
44
} from "@igniteui/cli-core";
55
import { PromptSession } from "./../PromptSession";
66
import { AddCommandType, PositionalArgs } from "./types";
@@ -11,7 +11,6 @@ let yargsContext: Argv | undefined;
1111
const command: AddCommandType = {
1212
command: "add [template] [name]",
1313
describe: "Adds a component or view template to the current project",
14-
templateManager: null,
1514
builder: (yargs) => {
1615
yargsContext = yargs;
1716
return yargs
@@ -76,18 +75,19 @@ const command: AddCommandType = {
7675
Util.error("Showcases projects don't support the add command", "red");
7776
return;
7877
}
79-
const framework = command.templateManager.getFrameworkById(config.project.framework);
78+
const templateManager = App.container.get<BaseTemplateManager>(TEMPLATE_MANAGER);
79+
const framework = templateManager.getFrameworkById(config.project.framework);
8080
if (!framework) {
8181
Util.error("Framework not supported", "red");
8282
return;
8383
}
84-
const frameworkLibrary = command.templateManager.getProjectLibrary(
84+
const frameworkLibrary = templateManager.getProjectLibrary(
8585
config.project.framework,
8686
config.project.projectType
8787
) as ProjectLibrary;
8888

8989
if (!argv.template && !argv.name) {
90-
const prompts = new PromptSession(command.templateManager);
90+
const prompts = new PromptSession();
9191
await prompts.chooseActionLoop(frameworkLibrary);
9292
return;
9393
}
@@ -123,7 +123,7 @@ const command: AddCommandType = {
123123
skipRoute: argv.skipRoute
124124
});
125125
await PackageManager.flushQueue(true);
126-
await PackageManager.ensureIgniteUISource(config.packagesInstalled, command.templateManager);
126+
await PackageManager.ensureIgniteUISource(config.packagesInstalled, templateManager);
127127
}
128128
},
129129
async addTemplate(fileName: string, template: Template, options?: AddTemplateArgs): Promise<boolean> {
@@ -165,7 +165,7 @@ const command: AddCommandType = {
165165
}
166166
if (!fail && templatePaths.length) {
167167
template.registerInProject(process.cwd(), fileName, options || {});
168-
command.templateManager.updateProjectConfiguration(template);
168+
App.container.get<BaseTemplateManager>(TEMPLATE_MANAGER).updateProjectConfiguration(template);
169169
template.packages.forEach(x => PackageManager.queuePackage(x));
170170
Util.log(`${Util.greenCheck()} View '${name}' added.`);
171171
return true;

packages/cli/lib/commands/build.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GoogleAnalytics, PackageManager, ProjectConfig, Util } from "@igniteui/cli-core";
1+
import { App, BaseTemplateManager, GoogleAnalytics, PackageManager, ProjectConfig, TEMPLATE_MANAGER, Util } from "@igniteui/cli-core";
22
import * as fs from "fs";
33
import * as path from "path";
44
import { BuildCommandType, PositionalArgs } from "./types";
@@ -8,7 +8,6 @@ const command: BuildCommandType = {
88
command: "build",
99
describe: "Builds the project",
1010
builder: (yargs) => yargs,
11-
templateManager: null,
1211
async handler(argv: ArgumentsCamelCase<PositionalArgs>) {
1312

1413
GoogleAnalytics.post({
@@ -19,7 +18,8 @@ const command: BuildCommandType = {
1918
},
2019
async build() {
2120
Util.log("Build started.");
22-
await PackageManager.ensureIgniteUISource(true, command.templateManager);
21+
const templateManager = App.container.get<BaseTemplateManager>(TEMPLATE_MANAGER);
22+
await PackageManager.ensureIgniteUISource(true, templateManager);
2323

2424
if (!ProjectConfig.hasLocalConfig()) {
2525
Util.error("Build command is supported only on existing project created with igniteui-cli", "red");

packages/cli/lib/commands/generate.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GoogleAnalytics, ProjectLibrary, Util } from "@igniteui/cli-core";
1+
import { App, BaseTemplateManager, GoogleAnalytics, ProjectLibrary, TEMPLATE_MANAGER, Util } from "@igniteui/cli-core";
22
import * as path from "path";
33
import { default as config } from "./config";
44
import { CommandType, PositionalArgs } from "./types";
@@ -27,18 +27,19 @@ async function handler(argv: ArgumentsCamelCase<PositionalArgs>) {
2727
return;
2828
}
2929

30-
if (command.templateManager.getFrameworkById(argv.framework) === undefined) {
30+
const templateManager = App.container.get<BaseTemplateManager>(TEMPLATE_MANAGER);
31+
if (templateManager.getFrameworkById(argv.framework) === undefined) {
3132
return Util.error("Framework not supported", "red");
3233
}
3334

3435
let projectLib: ProjectLibrary;
3536
if (argv.type) {
36-
projectLib = command.templateManager.getProjectLibrary(argv.framework, argv.type) as ProjectLibrary;
37+
projectLib = templateManager.getProjectLibrary(argv.framework, argv.type) as ProjectLibrary;
3738
if (!projectLib) {
3839
return Util.error(`Project type '${argv.type}' not found in framework '${argv.framework}'`);
3940
}
4041
} else {
41-
projectLib = command.templateManager.getProjectLibrary(argv.framework) as ProjectLibrary;
42+
projectLib = templateManager.getProjectLibrary(argv.framework) as ProjectLibrary;
4243
argv.type = projectLib.projectType;
4344
}
4445

@@ -78,7 +79,6 @@ const command: CommandType = {
7879
aliases: ["g"],
7980
command: "generate",
8081
describe: "Generates custom templates (see subcommands)",
81-
templateManager: null,
8282
builder: yargs => {
8383
yargs
8484
.command({

packages/cli/lib/commands/list.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Config, Framework, GoogleAnalytics, ProjectConfig, ProjectLibrary, Util } from "@igniteui/cli-core";
1+
import { App, BaseTemplateManager, Config, Framework, GoogleAnalytics, ProjectConfig, ProjectLibrary, TEMPLATE_MANAGER, Util } from "@igniteui/cli-core";
22
import { CommandType, PositionalArgs } from "./types";
33
import { ArgumentsCamelCase } from "yargs";
44

@@ -23,7 +23,6 @@ const command: CommandType = {
2323
.example("$0 list", "Show all frameworks and their project templates")
2424
.example("$0 list -f angular", "List component templates for Angular");
2525
},
26-
templateManager: null,
2726
handler(argv: ArgumentsCamelCase<PositionalArgs>) {
2827
GoogleAnalytics.post({
2928
t: "screenview",
@@ -49,19 +48,20 @@ const command: CommandType = {
4948
const templatesByGroup = [];
5049
const controlGroups: string[] = [];
5150

52-
const framework: Framework = command.templateManager.getFrameworkById(argv.framework);
51+
const templateManager = App.container.get<BaseTemplateManager>(TEMPLATE_MANAGER);
52+
const framework: Framework = templateManager.getFrameworkById(argv.framework);
5353
if (!framework) {
5454
return Util.error("Wrong framework provided", "red");
5555
}
5656

5757
let projectLib: ProjectLibrary;
5858
if (argv.type) {
59-
projectLib = command.templateManager.getProjectLibrary(argv.framework, argv.type) as ProjectLibrary;
59+
projectLib = templateManager.getProjectLibrary(argv.framework, argv.type) as ProjectLibrary;
6060
if (!projectLib) {
6161
return Util.error(`Project type '${argv.type}' not found in framework '${argv.framework}'`, "red");
6262
}
6363
} else {
64-
projectLib = command.templateManager.getProjectLibrary(argv.framework) as ProjectLibrary;
64+
projectLib = templateManager.getProjectLibrary(argv.framework) as ProjectLibrary;
6565
}
6666

6767
let maxIdLength = 0;
@@ -115,9 +115,10 @@ const command: CommandType = {
115115
};
116116

117117
function listAllFrameworks() {
118-
const frameworkIds: string[] = command.templateManager.getFrameworkIds();
118+
const templateManager = App.container.get<BaseTemplateManager>(TEMPLATE_MANAGER);
119+
const frameworkIds: string[] = templateManager.getFrameworkIds();
119120
const frameworks: Framework[] = frameworkIds
120-
.map(id => command.templateManager.getFrameworkById(id))
121+
.map(id => templateManager.getFrameworkById(id))
121122
.filter(f => !!f);
122123

123124
GoogleAnalytics.post({

packages/cli/lib/commands/mcp.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { ArgumentsCamelCase } from "yargs";
77
const command: CommandType = {
88
command: "mcp",
99
describe: "Starts the Ignite UI MCP server for AI assistant integration",
10-
templateManager: null,
1110
builder: (yargs) => {
1211
return yargs
1312
.option("remote", {

packages/cli/lib/commands/new.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { AI_AGENT_CHOICES, AIAgentTarget, GoogleAnalytics, PackageManager, ProjectConfig, ProjectLibrary, Util } from "@igniteui/cli-core";
1+
import { AI_AGENT_CHOICES, AIAgentTarget, App, type BaseTemplateManager, GoogleAnalytics, PackageManager, ProjectConfig, ProjectLibrary, TEMPLATE_MANAGER, Util } from "@igniteui/cli-core";
22
import * as path from "path";
33
import { PromptSession } from "./../PromptSession";
44
import { NewCommandType, PositionalArgs } from "./types";
5-
import { TemplateManager } from "../TemplateManager";
65
import { ArgumentsCamelCase, Choices } from "yargs";
76
import { configure } from "./ai-config";
87

@@ -78,8 +77,9 @@ const command: NewCommandType = {
7877
if (ProjectConfig.hasLocalConfig()) {
7978
return Util.error("There is already an existing project.", "red");
8079
}
80+
const templateManager = App.container.get<BaseTemplateManager>(TEMPLATE_MANAGER);
8181
if (!argv.name) {
82-
const prompts = new PromptSession(command.templateManager || new TemplateManager());
82+
const prompts = new PromptSession();
8383
await prompts.start();
8484
return;
8585
}
@@ -99,20 +99,20 @@ const command: NewCommandType = {
9999
Util.error(`Folder "${argv.name}" already exists!`, "red");
100100
return;
101101
}
102-
if (command.templateManager?.getFrameworkById(argv.framework) === undefined) {
102+
if (templateManager?.getFrameworkById(argv.framework) === undefined) {
103103
return Util.error("Framework not supported", "red");
104104
}
105105
let projectLib: ProjectLibrary;
106106
if (argv.type) {
107-
projectLib = command.templateManager?.getProjectLibrary(argv.framework, argv.type) as ProjectLibrary;
107+
projectLib = templateManager?.getProjectLibrary(argv.framework, argv.type) as ProjectLibrary;
108108
if (!projectLib) {
109109
return Util.error(`Project type "${argv.type}" not found in framework '${argv.framework}'`);
110110
}
111111
} else {
112-
projectLib = command.templateManager?.getProjectLibrary(argv.framework) as ProjectLibrary;
112+
projectLib = templateManager?.getProjectLibrary(argv.framework) as ProjectLibrary;
113113
}
114114

115-
if (command.templateManager?.getFrameworkById(argv.framework).id === "angular" && projectLib.projectType === "igx-ts") {
115+
if (templateManager?.getFrameworkById(argv.framework).id === "angular" && projectLib.projectType === "igx-ts") {
116116
Util.warn("Psst! Did you know you can use our schematics package with Angular CLI to create and modify your projects?", "yellow");
117117
Util.warn("Read more at: https://www.infragistics.com/products/ignite-ui-angular/angular/components/general/cli-overview", "yellow");
118118
}

packages/cli/lib/commands/start.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const execSyncNpmStart = (port: number, options: ExecSyncOptions): void => {
2222
const command: StartCommandType = {
2323
command: "start",
2424
describe: "Starts the project",
25-
templateManager: null,
2625
builder: (yargs) => {
2726
return yargs
2827
.option("port", {

packages/cli/lib/commands/types.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ArgumentsCamelCase, CommandModule } from "yargs";
2-
import { TemplateManager } from "../TemplateManager";
32
import { AddTemplateArgs, Template } from "@igniteui/cli-core";
43

54
export interface PositionalArgs {
@@ -40,8 +39,6 @@ export interface PositionalArgs {
4039
}
4140

4241
export interface CommandType extends CommandModule<{}, any> {
43-
templateManager?: TemplateManager;
44-
4542
/** Handler function that will be called by yargs after the command line has been parsed. */
4643
handler(/** do not use `this` in handler */this: void, argv: ArgumentsCamelCase<PositionalArgs>);
4744
}

0 commit comments

Comments
 (0)