Skip to content

Commit 0b842d3

Browse files
committed
Add a style-generator
1 parent 0f10d55 commit 0b842d3

File tree

4 files changed

+218
-1
lines changed

4 files changed

+218
-1
lines changed

generators/style/index.ts

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
import * as Path from "path";
2+
import * as Generator from "yeoman-generator";
3+
import chalk from "chalk";
4+
import yosay = require("yosay");
5+
import { isNullOrUndefined } from "util";
6+
7+
/**
8+
* Provides the functionality to generate WSC-styles.
9+
*/
10+
class WSCStyleGenerator extends Generator
11+
{
12+
/**
13+
* The options provided by the user.
14+
*/
15+
private settings: { [key: string]: any };
16+
17+
/**
18+
* Validates whether the a value is provided.
19+
*
20+
* @param {string} value
21+
* The value that is to be validated.
22+
*
23+
* @param {Generator.Answers} answers
24+
* The answers provided by the user.
25+
*/
26+
private forceInput = (value: string, answers?: Generator.Answers): boolean | string =>
27+
{
28+
if (value.length > 0)
29+
{
30+
return true;
31+
}
32+
else
33+
{
34+
return "Please enter a valid input!";
35+
}
36+
}
37+
38+
/**
39+
* Initializes a new instance of the `Generator` class.
40+
*
41+
* @param args
42+
* A set of arguments.
43+
*
44+
* @param opts
45+
* A set of options.
46+
*/
47+
constructor(args, opts)
48+
{
49+
super(args, opts);
50+
}
51+
52+
/**
53+
* Collects all informations about the style that is to be created.
54+
*/
55+
prompting()
56+
{
57+
this.log(yosay(`Welcome to the ${chalk.whiteBright("WoltLab Suite Core Style")} generator!`));
58+
59+
let prompts: Generator.Questions = [
60+
{
61+
type: "input",
62+
name: "stylesPath",
63+
message: "Where do you want to store your styles?",
64+
default: "styles",
65+
when: (answers: Generator.Answers) =>
66+
{
67+
if (isNullOrUndefined(this.config.get("stylesPath")))
68+
{
69+
return true;
70+
}
71+
else
72+
{
73+
answers["stylesPath"] = this.config.get("stylesPath");
74+
}
75+
}
76+
},
77+
{
78+
type: "input",
79+
name: "name",
80+
message: "What's the name of your style?",
81+
validate: this.forceInput
82+
},
83+
{
84+
type: "input",
85+
name: "displayName",
86+
message: "What's the display-name of your style?",
87+
default: (answers: Generator.Answers) =>
88+
{
89+
return answers["name"];
90+
},
91+
validate: this.forceInput
92+
},
93+
{
94+
type: "input",
95+
name: "description",
96+
message: "Please enter a description:"
97+
},
98+
{
99+
type: "checkbox",
100+
name: "components",
101+
message: "What do you want to provide?",
102+
choices: [
103+
{
104+
name: "Custom SCSS-Styles",
105+
value: "customStyles"
106+
},
107+
{
108+
name: "Variable-Overrides",
109+
value: "variableOverrides"
110+
}
111+
]
112+
},
113+
{
114+
type: "input",
115+
name: "componentPaths.customStyles",
116+
message: "Where do you want to store the custom SCSS-styles?",
117+
default: "styles.scss",
118+
when: (answers: Generator.Answers) =>
119+
{
120+
return (answers["components"] as string[]).includes("customStyles");
121+
}
122+
},
123+
{
124+
type: "input",
125+
name: "componentPaths.variableOverrides",
126+
message: "Where do you want to store the variable-overrides?",
127+
default: "override.scss",
128+
when: (answers: Generator.Answers) =>
129+
{
130+
return (answers["components"] as string[]).includes("variableOverrides");
131+
}
132+
}
133+
];
134+
135+
return this.prompt(prompts).then(answers =>
136+
{
137+
this.settings = answers;
138+
});
139+
}
140+
141+
/**
142+
* Writes the templates
143+
*/
144+
writing()
145+
{
146+
let basePath: string = Path.relative(Path.join(this.settings["stylesPath"], this.settings["name"]), ".");
147+
basePath = basePath.replace("\\", "/");
148+
149+
if (basePath.length === 0)
150+
{
151+
basePath = ".";
152+
}
153+
154+
this.settings["basePath"] = basePath + "/";
155+
this.fs.copyTpl(this.templatePath("Style.ts"), this.destinationPath(this.settings["stylesPath"], this.settings["name"], "Style.ts"), this.settings);
156+
157+
for (let component of (this.settings["components"] as string[]))
158+
{
159+
switch (component)
160+
{
161+
case "customStyles":
162+
this.fs.copyTpl(
163+
this.templatePath("blank.scss"),
164+
this.destinationPath(this.settings["stylesPath"], this.settings["name"], this.settings["componentPaths"][component]),
165+
this.settings);
166+
break;
167+
case "variableOverrides":
168+
this.fs.copyTpl(
169+
this.templatePath("blank.scss"),
170+
this.destinationPath(this.settings["stylesPath"], this.settings["name"], this.settings["componentPaths"][component]),
171+
this.settings);
172+
break;
173+
174+
}
175+
}
176+
}
177+
178+
/**
179+
* Installs the dependencies.
180+
*/
181+
finalize()
182+
{
183+
this.config.set("stylesPath", this.settings["stylesPath"]);
184+
this.config.save();
185+
186+
this.log();
187+
this.log("Your package \"" + this.settings["name"] + "\" has been created!");
188+
this.log();
189+
this.log(
190+
"Please keep in mind to add your styles-folder to the package" +
191+
"by adding \"...new StyleInstructionCollection(\"" + this.settings["stylesPath"] + "\"" +
192+
"to the Install- or Update instruction-collection.");
193+
}
194+
}
195+
196+
export = WSCStyleGenerator;

generators/style/templates/Style.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Style from "<%= basePath %>lib/Customization/Style";
2+
3+
let style: Style = new Style({
4+
Name: "<%= name %>",
5+
DisplayName: {
6+
en: "<%= displayName %>"
7+
},
8+
Description: {
9+
en: "<%= description %>"
10+
}<%
11+
%><% if (components.includes("customStyles")) { %><%
12+
%>,
13+
CustomScssFile: "<%= componentPaths["customStyles"] %>"<%
14+
%><% } %><%
15+
%><% if (components.includes("variableOverrides")) { %><%
16+
%>,
17+
OverrideScssFile: "<%= componentPaths["variableOverrides"] %>"<%
18+
%><% } %>
19+
});
20+
21+
export = style;

generators/style/templates/blank.scss

Whitespace-only changes.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)