Skip to content

Commit

Permalink
feat(step-by-step): add prompt to upgrade to private feed packages, #724
Browse files Browse the repository at this point in the history


Co-authored-by: Damyan Petev <damyan.petev@gmail.com>
  • Loading branch information
wnvko and damyanpetev committed May 26, 2020
1 parent 25db37d commit 22a32dc
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 30 deletions.
6 changes: 6 additions & 0 deletions packages/cli/lib/PromptSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as inquirer from "inquirer";
import * as path from "path";
import { default as add } from "./commands/add";
import { default as start } from "./commands/start";
import { default as upgrade } from "./commands/upgrade";
import { TemplateManager } from "./TemplateManager";

export class PromptSession extends BasePromptSession {
Expand Down Expand Up @@ -101,6 +102,11 @@ export class PromptSession extends BasePromptSession {
}
}

protected async upgradePackages() {
upgrade.templateManager = this.templateManager as TemplateManager;
await upgrade.upgrade({ skipInstall: true });
}

/**
* Get user name and set template's extra configurations if any
* @param projectLibrary to add component to
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/lib/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ command = {
},
"skip-route": {
alias: "skr",
describe: "Don't auto-generate am app navigation route for the new component",
describe: "Don't auto-generate an app navigation route for the new component",
type: "boolean",
global: true
}
Expand Down
27 changes: 27 additions & 0 deletions packages/core/prompt/BasePromptSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ export abstract class BasePromptSession {
/** Install packages and run project */
protected abstract async completeAndRun(port?: number);

/** Upgrade packages to use private Infragistics feed */
protected abstract async upgradePackages();

