Skip to content

Commit

Permalink
Merge pull request #105 from IgniteUI/IGanchev/step-by
Browse files Browse the repository at this point in the history
IGanchev/step by
  • Loading branch information
bazal4o authored Jan 2, 2018
2 parents 8155da0 + 765ec73 commit 17e0034
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 25 deletions.
2 changes: 0 additions & 2 deletions lib/PromptSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export class PromptSession {

// tslint:disable:object-literal-sort-keys
if (config != null && !config.project.isShowcase) {
//throw new Error("Add command is supported only on existing project created with igntie-ui-cli");
projLibrary = this.templateManager.getProjectLibrary(config.project.framework, config.project.projectType);
await this.chooseActionLoop(projLibrary, config.project.theme);
} else {
Expand Down Expand Up @@ -84,7 +83,6 @@ export class PromptSession {
//TODO: restore cwd?
}
}

/**
* Starts a loop of 'Choose an action' questions
* @param framework The framework to use
Expand Down
7 changes: 6 additions & 1 deletion lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ export async function run(args = null) {
Util.log("quickstart Created");
break;
case "add":
await add.execute(argv);
if (add.check(argv)) {
await add.execute(argv);
} else {
yargsModule.showHelp();
return;
}
break;
case "build":
await build.execute(argv);
Expand Down
21 changes: 18 additions & 3 deletions lib/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ProjectConfig } from "../ProjectConfig";
import { TemplateManager } from "../TemplateManager";
import { Util } from "../Util";
import { PackageManager } from "./../packages/PackageManager";
import { PromptSession } from "./../PromptSession";

let command: {
[name: string]: any,
Expand All @@ -11,21 +12,29 @@ let command: {
};
// tslint:disable:object-literal-sort-keys
command = {
command: "add <template> <name>",
command: "add [template] [name]",
desc: "Add component by it ID and providing a name.",
templateManager: null,
builder: {
template: {
alias: "t",
description: `Template ID, such as "grid", "combo", etc.`,
type: "string"
type: "string",
global: true
},
name: {
alias: "n",
description: "File name.",
type: "string"
type: "string",
global: true
}
},
check: argv => {
if ((!argv.name && argv.template) || (argv.name && !argv.template)) {
return false;
}
return true;
},
async execute(argv) {
//command.template;
const config = ProjectConfig.getConfig();
Expand All @@ -47,6 +56,12 @@ command = {
config.project.projectType
) as ProjectLibrary;

if (!argv.template && !argv.name) {
const prompts = new PromptSession(command.templateManager);
await prompts.chooseActionLoop(frameworkLibrary, config.project.theme);
return;
}

if (!frameworkLibrary.hasTemplate(argv.template)) {
Util.error("Template doesn't exist in the current library", "red");
return;
Expand Down
15 changes: 12 additions & 3 deletions lib/commands/new.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as fs from "fs-extra";
import * as path from "path";
import { ProjectConfig } from "../ProjectConfig";
import { Util } from "../Util";
import { PromptSession } from "./../PromptSession";
import { TemplateManager } from "./../TemplateManager";

let command: {
Expand All @@ -10,12 +12,11 @@ let command: {
};
// tslint:disable:object-literal-sort-keys
command = {
command: "new <name>",
command: "new [name]",
desc: "Creating a new project",
builder: {
name: {
alias: "n",
default: "app",
describe: "Project name",
type: "string"
},
Expand All @@ -39,8 +40,16 @@ command = {
},
template: null,
async execute(argv) {
if (ProjectConfig.getConfig() !== null) {
return Util.error("There is already an existing project.", "red");
}
if (!argv.name && !argv.type && !argv.theme) {
const prompts = new PromptSession(command.template);
await prompts.start();
return;
}
if (command.template.getFrameworkById(argv.framework) === undefined) {
return Util.error("Framework not supported");
return Util.error("Framework not supported", "red");
}
let projectLib: ProjectLibrary;
if (argv.type) {
Expand Down
19 changes: 7 additions & 12 deletions spec/acceptance/add-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from "fs-extra";
import { parse } from "path";
import cli = require("../../lib/cli");
import { ProjectConfig } from "../../lib/ProjectConfig";
import { PromptSession } from "./../../lib/PromptSession";

describe("Add command", () => {
let testFolder = parse(__filename).name;
Expand All @@ -25,18 +26,6 @@ describe("Add command", () => {
fs.rmdirSync(`./output/${testFolder}`);
});

it("Should show help with missing arg", async done => {

const child = spawnSync("node", ["../../bin/execute.js", "add", "grid" ], {
encoding: "utf-8"
});
//await cli.run(["add", "grid"]);
expect(child.stderr.toString("utf-8")).toMatch(/add\s+<template>\s+<name>/);
expect(child.stdout.length).toEqual(0);

done();
});

it("Should not work without a project", async done => {
await cli.run(["add", "grid", "name"]);

Expand All @@ -45,6 +34,12 @@ describe("Add command", () => {
);
expect(console.log).toHaveBeenCalledTimes(0);

await cli.run(["add"]);
expect(console.error).toHaveBeenCalledWith(
jasmine.stringMatching(/Add command is supported only on existing project created with igniteui-cli\s*/)
);
expect(console.log).toHaveBeenCalledTimes(0);

