-
-
Notifications
You must be signed in to change notification settings - Fork 601
/
entry.ts
107 lines (101 loc) · 3.17 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
import { InputValidate } from "@webpack-cli/webpack-scaffold";
import validate from "./validate";
interface IEntry extends IYeoman {
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: IEntry, answer: {
entryType: boolean;
}): Promise<{}> {
let entryIdentifiers: string[];
let result: Promise<{}>;
if (answer.entryType) {
result = self
.prompt([
InputValidate(
"multipleEntries",
"Type the names you want for your modules (entry files), separated by comma [example: app,vendor]",
validate,
),
])
.then((multipleEntriesAnswer: {
multipleEntries: string,
}) => {
const webpackEntryPoint: object = {};
entryIdentifiers = multipleEntriesAnswer.multipleEntries.split(",");
function forEachPromise(entries: string[], fn: (entryProp: string) => Promise<{} | void>) {
return entries.reduce((promise: Promise<{}>, prop: string) => {
const trimmedProp: string = prop.trim();
return promise.then((n: object) => {
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}"? [example: ./src/${entryProp}]`,
validate,
),
]),
).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, "")}\'`;
}
webpackEntryPoint[val] = entryPropAnswer[val];
});
return webpackEntryPoint;
});
});
} else {
result = self
.prompt([
InputValidate(
"singularEntry",
"Which module will be the first to enter the application? [default: ./src/index]",
),
])
.then((singularEntryAnswer: {
singularEntry: string,
}): string => {
let { singularEntry } = singularEntryAnswer;
singularEntry = `\'${singularEntry.replace(/"|'/g, "")}\'`;
if (singularEntry.length <= 0) {
self.usingDefaults = true;
}
return singularEntry;
});
}
return result;
}