Skip to content

Commit 17e4511

Browse files
committedMay 28, 2019
misc(generators): refactor utils
1 parent 0a648f7 commit 17e4511

File tree

5 files changed

+246
-58
lines changed

5 files changed

+246
-58
lines changed
 

‎packages/generators/utils/entry.ts

+51-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Generator from "yeoman-generator";
2-
import { InputValidate } from "@webpack-cli/webpack-scaffold";
2+
import { Input, InputValidate } from "@webpack-cli/webpack-scaffold";
33

44
import validate from "./validate";
55

@@ -16,22 +16,18 @@ interface CustomGenerator extends Generator {
1616
* @returns {Object} An Object that holds the answers given by the user, later used to scaffold
1717
*/
1818

19-
export default function entry(
20-
self: CustomGenerator,
21-
answer: {
22-
entryType: boolean;
23-
}
24-
): Promise<void | {}> {
19+
export default function entry(self: CustomGenerator, multiEntries: boolean): Promise<{}> {
2520
let entryIdentifiers: string[];
26-
let result: Promise<void | {}>;
27-
if (answer.entryType) {
21+
let result: Promise<{}>;
22+
if (multiEntries) {
2823
result = self
2924
.prompt([
3025
InputValidate(
3126
"multipleEntries",
32-
"Type the names you want for your modules (entry files), separated by comma [example: app,vendor]",
33-
validate
34-
)
27+
"Type the names you want for your modules (entry files) separated by comma",
28+
validate,
29+
"pageOne, pageTwo",
30+
),
3531
])
3632
.then(
3733
(multipleEntriesAnswer: { multipleEntries: string }): Promise<void | {}> => {
@@ -91,27 +87,56 @@ export default function entry(
9187
!entryPropAnswer[val].includes("path") &&
9288
!entryPropAnswer[val].includes("process")
9389
) {
94-
entryPropAnswer[val] = `\'${entryPropAnswer[val].replace(/"|'/g, "")}\'`;
90+
n[val] = `\'./${n[val].replace(/"|'/g, "").concat(".js")}\'`;
9591
}
96-
webpackEntryPoint[val] = entryPropAnswer[val];
97-
}
98-
);
99-
return webpackEntryPoint;
92+
webpackEntryPoint[val] = n[val];
93+
});
94+
} else {
95+
n = {};
96+
}
97+
return fn(trimmedProp);
98+
});
99+
}, Promise.resolve());
100+
}
101+
return forEachPromise(entryIdentifiers, (entryProp: string): Promise<{} | void> =>
102+
self.prompt([
103+
InputValidate(
104+
`${entryProp}`,
105+
`What is the location of "${entryProp}"?`,
106+
validate,
107+
`./src/${entryProp}`,
108+
),
109+
]),
110+
).then((entryPropAnswer: object): object => {
111+
Object.keys(entryPropAnswer).forEach((val: string): void => {
112+
if (
113+
entryPropAnswer[val].charAt(0) !== "(" &&
114+
entryPropAnswer[val].charAt(0) !== "[" &&
115+
!entryPropAnswer[val].includes("function") &&
116+
!entryPropAnswer[val].includes("path") &&
117+
!entryPropAnswer[val].includes("process")
118+
) {
119+
entryPropAnswer[val] = `\'./${entryPropAnswer[val].replace(/"|'/g, "").concat(".js")}\'`;
100120
}
101121
);
102122
}
103123
);
104124
} else {
105125
result = self
106-
.prompt([InputValidate("singularEntry", "Which will be your application entry point? (src/index)")])
107-
.then(
108-
(singularEntryAnswer: { singularEntry: string }): string => {
109-
let { singularEntry } = singularEntryAnswer;
110-
singularEntry = `\'${singularEntry.replace(/"|'/g, "")}\'`;
111-
if (singularEntry.length <= 0) {
112-
self.usingDefaults = true;
113-
}
114-
return singularEntry;
126+
.prompt([
127+
Input(
128+
"singularEntry",
129+
"Which will be your application entry point?",
130+
"src/index",
131+
),
132+
])
133+
.then((singularEntryAnswer: {
134+
singularEntry: string,
135+
}): string => {
136+
let { singularEntry } = singularEntryAnswer;
137+
singularEntry = `\'./${singularEntry.replace(/"|'/g, "").concat(".js")}\'`;
138+
if (singularEntry.length <= 0) {
139+
self.usingDefaults = true;
115140
}
116141
);
117142
}

‎packages/generators/utils/plugins.ts

-11
This file was deleted.

‎packages/generators/utils/style.ts

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import tooltip from "./tooltip";
2+
3+
export default function style(self, stylingType) {
4+
const ExtractUseProps = [];
5+
let regExpForStyles = null;
6+
switch (stylingType) {
7+
case "SASS":
8+
self.dependencies.push(
9+
"sass-loader",
10+
"node-sass",
11+
"style-loader",
12+
"css-loader",
13+
);
14+
regExpForStyles = `${new RegExp(/\.(scss|css)$/)}`;
15+
if (self.isProd) {
16+
ExtractUseProps.push(
17+
{
18+
loader: "'css-loader'",
19+
options: {
20+
sourceMap: true,
21+
},
22+
},
23+
{
24+
loader: "'sass-loader'",
25+
options: {
26+
sourceMap: true,
27+
},
28+
},
29+
);
30+
} else {
31+
ExtractUseProps.push(
32+
{
33+
loader: "'style-loader'",
34+
},
35+
{
36+
loader: "'css-loader'",
37+
},
38+
{
39+
loader: "'sass-loader'",
40+
},
41+
);
42+
}
43+
break;
44+
case "LESS":
45+
regExpForStyles = `${new RegExp(/\.(less|css)$/)}`;
46+
self.dependencies.push(
47+
"less-loader",
48+
"less",
49+
"style-loader",
50+
"css-loader",
51+
);
52+
if (self.isProd) {
53+
ExtractUseProps.push(
54+
{
55+
loader: "'css-loader'",
56+
options: {
57+
sourceMap: true,
58+
},
59+
},
60+
{
61+
loader: "'less-loader'",
62+
options: {
63+
sourceMap: true,
64+
},
65+
},
66+
);
67+
} else {
68+
ExtractUseProps.push(
69+
{
70+
loader: "'css-loader'",
71+
options: {
72+
sourceMap: true,
73+
},
74+
},
75+
{
76+
loader: "'less-loader'",
77+
options: {
78+
sourceMap: true,
79+
},
80+
},
81+
);
82+
}
83+
break;
84+
case "PostCSS":
85+
self.configuration.config.topScope.push(
86+
tooltip.postcss(),
87+
"const autoprefixer = require('autoprefixer');",
88+
"const precss = require('precss');",
89+
"\n",
90+
);
91+
self.dependencies.push(
92+
"style-loader",
93+
"css-loader",
94+
"postcss-loader",
95+
"precss",
96+
"autoprefixer",
97+
);
98+
regExpForStyles = `${new RegExp(/\.css$/)}`;
99+
if (self.isProd) {
100+
ExtractUseProps.push(
101+
{
102+
loader: "'css-loader'",
103+
options: {
104+
importLoaders: 1,
105+
sourceMap: true,
106+
},
107+
},
108+
{
109+
loader: "'postcss-loader'",
110+
options: {
111+
plugins: `function () {
112+
return [
113+
precss,
114+
autoprefixer
115+
];
116+
}`,
117+
},
118+
},
119+
);
120+
} else {
121+
ExtractUseProps.push(
122+
{
123+
loader: "'style-loader'",
124+
},
125+
{
126+
loader: "'css-loader'",
127+
options: {
128+
importLoaders: 1,
129+
sourceMap: true,
130+
},
131+
},
132+
{
133+
loader: "'postcss-loader'",
134+
options: {
135+
plugins: `function () {
136+
return [
137+
precss,
138+
autoprefixer
139+
];
140+
}`,
141+
},
142+
},
143+
);
144+
}
145+
break;
146+
case "CSS":
147+
self.dependencies.push("style-loader", "css-loader");
148+
regExpForStyles = `${new RegExp(/\.css$/)}`;
149+
if (self.isProd) {
150+
ExtractUseProps.push({
151+
loader: "'css-loader'",
152+
options: {
153+
sourceMap: true,
154+
},
155+
});
156+
} else {
157+
ExtractUseProps.push(
158+
{
159+
loader: "'style-loader'",
160+
options: {
161+
sourceMap: true,
162+
},
163+
},
164+
{
165+
loader: "'css-loader'",
166+
},
167+
);
168+
}
169+
break;
170+
}
171+
return { ExtractUseProps, regExpForStyles };
172+
}

‎packages/utils/run-prettier.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default function runPrettier(outputPath: string, source: string, cb?: Fun
1919
try {
2020
prettySource = prettier.format(source, {
2121
filepath: outputPath,
22-
parser: "babylon",
22+
parser: "babel",
2323
singleQuote: true,
2424
tabWidth: 1,
2525
useTabs: true,

‎packages/utils/scaffold.ts

+22-20
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,28 @@ export default function runTransform(transformConfig: TransformConfig, action: s
8181
configurationName = "webpack." + config.configName + ".js";
8282
}
8383

84-
const projectRoot = findProjectRoot();
85-
const outputPath: string = initActionNotDefined
86-
? transformConfig.configPath
87-
: path.join(projectRoot || process.cwd(), configurationName);
88-
const source: string = ast.toSource({
89-
quote: "single"
90-
});
91-
runPrettier(outputPath, source);
92-
}
93-
)
94-
.catch(
95-
(err: Error): void => {
96-
console.error(err.message ? err.message : err);
97-
}
98-
);
99-
}
100-
);
101-
let successMessage: string = `Congratulations! Your new webpack configuration file has been created!\n`;
84+
const projectRoot = findProjectRoot();
85+
const outputPath: string = initActionNotDefined
86+
? transformConfig.configPath
87+
: path.join(projectRoot || process.cwd(), configurationName);
88+
const source: string = ast.toSource({
89+
quote: "single",
90+
});
91+
runPrettier(outputPath, source);
92+
93+
})
94+
.catch((err: IError) => {
95+
console.error(err.message ? err.message : err);
96+
});
97+
});
98+
let successMessage: string =
99+
chalk.green(`Congratulations! Your new webpack configuration file has been created!\n\n`) +
100+
`You can now run ${chalk.green("npm run start")} to run your project!\n\n`;
101+
102102
if (initActionNotDefined && transformConfig.config.item) {
103-
successMessage = `Congratulations! ${transformConfig.config.item} has been ${action}ed!\n`;
103+
successMessage = chalk.green(`Congratulations! ${
104+
transformConfig.config.item
105+
} has been ${action}ed!\n`);
104106
}
105-
process.stdout.write("\n" + chalk.green(successMessage));
107+
process.stdout.write(`\n${successMessage}`);
106108
}

0 commit comments

Comments
 (0)
Please sign in to comment.