Skip to content

Commit

Permalink
Merge branch 'IGanchev/fix-grid-summaries-template' of https://github…
Browse files Browse the repository at this point in the history
….com/IgniteUI/igniteui-cli into IGanchev/fix-grid-summaries-template
  • Loading branch information
IvayloG committed Dec 13, 2018
2 parents 4d052b2 + 4d8ae71 commit 988582e
Show file tree
Hide file tree
Showing 13 changed files with 422 additions and 258 deletions.
162 changes: 111 additions & 51 deletions lib/PromptSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Util } from "./Util";

export class PromptSession {
private WIZARD_BACK_OPTION = "Back";
private config: Config;

constructor(private templateManager: TemplateManager) { }

Expand All @@ -28,6 +29,7 @@ export class PromptSession {
return retProm;
}
}

/**
* Start questions session for project creation
*/
Expand All @@ -40,12 +42,12 @@ export class PromptSession {
let projLibrary: ProjectLibrary;
let theme: string;
add.templateManager = this.templateManager;
const config = ProjectConfig.getConfig();
this.config = ProjectConfig.getConfig();
const defaultProjName = "IG Project";

if (ProjectConfig.hasLocalConfig() && !config.project.isShowcase) {
projLibrary = this.templateManager.getProjectLibrary(config.project.framework, config.project.projectType);
theme = config.project.theme;
if (ProjectConfig.hasLocalConfig() && !this.config.project.isShowcase) {
projLibrary = this.templateManager.getProjectLibrary(this.config.project.framework, this.config.project.projectType);
theme = this.config.project.theme;
} else {
Util.log(""); /* new line */

Expand All @@ -69,7 +71,7 @@ export class PromptSession {
type: "list",
name: "framework",
message: "Choose framework:",
choices: this.templateManager.getFrameworkNames(),
choices: this.getFrameworkNames(),
default: "jQuery"
});

Expand All @@ -85,7 +87,7 @@ export class PromptSession {
await projTemplate.generateFiles(process.cwd(), projectName, theme);

Util.log(Util.greenCheck() + " Project structure generated.");
if (!config.skipGit) {
if (!this.config.skipGit) {
Util.gitInit(process.cwd(), projectName);
}
// move cwd to project folder
Expand Down Expand Up @@ -273,25 +275,22 @@ export class PromptSession {
let selectedTemplate: Template;
const templates: Template[] = component.templates;
const config = ProjectConfig.getConfig();
if (templates.length === 1) {
//get the only one template
selectedTemplate = templates[0];
} else {
const templateRes = await this.getUserInput({
type: "list",
name: "template",
message: "Choose one:",
choices: this.formatOutput(templates)
}, true);

if (templateRes === this.WIZARD_BACK_OPTION) {
return false;
}
const templateRes = await this.getUserInput({
type: "list",
name: "template",
message: "Choose one:",
choices: this.formatOutput(templates)
}, true);

selectedTemplate = templates.find((value, i, obj) => {
return value.name === templateRes;
});
if (templateRes === this.WIZARD_BACK_OPTION) {
return false;
}

selectedTemplate = templates.find((value, i, obj) => {
return value.name === templateRes;
});

if (selectedTemplate) {
let success = false;
const availableDefaultName = Util.getAvailableName(selectedTemplate.name, false,
Expand Down Expand Up @@ -366,13 +365,20 @@ export class PromptSession {
}

/**
* Gets the user input according to provided @param options.
* If @param withBackChoice is set to true adds Back option to the list
* Gets the user input according to provided `options`.Returns directly if single choice is provided.
* @param options to use for the user input
* @param withBackChoice determines whether or not Back option should be added
* @param withBackChoice Add a "Back" option to choices list
*/
private async getUserInput(options: IUserInputOptions, withBackChoice: boolean = false): Promise<string> {

if (options.choices) {
if (options.choices.length < 2) {
// single choice to return:
let choice = options.choices[0];
choice = choice.value || choice;
this.logAutoSelected(options, choice);
return choice;
}
if (withBackChoice) {
options.choices.push(this.WIZARD_BACK_OPTION);
}
Expand Down Expand Up @@ -409,6 +415,28 @@ export class PromptSession {
return result;
}

private logAutoSelected(options: IUserInputOptions, choice: any) {
let text;
switch (options.name) {
case "framework":
text = ` Framework`;
break;
case "projectType":
text = ` Project type`;
break;
default:
return;
//TODO: text = ` ${options.name}`;
}
GoogleAnalytics.post({
t: "event",
ec: "$ig wizard",
el: options.message,
ea: `${options.name}: ${choice}`
});
Util.log(`${text}: ${choice}`);
}

/**
* Check if provided @param name is valid for project name
* @param name the name to check
Expand All @@ -435,32 +463,69 @@ export class PromptSession {
*/
private async getProjectLibrary(framework: Framework): Promise<ProjectLibrary> {
let projectLibrary: ProjectLibrary;
if (framework.projectLibraries.length > 1) {
const projectRes = await this.getUserInput({
type: "list",
name: "projectType",
message: "Choose the type of project:",
choices: this.templateManager.getProjectLibraryNames(framework.id)
const projectLibraries = this.getProjectLibNames(framework);

const projectRes = await this.getUserInput({
type: "list",
name: "projectType",
message: "Choose the type of project:",
choices: projectLibraries
});
projectLibrary = this.templateManager.getProjectLibraryByName(framework, projectRes);

return projectLibrary;
}

/** Returns the framework names, potentially filtered by config */
private getFrameworkNames(): string[] {
let frameworksNames: string[] = [];
if (
this.config.stepByStep &&
this.config.stepByStep.frameworks &&
this.config.stepByStep.frameworks.length
) {
this.config.stepByStep.frameworks.forEach(x => {
const framework = this.templateManager.getFrameworkById(x);
if (framework) {
frameworksNames.push(framework.name);
}
});
}
if (!frameworksNames.length) {
// no config or wrong projTypes array:
frameworksNames = this.templateManager.getFrameworkNames();
}
return frameworksNames;
}

projectLibrary = this.templateManager.getProjectLibraryByName(framework, projectRes);
} else {
projectLibrary = this.templateManager.getProjectLibrary(framework.id);
/** Returns the projectLibraries names, potentially filtered by config */
private getProjectLibNames(framework: Framework): string[] {
let projectLibraries: string[] = [];
const frameworkConfig = this.config.stepByStep && this.config.stepByStep[framework.id as FrameworkId];

if (frameworkConfig && frameworkConfig.projTypes && frameworkConfig.projTypes.length) {
frameworkConfig.projTypes.forEach(x => {
const projLib = framework.projectLibraries.find(p => p.projectType === x);
if (projLib) {
projectLibraries.push(projLib.name);
}
});
}

return projectLibrary;
if (!projectLibraries.length) {
// no config or wrong projTypes array:
projectLibraries = this.templateManager.getProjectLibraryNames(framework.id);
}
return projectLibraries;
}

/**
* Gets project template from the user input, or default if provided @param projectLibrary has a single template
* Gets project template from user input, or default if provided `projectLibrary` has a single template
* @param projectLibrary to get theme for
*/
private async getProjectTemplate(projectLibrary: ProjectLibrary): Promise<ProjectTemplate> {
let projTemplate: ProjectTemplate;

if (projectLibrary.projectIds.length < 2) {
return projectLibrary.getProject(projectLibrary.projectIds[0]);
}
const componentNameRes = await this.getUserInput({
type: "list",
name: "projTemplate",
Expand All @@ -478,18 +543,13 @@ export class PromptSession {
*/
private async getTheme(projectLibrary: ProjectLibrary): Promise<string> {
let theme: string;
if (projectLibrary.themes.length < 2) {
theme = projectLibrary.themes[0] || "";
} else {
theme = await this.getUserInput({
type: "list",
name: "theme",
message: "Choose the theme for the project:",
choices: projectLibrary.themes,
default: projectLibrary.themes[0]
});
}

theme = await this.getUserInput({
type: "list",
name: "theme",
message: "Choose the theme for the project:",
choices: projectLibrary.themes,
default: projectLibrary.themes[0]
});
return theme;
}

Expand Down
8 changes: 7 additions & 1 deletion lib/config/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@
"igPackageRegistry": "https://packages.infragistics.com/npm/js-licensed/",
"customTemplates": [],
"skipGit": false,
"skipAnalytic": false
"skipAnalytic": false,
"stepByStep": {
"frameworks": ["angular"],
"angular": {
"projTypes": ["igx-ts"]
}
}
}
7 changes: 5 additions & 2 deletions lib/templates/IgniteUIForAngularTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ export class IgniteUIForAngularTemplate extends AngularTemplate {
for (const dep of this.dependencies) {
if (dep.from && dep.from.startsWith(".")) {
// relative file dependency
dep.from = TsUtils.relativePath(mainModulePath, path.join(projectPath, dep.from), true, true);
const copy = Object.assign({}, dep);
copy.from = TsUtils.relativePath(mainModulePath, path.join(projectPath, copy.from), true, true);
mainModule.addNgModuleMeta(copy, this.getBaseVariables(name));
} else {
mainModule.addNgModuleMeta(dep, this.getBaseVariables(name));
}
mainModule.addNgModuleMeta(dep, this.getBaseVariables(name));
}
mainModule.finalize();

Expand Down
21 changes: 20 additions & 1 deletion lib/types/Config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ declare interface Config {
* and server configuration of the project */
//"projectBuild": "tsc",
//"serverType": "webpack"
}
},
/** Step by step mode configuration */
stepByStep?: {
/** Frameworks to list in Step by Step mode */
frameworks: string[];
[FrameworkId.angular]: StepFrameworkConfig;
[FrameworkId.react]: StepFrameworkConfig;
[FrameworkId.jquery]: StepFrameworkConfig;
},

/** An array of paths to read custom templates from */
customTemplates: string[];
Expand All @@ -53,3 +61,14 @@ declare interface Config {
*/
disableAnalytics: boolean;
}

declare enum FrameworkId {
angular = "angular",
react = "react",
jquery = "jquery"
}

declare interface StepFrameworkConfig {
/** Project types to list in Step by Step */
projTypes: string[];
}
Loading

0 comments on commit 988582e

Please sign in to comment.