/**
* Get user name and set template's extra configurations if any
* @param projectLibrary to add component to
Expand Down Expand Up @@ -369,6 +372,29 @@ export abstract class BasePromptSession {
break;
case "Complete & Run":
const config = ProjectConfig.localConfig();

if (config.project.framework === "angular" &&
config.project.projectType === "igx-ts" &&
!config.packagesInstalled) {
// TODO: should we add check if there are paid components at all
Util.log("The project will be created using a Trial version of Ignite UI for Angular.");
Util.log("You can always run the upgrade-packages command once it's created.");
const shouldUpgrade = await this.getUserInput({
type: "list",
name: "shouldUpgrade",
message: "Would you like to upgrade to the licensed feed now?",
choices: [
{ value: "yes", name: "Yes (requires active subscription)", short: "Yes" },
{ value: "no", name: "Skip for now", short: "Skip" }
],
default: "yes"
});

if (shouldUpgrade === "yes") {
await this.upgradePackages();
}
}

const defaultPort = config.project.defaultPort;
const port = await this.getUserInput({
type: "input",
Expand All @@ -386,6 +412,7 @@ export abstract class BasePromptSession {
});
config.project.defaultPort = parseInt(port, 10);
ProjectConfig.setConfig(config);

await this.completeAndRun(config.project.defaultPort);
break;
}
Expand Down
34 changes: 23 additions & 11 deletions packages/ng-schematics/src/ng-new/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import {
apply, chain, empty, MergeStrategy, mergeWith,
move, Rule, schematic, SchematicContext, SchematicsException, Tree
} from "@angular-devkit/schematics";
import {
NodePackageInstallTask,
RepositoryInitializerTask,
RunSchematicTask
} from "@angular-devkit/schematics/tasks";
import { App, GoogleAnalytics, ProjectLibrary, ProjectTemplate, Util } from "@igniteui/cli-core";
apply,
chain,
empty,
MergeStrategy,
mergeWith,
move,
Rule,
schematic,
SchematicContext,
SchematicsException,
TaskId,
Tree } from "@angular-devkit/schematics";
import { NodePackageInstallTask, RepositoryInitializerTask, RunSchematicTask } from "@angular-devkit/schematics/tasks";
import { App, GoogleAnalytics, ProjectLibrary, ProjectTemplate, Util } from "@igniteui/cli-core";
import { defer, Observable } from "rxjs";
import { NewProjectOptions } from "../app-projects/schema";
import { SchematicsPromptSession } from "../prompt/SchematicsPromptSession";
Expand Down Expand Up @@ -150,9 +155,16 @@ export function newProject(options: OptionsSchema): Rule {
]), MergeStrategy.Overwrite
),
(tree: Tree, context: IgxSchematicContext) => {
const installChain = [];
const installChain: TaskId[] = [];
if (prompt.userAnswers && prompt.userAnswers.get("upgradePackages")) {
const upgradeTaskId = context.addTask(
new RunSchematicTask("upgrade-packages", { skipInstall: true }));
installChain.push(upgradeTaskId);
}
if (!options.skipInstall) {
const installTask = context.addTask(new NodePackageInstallTask(options.name));
const installTask = context.addTask(
new NodePackageInstallTask(options.name),
[...installChain]);
installChain.push(installTask);
}
if (!options.skipGit) {
Expand Down
38 changes: 31 additions & 7 deletions packages/ng-schematics/src/ng-new/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("Schematics ng-new", () => {
spyOn(GoogleAnalytics, "post");
});

it("works with no name provided", done => {
fit("works with no name provided", done => {
const runner = new SchematicTestRunner("schematics", collectionPath);
const myTree = Tree.empty();
const workingDirectory = "my-test-project";
Expand All @@ -27,18 +27,24 @@ describe("Schematics ng-new", () => {

const mockSession = {
chooseActionLoop: spyOn(SchematicsPromptSession.prototype, "chooseActionLoop")
.and.returnValue(Promise.resolve(true)),
.and.returnValue(Promise.resolve(true)),
getProjectLibrary: spyOn(SchematicsPromptSession.prototype, "getProjectLibrary")
.and.returnValue((Promise.resolve(mockLibrary))),
.and.returnValue((Promise.resolve(mockLibrary))),
getProjectTemplate: spyOn(SchematicsPromptSession.prototype, "getProjectTemplate")
.and.returnValue(Promise.resolve("empty-page")),
.and.returnValue(Promise.resolve("empty-page")),
getTheme: spyOn(SchematicsPromptSession.prototype, "getTheme")
.and.returnValue(Promise.resolve("custom")),
.and.returnValue(Promise.resolve("custom")),
getUserInput: spyOn(SchematicsPromptSession.prototype, "getUserInput")
.and.returnValue(Promise.resolve(workingDirectory)),
setTree: spyOn(SchematicsPromptSession.prototype, "setContext").and.callThrough()
.and.returnValue(Promise.resolve(workingDirectory))
};

const userAnswers = new Map<string, any>();
userAnswers.set("upgradePackages", true);
Object.defineProperty(SchematicsPromptSession.prototype, "userAnswers", {
get: () => userAnswers,
set: () => void 0
});

spyOn(AppProjectSchematic, "default").and.returnValue((currentTree: Tree, _context: SchematicContext) => {
currentTree.create("gitignore", "");
return currentTree;
Expand All @@ -54,6 +60,11 @@ describe("Schematics ng-new", () => {
expect(e.files.length).toEqual(1);
expect(e.exists(`${workingDirectory}/.gitignore`)).toBeTruthy();
const taskOptions = runner.tasks.map(task => task.options);
const expectedUpgrade = {
collection: null,
name: "upgrade-packages",
options: { skipInstall: true }
};
const expectedInstall: NodePackageTaskOptions = {
command: "install",
quiet: true,
Expand All @@ -75,6 +86,8 @@ describe("Schematics ng-new", () => {
directory: "my-test-project"
}
};
expect(taskOptions.length).toBe(4);
expect(taskOptions).toContain(expectedUpgrade);
expect(taskOptions).toContain(jasmine.objectContaining(expectedInstall));
expect(taskOptions).toContain(expectedInit);
expect(taskOptions).toContain(expectedStart);
Expand All @@ -95,6 +108,10 @@ describe("Schematics ng-new", () => {
.and.returnValue((Promise.resolve(mockLibrary)))
};

const userAnswers = new Map<string, any>();
userAnswers.set("upgradePackages", true);
spyOnProperty(SchematicsPromptSession.prototype, "userAnswers", "get").and.returnValue(userAnswers);

spyOn(AppProjectSchematic, "default").and.returnValue((currentTree: Tree, _context: SchematicContext) => {
currentTree.create("gitignore", "");
return currentTree;
Expand All @@ -110,6 +127,11 @@ describe("Schematics ng-new", () => {
expect(e.files.length).toEqual(1);
expect(e.exists(`${workingDirectory}/.gitignore`)).toBeTruthy();
const taskOptions = runner.tasks.map(task => task.options);
const expectedUpgrade = {
collection: null,
name: "upgrade-packages",
options: { skipInstall: true }
};
const expectedInstall: NodePackageTaskOptions = {
command: "install",
quiet: true,
Expand All @@ -124,6 +146,8 @@ describe("Schematics ng-new", () => {
authorName: undefined,
authorEmail: undefined
};
expect(taskOptions.length).toBe(3);
expect(taskOptions).toContain(expectedUpgrade);
expect(taskOptions).toContain(jasmine.objectContaining(expectedInstall));
expect(taskOptions).toContain(expectedInit);
done();
Expand Down
6 changes: 6 additions & 0 deletions packages/ng-schematics/src/prompt/SchematicsPromptSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class SchematicsPromptSession extends BasePromptSession {
public tree: Tree;
public context: SchematicContext;
public projectName: string;
public userAnswers: Map<string, any>;

constructor(
templateManager: BaseTemplateManager) {
Expand All @@ -23,6 +24,7 @@ export class SchematicsPromptSession extends BasePromptSession {
this.context = context;
this.tree = tree;
this.projectName = projectName;
this.userAnswers = new Map<string, any>();
}

public async getUserInput(options: IUserInputOptions, withBackChoice: boolean = false): Promise<string> {
Expand All @@ -49,6 +51,10 @@ export class SchematicsPromptSession extends BasePromptSession {
// TODO?
}

protected async upgradePackages() {
this.userAnswers.set("upgradePackages", true);
}

protected templateSelectedTask(type: "component" | "view" = "component"): Task<PromptTaskContext> {
return async (_runner, context) => {
if (!context.template) {
Expand Down
6 changes: 5 additions & 1 deletion packages/ng-schematics/src/upgrade-packages/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ describe("Schematics upgrade-packages", () => {
expect(runner.tasks).toEqual([]);

upgradeSpy.and.returnValue(Promise.resolve(true));
await runner.runSchematicAsync(schematicName, { }, appTree).toPromise();
await runner.runSchematicAsync(schematicName, { skipInstall: true }, appTree).toPromise();
expect(upgradeSpy).toHaveBeenCalledTimes(2);
expect(runner.tasks).toEqual([]);

await runner.runSchematicAsync(schematicName, { }, appTree).toPromise();
expect(upgradeSpy).toHaveBeenCalledTimes(3);
const installTaskOptions = new NodePackageInstallTask().toConfiguration();
expect(runner.tasks).toContain(jasmine.objectContaining(installTaskOptions));

Expand Down
8 changes: 6 additions & 2 deletions packages/ng-schematics/src/upgrade-packages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { defer } from "rxjs";
import { SchematicsTemplateManager } from "../SchematicsTemplateManager";
import { setVirtual } from "../utils/NgFileSystem";

export default function(_options: any): Rule {
interface UpgradeOptions {
skipInstall?: boolean;
}

export default function(options: UpgradeOptions): Rule {
return (tree: Tree, context: SchematicContext) => {
App.initialize("angular-cli");
GoogleAnalytics.post({
Expand All @@ -19,7 +23,7 @@ export default function(_options: any): Rule {
setVirtual(tree);
return defer(async () => {
const success = await project.upgradeIgniteUIPackages("", "");
if (success) {
if (success && !options.skipInstall) {
context.addTask(new NodePackageInstallTask());
}
return tree;
Expand Down
14 changes: 14 additions & 0 deletions packages/ng-schematics/src/upgrade-packages/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/schema",
"id": "SchematicsAngularNgNew",
"title": "Ignite UI for Angular Upgrade packages Options Schema",
"type": "object",
"properties": {
"skipInstall": {
"description": "Do not automatically install packages.",
"type": "boolean",
"default": false,
"alias": "si"
}
}
}
Loading

0 comments on commit 22a32dc

Please sign in to comment.