-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathIgniteUIForReactTemplate.ts
More file actions
146 lines (131 loc) · 4.34 KB
/
IgniteUIForReactTemplate.ts
File metadata and controls
146 lines (131 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {
AddTemplateArgs,
App,
ControlExtraConfiguration,
defaultDelimiters,
FS_TOKEN,
IFileSystem,
RouteLike,
ROUTES_VARIABLE_NAME,
Template,
Util
} from "@igniteui/cli-core";
import * as fs from "fs";
import * as path from "path";
import { ReactTypeScriptFileUpdate } from "../../templates/react/ReactTypeScriptFileUpdate";
import { JsxEmit } from "typescript";
export class IgniteUIForReactTemplate implements Template {
public components: string[];
public controlGroup: string;
public listInComponentTemplates: boolean = true;
public listInCustomTemplates: boolean = false;
public id: string;
public name: string;
public description: string;
public dependencies: string[] = [];
public framework: string = "react";
public projectType: string;
public hasExtraConfiguration: boolean = false;
public packages = [];
public delimiters = defaultDelimiters;
// non-standard template prop
protected widget: string;
/**
* Base ReactTemplate constructor
* @param rootPath The template folder path. Pass in `__dirname`
*/
constructor(private rootPath: string) { }
public get templatePaths(): string[] {
return [path.join(this.rootPath, "files")];
}
public generateConfig(name: string, options: {}): {[key: string]: any} {
let config = {};
if (options["extraConfig"]) {
config = options["extraConfig"];
}
config["path"] = this.folderName(name); //folder name allowed spaces, any casing
config["name"] = Util.nameFromPath(name); // this name should not have restrictions
config["ClassName"] = Util.className(Util.nameFromPath(name)); //first letter capital, no spaces and no dashes,
config["filePrefix"] = Util.lowerDashed(Util.nameFromPath(name));
config["cliVersion"] = Util.version();
if (this.widget) {
config["widget"] = this.widget;
config["Control"] = Util.className(this.widget);
}
if (this.description) {
config["description"] = this.description;
}
return config;
}
public registerInProject(projectPath: string, name: string, options?: AddTemplateArgs, defaultPath = false) {
if (!options.parentName) {
options.parentName = "app";
}
if (!options.parentRoutingModulePath) {
options.parentRoutingModulePath = "src/app/app-routes.tsx";
}
const routeModulePath: string = options.parentRoutingModulePath;
if (!(options && options.skipRoute)
&& App.container.get<IFileSystem>(FS_TOKEN).fileExists(routeModulePath)) {
const routingModule = new ReactTypeScriptFileUpdate(
path.join(projectPath, routeModulePath),
{ convertTabsToSpaces: false, indentSize: 2, singleQuotes: true },
{ jsx: JsxEmit.Preserve }
);
const modulePath = `./${Util.lowerDashed(name)}/${Util.lowerDashed(name)}-routes`;
if (defaultPath) {
routingModule.addRoute({
index: true,
redirectTo: options.path,
}
);
}
routingModule.addRoute({
path: this.fileName(name),
element: Util.className(name),
text: Util.nameFromPath(name)
},
false // multiline
);
if (options.hasChildren) {
const child: RouteLike = {
identifierName: ROUTES_VARIABLE_NAME,
aliasName: options.routerChildren,
modulePath
};
routingModule.addChildRoute(this.fileName(name), child, true);
}
routingModule.finalize();
}
}
public getExtraConfiguration(): ControlExtraConfiguration[] {
throw new Error("Method not implemented.");
}
public setExtraConfiguration(extraConfigKeys: {}) {
throw new Error("Method not implemented.");
}
protected folderName(pathName: string): string {
//TODO: should remove the spaces
const parts = path.parse(pathName);
let folderName = pathName;
if (parts.dir) {
folderName = path.join(parts.dir, parts.name);
folderName = folderName.replace(/\\/g, "/");
// TODO: config-based "src/app"?
const relative = path.join(process.cwd(), "src/views", folderName);
// path.join will also resolve any '..' segments
// so if relative result doesn't start with CWD it's out of project root
if (!relative.startsWith(process.cwd())) {
Util.error(`Path ${"src/views/" + folderName} is not valid!`, "red");
process.exit(1);
}
//clean up potential leading spaces in folder names (`path/ name`):
folderName = folderName.replace(/\/\s+/g, "/");
}
return Util.lowerDashed(folderName);
}
protected fileName(pathName: string): string {
const name = Util.nameFromPath(pathName);
return Util.lowerDashed(name);
}
}