/
entry.ts
126 lines (119 loc) · 3.55 KB
/
entry.ts
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
import * as Generator from "yeoman-generator";
import { Input, InputValidate } from "@webpack-cli/webpack-scaffold";
import validate from "./validate";
interface CustomGenerator extends Generator {
usingDefaults?: boolean;
}
/**
*
* Prompts for entry points, either if it has multiple or one entry
*
* @param {Object} self - A variable holding the instance of the prompting
* @param {Object} answer - Previous answer from asking if the user wants single or multiple entries
* @returns {Object} An Object that holds the answers given by the user, later used to scaffold
*/
export default function entry(
self: CustomGenerator,
multiEntries: boolean,
): Promise<object | string> {
let entryIdentifiers: string[];
let result: Promise<{}>;
if (multiEntries) {
result = self
.prompt([
InputValidate(
"multipleEntries",
"How do you want to name your bundles? (separated by comma)",
validate,
"pageOne, pageTwo"
)
])
.then(
(multipleEntriesAnswer: { multipleEntries: string }): Promise<void | {}> => {
const webpackEntryPoint: object = {};
entryIdentifiers = multipleEntriesAnswer.multipleEntries.split(",");
function forEachPromise(
entries: string[],
fn: (entryProp: string) => Promise<void | {}>
): Promise<void | {}> {
return entries.reduce((promise: Promise<{}>, prop: string): Promise<void | {}> => {
const trimmedProp: string = prop.trim();
return promise.then(
(n: object): Promise<void | {}> => {
if (n) {
Object.keys(n).forEach(
(val: string): void => {
if (
n[val].charAt(0) !== "(" &&
n[val].charAt(0) !== "[" &&
!n[val].includes("function") &&
!n[val].includes("path") &&
!n[val].includes("process")
) {
n[val] = `\'./${n[val].replace(/"|'/g, "").concat(".js")}\'`;
}
webpackEntryPoint[val] = n[val];
}
);
} else {
n = {};
}
return fn(trimmedProp);
}
);
}, Promise.resolve());
}
return forEachPromise(
entryIdentifiers,
(entryProp: string): Promise<void | {}> =>
self.prompt([
InputValidate(
`${entryProp}`,
`What is the location of "${entryProp}"?`,
validate,
`src/${entryProp}`,
),
]))
.then(
(entryPropAnswer: object): object => {
Object.keys(entryPropAnswer).forEach(
(val: string): void => {
if (
entryPropAnswer[val].charAt(0) !== "(" &&
entryPropAnswer[val].charAt(0) !== "[" &&
!entryPropAnswer[val].includes("function") &&
!entryPropAnswer[val].includes("path") &&
!entryPropAnswer[val].includes("process")
) {
entryPropAnswer[val] = `\'./${entryPropAnswer[val].replace(/"|'/g, "").concat(".js")}\'`;
}
webpackEntryPoint[val] = entryPropAnswer[val];
}
);
return webpackEntryPoint;
}
);
}
);
} else {
result = self
.prompt([
Input(
"singularEntry",
"Which will be your application entry point?",
"src/index",
)
])
.then(
(singularEntryAnswer: { singularEntry: string }): string => {
let { singularEntry } = singularEntryAnswer;
singularEntry = `\'./${singularEntry.replace(/"|'/g, "").concat(".js")}\'`;
if (singularEntry.length <= 0) {
self.usingDefaults = true;
}
return singularEntry;
}
);
}
return result;
}