done();
});

Expand Down
6 changes: 3 additions & 3 deletions spec/acceptance/help-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ describe("Help command", () => {
const originalHelpText: string = `Commands:
quickstart A quick start for your project
start start the project
new <name> Creating a new project
new [name] Creating a new project
build build the project
test test the project
add <template> <name> Add component by it ID and providing a name.
add [template] [name] Add component by it ID and providing a name.
Options:
--help Show help [boolean]`;

Expand All @@ -28,7 +28,7 @@ describe("Help command", () => {
});
const originalNewHelpText: string = `Options:
--help Show help [boolean]
--name, -n Project name [string] [default: "app"]
--name, -n Project name [string]
--framework, -f Framework to setup project for
[string] [choices: "angular", "jquery", "react"] [default: "jquery"]
--type, -t Project type (depends on framework) [string]
Expand Down
28 changes: 28 additions & 0 deletions spec/unit/add-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

import * as fs from "fs-extra";
import { default as addCmd } from "../../lib/commands/add";
import { ProjectConfig } from "../../lib/ProjectConfig";
import { PromptSession } from "../../lib/PromptSession";
import { Util } from "../../lib/Util";

describe("Unit - Add command", () => {

it("Should start prompt session with missing arg", async done => {
spyOn(ProjectConfig, "getConfig").and.returnValue({ project: {
framework: "angular",
theme: "infragistics"}});

const mockProjLib = {};
addCmd.templateManager = jasmine.createSpyObj("TemplateManager", {
getFrameworkById: {},
getProjectLibrary: mockProjLib
});

const promptSession = PromptSession.prototype;
spyOn(promptSession, "chooseActionLoop");

await addCmd.execute({});
expect(promptSession.chooseActionLoop).toHaveBeenCalledWith(mockProjLib, "infragistics");
done();
});
});
31 changes: 30 additions & 1 deletion spec/unit/new-spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { default as newCmd } from "../../lib/commands/new";
import { ProjectConfig } from "../../lib/ProjectConfig";
import { PromptSession } from "../../lib/PromptSession";
import { Util } from "../../lib/Util";
describe("Unit - New command", () => {

beforeEach(() => {
spyOn(Util, "log");
});

it("New command in existing project", async done => {
spyOn(Util, "error");
spyOn(ProjectConfig, "getConfig").and.returnValue({});

await newCmd.execute({});
expect(Util.error).toHaveBeenCalledWith("There is already an existing project.", "red");
done();
});

it("Logs error for wrong framework", async done => {
spyOn(Util, "error");
//spied getFrameworkById won't return anything, i.e. not found
Expand All @@ -14,7 +25,7 @@ describe("Unit - New command", () => {
await newCmd.execute({ name: "Test", framework: "jq"});

expect(newCmd.template.getFrameworkById).toHaveBeenCalledWith("jq");
expect(Util.error).toHaveBeenCalledWith("Framework not supported");
expect(Util.error).toHaveBeenCalledWith("Framework not supported", "red");
//no further attempts to get project:
expect(newCmd.template.getProjectLibrary).toHaveBeenCalledTimes(0);
expect(Util.log).toHaveBeenCalledTimes(0);
Expand Down Expand Up @@ -63,6 +74,24 @@ describe("Unit - New command", () => {
done();
});

it("Should start prompt session with missing arg", async done => {
spyOn(ProjectConfig, "getConfig").and.returnValue(null);

const mockProjLib = {};
newCmd.template = jasmine.createSpyObj("TemplateManager", {
getFrameworkById: {},
getProjectLibrary: mockProjLib
});

const promptSession = PromptSession.prototype;
spyOn(promptSession, "start");
//spied getFrameworkById won't return anything, i.e. not found

await newCmd.execute({});
expect(promptSession.start).toHaveBeenCalled();
done();
});

it("Logs error for unavailable project", async done => {
spyOn(Util, "error");

Expand Down

0 comments on commit 17e0034

Please sign in to comment.