Skip to content

Commit

Permalink
feat(projects): Make templates selectable in step-by-step
Browse files Browse the repository at this point in the history
  • Loading branch information
damyanpetev committed Oct 15, 2018
1 parent f42c8fa commit cd98bae
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 23 deletions.
29 changes: 20 additions & 9 deletions lib/BaseProjectLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class BaseProjectLibrary implements ProjectLibrary {
/** Implementation, not part of the interface */
public groupDescriptions = new Map<string, string>();

/** Used to prefix folders that don't contain usable templates */
protected _ignorePrefix = "_";
protected _projectsPath: string = "projects";
protected _customTemplatesPath: string = "custom-templates";
protected _generateCommandPath: string = "generate";
Expand All @@ -24,11 +26,20 @@ export class BaseProjectLibrary implements ProjectLibrary {
list = list.concat(this.customTemplates);
return list;
}
private _projects: string[] = [];
public get projects(): string[] {
private _projectIds: string[] = [];
public get projectIds(): string[] {
//read projects list
if (!this._projectIds.length) {
this._projectIds = Util.getDirectoryNames(path.join(this.rootPath, this._projectsPath));
this._projectIds = this._projectIds.filter(x => !x.startsWith(this._ignorePrefix));
}
return this._projectIds;
}

private _projects: ProjectTemplate[] = [];
public get projects(): ProjectTemplate[] {
if (!this._projects.length) {
this._projects = Util.getDirectoryNames(path.join(this.rootPath, this._projectsPath));
this._projects = this.projectIds.map(x => this.getProject(x));
}
return this._projects;
}
Expand Down Expand Up @@ -183,18 +194,18 @@ export class BaseProjectLibrary implements ProjectLibrary {

/**
* Get project template
* @param name Optional name of the project template. Defaults to "empty"
* @param id ID of the project template.
*/
public getProject(name: string = "empty"): ProjectTemplate {
if (this.hasProject(name)) {
const projModule = require(path.join(this.rootPath, this._projectsPath, name));
public getProject(id: string): ProjectTemplate {
if (this.hasProject(id)) {
const projModule = require(path.join(this.rootPath, this._projectsPath, id));
return projModule.default || projModule as ProjectTemplate;
}
return null;
}

public hasProject(name: string): boolean {
return this.projects.indexOf(name) > -1;
public hasProject(id: string): boolean {
return this.projectIds.indexOf(id) > -1;
}
//abstraction for projects

Expand Down
25 changes: 24 additions & 1 deletion lib/PromptSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ export class PromptSession {
const framework = this.templateManager.getFrameworkByName(frameRes);
//app name validation???
projLibrary = await this.getProjectLibrary(framework);

const projTemplate = await this.getProjectTemplate(projLibrary);
// project options:
theme = await this.getTheme(projLibrary);

const projTemplate = projLibrary.getProject();
Util.log(" Generating project structure.");
await projTemplate.generateFiles(process.cwd(), projectName, theme);

Expand Down Expand Up @@ -445,6 +447,27 @@ export class PromptSession {
return projectLibrary;
}

/**
* Gets the them from the user input, or default if provided @param projectLibrary has single theme
* @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({
choices: this.formatOutput(projectLibrary.projects),
message: "Choose project template:",
name: "projTemplate",
type: "list"
});
projTemplate = projectLibrary.projects.find(x => x.name === componentNameRes);

return projTemplate;
}

/**
* Gets the them from the user input, or default if provided @param projectLibrary has single theme
* @param projectLibrary to get theme for
Expand Down
7 changes: 4 additions & 3 deletions lib/types/ProjectLibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ interface ProjectLibrary {
themes: string[];
/** Collection of component which the framework supports e.g. igGrid, igTextEditor */
components: Component[];
/**List of supported projects as templates (Showcases()) e.g. Empty, Showcase1, Showcase 2 */
//TODO This should be project Types?
projects: string[];
/**List of supported projects as template Ids (Showcases()) e.g. Empty, Showcase1, Showcase 2 */
projectIds: string[];
/** List of project template instances */
projects: ProjectTemplate[];
/**Collection of custom templates provided by the framework */
templates: Template[];
/** JS, TS, Dart */
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/BaseProjectLibrary-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("Unit - Base project library ", () => {

const library = new BaseProjectLibrary("../test");
expect(library.hasProject("angular")).toBe(true);
expect(library.projects).toEqual(mockProjects);
expect(library.projectIds).toEqual(mockProjects);
expect(Util.getDirectoryNames).toHaveBeenCalledWith(path.join("../test", "projects"));
done();
});
Expand Down
2 changes: 1 addition & 1 deletion templates/angular/igx-ts/projects/_base/files/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# $(name)

This project was generated with [IgniteUI CLI](https://github.com/IgniteUI/igniteui-cli) version $(cliVersion).
This project was generated with [Ignite UI CLI](https://github.com/IgniteUI/igniteui-cli) version $(cliVersion).

## Development server

Expand Down
4 changes: 1 addition & 3 deletions templates/angular/igx-ts/projects/_base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from "path";
import { Util } from "../../../../../lib/Util";

export class BaseIgxProject implements ProjectTemplate {
public id: string = "angular";
public id: string = "base";
public name = "base";
public description = "Empty project layout structure for Ignite UI for Angular";
public dependencies: string[] = [];
Expand Down Expand Up @@ -60,5 +60,3 @@ $app-palette: igx-palette($primary, $secondary);
return Util.processTemplates(path.join(__dirname, "./files"), path.join(outputPath, name), config, pathsConfig);
}
}
// TODO: Don't export usable instance for base?
export default new BaseIgxProject();
10 changes: 5 additions & 5 deletions templates/angular/igx-ts/projects/side-nav/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as path from "path";
import { Util } from "../../../../../lib/Util";
import { BaseIgxProject } from "../base";
import { BaseIgxProject } from "../_base";

class SideNavProject extends BaseIgxProject implements ProjectTemplate {
public id: string = "angular";
public name = "empty";
public description = "Default empty angular project structure for Ignite UI for Angular";
export class SideNavProject extends BaseIgxProject implements ProjectTemplate {
public id: string = "side-nav";
public name = "Default side navigation";
public description = "Project structure with side navigation drawer";
public dependencies: string[] = [];
public framework: string = "angular";
public projectType: string = "igx-ts";
Expand Down

0 comments on commit cd98bae

Please sign in to comment.