Skip to content

Commit

Permalink
feat(commands): Add 'upgrade-packages' command
Browse files Browse the repository at this point in the history
  • Loading branch information
dafo authored May 26, 2020
1 parent c194796 commit 25db37d
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/cli/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { default as newCommand } from "./commands/new";
import { default as quickstart } from "./commands/quickstart";
import { default as start } from "./commands/start";
import { default as test } from "./commands/test";
import { default as upgrade } from "./commands/upgrade";
import { PromptSession } from "./PromptSession";
import {TemplateManager} from "./TemplateManager";

Expand All @@ -36,6 +37,7 @@ export async function run(args = null) {
start.templateManager = templateManager;
generate.templateManager = templateManager;
list.templateManager = templateManager;
upgrade.templateManager = templateManager;

const yargsModule = args ? yargs(args) : yargs;

Expand All @@ -50,6 +52,7 @@ export async function run(args = null) {
.command(doc)
.command(test)
.command(list)
.command(upgrade)
.options({
version: {
alias: "v",
Expand Down Expand Up @@ -108,6 +111,9 @@ export async function run(args = null) {
case "list":
list.execute(argv);
break;
case "upgrade-packages":
await upgrade.execute(argv);
break;
default:
Util.log("Starting Step by step mode.", "green");
Util.log("For available commands, stop this execution and use --help.", "green");
Expand Down
77 changes: 77 additions & 0 deletions packages/cli/lib/commands/upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { GoogleAnalytics, ProjectConfig, Util } from "@igniteui/cli-core";
import { TemplateManager } from "../TemplateManager";

let command: {
[name: string]: any,
templateManager: TemplateManager,
execute: (argv: any) => Promise<void>,
upgrade: (argv: any) => Promise<void>
};

// tslint:disable:object-literal-sort-keys
command = {
command: "upgrade-packages",
desc: "upgrades Ignite UI Packages",
templateManager: null,
builder: {
"skip-install": {
alias: "si",
default: false,
describe: "Runs upgrade command without performing install",
type: "boolean"
}
},
async execute(argv) {

GoogleAnalytics.post({
t: "screenview",
cd: "Upgrade packages"
});

return command.upgrade(argv);
},
async upgrade(argv) {
const config = ProjectConfig.getConfig();
const framework = config.project.framework;
const projectType = config.project.projectType;

switch (framework.toLowerCase()) {
case "jquery":
Util.log("Upgrading packages for jQuery projects is currently not supported!");
return;
case "react":
Util.log("Upgrading packages for React projects is currently not supported!");
return;
case "angular":
if (projectType === "igx-ts") {
const projectLibrary = command.templateManager.getProjectLibrary(framework, projectType);
let project;
if (!config.project.projectTemplate) {
// in case project template is missing from the config we provide backward.
project = projectLibrary.getProject(projectLibrary.projectIds[0]);
} else {
project = projectLibrary.getProject(config.project.projectTemplate);
}
const success = await project.upgradeIgniteUIPackages(process.cwd(), "");
if (success && !argv.skipInstall) {
Util.execSync("npm install");
}
} else {
Util.log("Upgrading packages for Angular Wrappers projects is currently not supported!");
return;
}
break;
default:
break;
}

GoogleAnalytics.post({
t: "event",
ec: "$ig upgrade-packages",
cd1: framework,
cd2: projectType
});
}
};

export default command;
2 changes: 1 addition & 1 deletion packages/core/packages/PackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class PackageManager {
// TODO multiple projects?
let project: ProjectTemplate;
if (!config.project.projectTemplate) {
// in case project tempale is missing from the config we provide backward.
// in case project template is missing from the config we provide backward.
project = projectLibrary.getProject(projectLibrary.projectIds[0]);
} else {
project = projectLibrary.getProject(config.project.projectTemplate);
Expand Down
3 changes: 2 additions & 1 deletion spec/acceptance/help-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ describe("Help command", () => {
config gets, sets or adds a configuration property
doc [term] opens the Infragistics search for the given term
test executes project tests
list list all templates [aliases: l]
list list all templates [aliases: l]
upgrade-packages upgrades Ignite UI Packages
Options:
--version, -v Show current Ignite UI CLI version [boolean]
--help, -h Show help [boolean]`.replace(/\s/g, "");
Expand Down
76 changes: 76 additions & 0 deletions spec/unit/upgrade-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {
BaseTemplateManager, Config, GoogleAnalytics, PackageManager, ProjectConfig, ProjectLibrary, ProjectTemplate, Util
} from "@igniteui/cli-core";

import { default as upgradeCmd } from "../../packages/cli/lib/commands/upgrade";

describe("Unit - Upgrade command", () => {
beforeAll(() => {
spyOn(GoogleAnalytics, "post");
});

beforeEach(() => {
spyOn(Util, "log");
spyOn(process, "chdir");
spyOn(Util, "execSync");
spyOn(PackageManager, "installPackages");
});

it("Upgrade an Ignite UI for Angular project", async done => {
// tslint:disable-next-line: no-object-literal-type-assertion
const config: Config = {
project: {
framework: "angular",
projectTemplate: "test-template",
projectType: "igx-ts"
}
} as Config;
spyOn(ProjectConfig, "getConfig").and.returnValue(config);

const mockProject: Partial<ProjectTemplate> = {
upgradeIgniteUIPackages: () => null
};
const mockProjLib: Partial<ProjectLibrary> = { getProject: () => null };
const mockTemplateManager: Partial<BaseTemplateManager> = { getProjectLibrary: () => null };
upgradeCmd.templateManager = mockTemplateManager as any;
spyOn(mockTemplateManager, "getProjectLibrary").and.returnValue(mockProjLib);
spyOn(mockProjLib, "getProject").and.returnValue(mockProject);
const upgradeIgniteUIPackagesSpy = spyOn(mockProject, "upgradeIgniteUIPackages");

upgradeIgniteUIPackagesSpy.and.returnValue(Promise.resolve(true));
await upgradeCmd.execute({ skipInstall: true });
expect(mockTemplateManager.getProjectLibrary).toHaveBeenCalledTimes(1);
expect(mockTemplateManager.getProjectLibrary)
.toHaveBeenCalledWith(config.project.framework, config.project.projectType);
expect(mockProjLib.getProject).toHaveBeenCalledWith(config.project.projectTemplate);
expect(mockProject.upgradeIgniteUIPackages).toHaveBeenCalledTimes(1);
expect(mockProject.upgradeIgniteUIPackages).toHaveBeenCalledWith(process.cwd(), "");
expect(Util.execSync).not.toHaveBeenCalled();

upgradeIgniteUIPackagesSpy.and.returnValue(Promise.resolve(false));
await upgradeCmd.execute({ skipInstall: false });
expect(mockProject.upgradeIgniteUIPackages).toHaveBeenCalledTimes(2);
expect(Util.execSync).not.toHaveBeenCalled();

upgradeIgniteUIPackagesSpy.and.returnValue(Promise.resolve(true));
await upgradeCmd.execute({ skipInstall: false });
expect(mockProject.upgradeIgniteUIPackages).toHaveBeenCalledTimes(3);
expect(Util.execSync).toHaveBeenCalledWith("npm install");

done();
});

it("Logs error for not supported framework", async done => {
// tslint:disable-next-line: no-object-literal-type-assertion
const config: Config = {
project: {
framework: "jquery"
}
} as Config;
spyOn(ProjectConfig, "getConfig").and.returnValue(config);

await upgradeCmd.execute({});
expect(Util.log).toHaveBeenCalledTimes(1);
done();
});
});

0 comments on commit 25db37d

Please sign in to comment.