Skip to content

Commit 1b07d2b

Browse files
committedMay 28, 2019
misc(init): refactor with async/await
1 parent 488b06c commit 1b07d2b

File tree

2 files changed

+98
-113
lines changed

2 files changed

+98
-113
lines changed
 

‎packages/generators/init-generator.ts

+97-112
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import entryQuestions from "./utils/entry";
1212
import langQuestionHandler from "./utils/language";
1313
import styleQuestionHandler, { Loader, StylingType } from "./utils/style";
1414
import tooltip from "./utils/tooltip";
15+
import { type } from "os";
1516

1617
/**
1718
*
@@ -126,7 +127,7 @@ export default class InitGenerator extends Generator {
126127
}
127128

128129
// eslint-disable-next-line
129-
public prompting(): any {
130+
public async prompting() {
130131
const done: () => {} = this.async();
131132
const self: this = this;
132133
let regExpForStyles: string;
@@ -142,131 +143,115 @@ export default class InitGenerator extends Generator {
142143
`Alternatively, run "webpack(-cli) --help" for usage info\n\n`,
143144
);
144145

145-
return this.prompt([
146-
Confirm("multiEntries", "Will your application have multiple bundles?", false),
147-
])
148-
.then((multiEntriesAnswer: {
149-
multiEntries: boolean,
150-
}): Promise<{}> =>
151-
entryQuestions(self, multiEntriesAnswer.multiEntries),
152-
)
153-
.then((entryOption: object | string): void => {
154-
if (typeof entryOption === "string" && entryOption.length > 0) {
155-
this.configuration.config.webpackOptions.entry = `${entryOption}`;
156-
} else if (typeof entryOption === "object") {
157-
this.configuration.config.webpackOptions.entry = entryOption;
158-
}
159-
})
160-
.then((): Promise<{}> =>
161-
this.prompt([
162-
Input(
163-
"outputDir",
164-
"In which folder do you want to store your generated bundles?",
165-
"dist",
166-
),
167-
]),
168-
)
169-
.then((outputDirAnswer: {
170-
outputDir: string;
171-
}): void => {
172-
// As entry is not required anymore and we dont set it to be an empty string or """""
173-
// it can be undefined so falsy check is enough (vs entry.length);
174-
if (
175-
!this.configuration.config.webpackOptions.entry &&
176-
!this.usingDefaults
177-
) {
178-
this.configuration.config.webpackOptions.output = {
179-
chunkFilename: "'[name].[chunkhash].js'",
180-
filename: "'[name].[chunkhash].js'",
181-
};
182-
} else if (!this.usingDefaults) {
183-
this.configuration.config.webpackOptions.output = {
184-
filename: "'[name].[chunkhash].js'",
185-
};
186-
}
187-
if (!this.usingDefaults && outputDirAnswer.outputDir.length) {
188-
this.configuration.config.webpackOptions.output.path =
189-
`path.resolve(__dirname, '${outputDirAnswer.outputDir}')`;
190-
}
191-
})
192-
.then((): Promise<{}> =>
193-
this.prompt([
194-
List("langType", "Will you use one of the below JS solutions?", [
195-
"ES6",
196-
"Typescript",
197-
"No",
198-
]),
146+
const { multiEntries }: { multiEntries: boolean } = await this.prompt([
147+
Confirm(
148+
"multiEntries",
149+
"Will your application have multiple bundles?",
150+
false
151+
),
152+
]);
153+
154+
const entryOption: string | object = await entryQuestions(self, multiEntries);
155+
156+
if (typeof entryOption === "string" && entryOption.length > 0) {
157+
this.configuration.config.webpackOptions.entry = `${entryOption}`;
158+
} else if (typeof entryOption === "object") {
159+
this.configuration.config.webpackOptions.entry = entryOption;
160+
}
161+
162+
const { outputDir }: { outputDir: string } = await this.prompt([
163+
Input(
164+
"outputDir",
165+
"In which folder do you want to store your generated bundles?",
166+
"dist",
167+
),
168+
]);
169+
170+
// As entry is not required anymore and we dont set it to be an empty string or """""
171+
// it can be undefined so falsy check is enough (vs entry.length);
172+
if (
173+
!this.configuration.config.webpackOptions.entry &&
174+
!this.usingDefaults
175+
) {
176+
this.configuration.config.webpackOptions.output = {
177+
chunkFilename: "'[name].[chunkhash].js'",
178+
filename: "'[name].[chunkhash].js'",
179+
};
180+
} else if (!this.usingDefaults) {
181+
this.configuration.config.webpackOptions.output = {
182+
filename: "'[name].[chunkhash].js'",
183+
};
184+
}
185+
if (!this.usingDefaults && outputDir.length) {
186+
this.configuration.config.webpackOptions.output.path =
187+
`path.resolve(__dirname, '${outputDir}')`;
188+
}
189+
190+
const { langType }: { langType: string } = await this.prompt([
191+
List("langType", "Will you use one of the below JS solutions?", [
192+
"ES6",
193+
"Typescript",
194+
"No",
199195
]),
200-
)
201-
.then((langTypeAnswer: {
202-
langType: string;
203-
}): void => {
204-
langQuestionHandler(this, langTypeAnswer.langType);
205-
})
206-
.then((): Promise<{}> =>
207-
this.prompt([
196+
]);
197+
198+
langQuestionHandler(this, langType);
199+
200+
const { stylingType }: { stylingType: string } = await this.prompt([
208201
List("stylingType", "Will you use one of the below CSS solutions?", [
209202
"No",
210203
StylingType.CSS,
211204
StylingType.SASS,
212205
StylingType.LESS,
213206
StylingType.PostCSS,
214207
]),
215-
]))
216-
.then((stylingTypeAnswer: {
217-
stylingType: string;
218-
}): void => {
219-
({ ExtractUseProps, regExpForStyles } = styleQuestionHandler(self, stylingTypeAnswer.stylingType));
220-
})
221-
.then((): Promise<{}> | void => {
208+
]);
209+
210+
({ ExtractUseProps, regExpForStyles } = styleQuestionHandler(self, stylingType));
211+
212+
if (this.isProd) {
213+
// Ask if the user wants to use extractPlugin
214+
const { useExtractPlugin }: { useExtractPlugin: string } = await this.prompt([
215+
Input(
216+
"useExtractPlugin",
217+
"If you want to bundle your CSS files, what will you name the bundle? (press enter to skip)",
218+
),
219+
]);
220+
221+
if (regExpForStyles) {
222222
if (this.isProd) {
223-
// Ask if the user wants to use extractPlugin
224-
return this.prompt([
225-
Input(
226-
"useExtractPlugin",
227-
"If you want to bundle your CSS files, what will you name the bundle? (press enter to skip)",
228-
),
229-
]);
230-
}
231-
})
232-
.then((useExtractPluginAnswer: {
233-
useExtractPlugin: string;
234-
}): void => {
235-
if (regExpForStyles) {
236-
if (this.isProd) {
237-
const cssBundleName: string = useExtractPluginAnswer.useExtractPlugin;
238-
this.dependencies.push("mini-css-extract-plugin");
239-
this.configuration.config.topScope.push(
240-
tooltip.cssPlugin(),
241-
"const MiniCssExtractPlugin = require('mini-css-extract-plugin');",
242-
"\n",
223+
const cssBundleName: string = useExtractPlugin;
224+
this.dependencies.push("mini-css-extract-plugin");
225+
this.configuration.config.topScope.push(
226+
tooltip.cssPlugin(),
227+
"const MiniCssExtractPlugin = require('mini-css-extract-plugin');",
228+
"\n",
229+
);
230+
if (cssBundleName.length !== 0) {
231+
(this.configuration.config.webpackOptions.plugins as string[]).push(
232+
// TODO: use [contenthash] after it is supported
233+
`new MiniCssExtractPlugin({ filename:'${cssBundleName}.[chunkhash].css' })`,
234+
);
235+
} else {
236+
(this.configuration.config.webpackOptions.plugins as string[]).push(
237+
"new MiniCssExtractPlugin({ filename:'style.css' })",
243238
);
244-
if (cssBundleName.length !== 0) {
245-
(this.configuration.config.webpackOptions.plugins as string[]).push(
246-
// TODO: use [contenthash] after it is supported
247-
`new MiniCssExtractPlugin({ filename:'${cssBundleName}.[chunkhash].css' })`,
248-
);
249-
} else {
250-
(this.configuration.config.webpackOptions.plugins as string[]).push(
251-
"new MiniCssExtractPlugin({ filename:'style.css' })",
252-
);
253-
}
254-
255-
ExtractUseProps.unshift({
256-
loader: "MiniCssExtractPlugin.loader",
257-
});
258239
}
259240

260-
this.configuration.config.webpackOptions.module.rules.push(
261-
{
262-
test: regExpForStyles,
263-
use: ExtractUseProps,
264-
},
265-
);
241+
ExtractUseProps.unshift({
242+
loader: "MiniCssExtractPlugin.loader",
243+
});
266244
}
267245

268-
done();
269-
});
246+
this.configuration.config.webpackOptions.module.rules.push(
247+
{
248+
test: regExpForStyles,
249+
use: ExtractUseProps,
250+
},
251+
);
252+
}
253+
}
254+
done();
270255
}
271256

272257
public installPlugins(): void {

‎packages/generators/utils/entry.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface CustomGenerator extends Generator {
1919
export default function entry(
2020
self: CustomGenerator,
2121
multiEntries: boolean,
22-
): Promise<{}> {
22+
): Promise<object | string> {
2323
let entryIdentifiers: string[];
2424
let result: Promise<{}>;
2525
if (multiEntries) {

0 commit comments

Comments
 (0)
Please sign in to comment.