-
-
Notifications
You must be signed in to change notification settings - Fork 620
/
Copy pathentry.ts
109 lines (101 loc) · 3.09 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
import 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 async function entry(
self: CustomGenerator,
multiEntries: boolean,
autoGenerateDefaults = false
): Promise<void | {}> {
const webpackEntryPoint: object = {};
// TODO: refactoring to async/await
async function forEachPromise(
entries: string[],
fn: (entryProp: string) => Promise<void | {}>
): Promise<object | {}> {
return entries.reduce((promise: Promise<{}>, prop: string): object => {
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());
}
if (multiEntries) {
const multipleEntriesAnswer = await InputValidate(
self,
"multipleEntries",
"What do you want to name your bundles? (separated by comma)",
validate,
"pageOne, pageTwo",
autoGenerateDefaults
);
const entryIdentifiers: string[] = multipleEntriesAnswer.multipleEntries.split(",");
const entryPropAnswer = await forEachPromise(
entryIdentifiers,
(entryProp: string): Promise<void | {}> => {
return InputValidate(
self,
`${entryProp}`,
`What is the location of "${entryProp}"?`,
validate,
`src/${entryProp}`,
autoGenerateDefaults
);
}
);
const remainingEntryPropKeys = Object.keys(entryPropAnswer);
if (remainingEntryPropKeys.length > 0) {
remainingEntryPropKeys.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;
}
const singleEntryResult = await Input(
self,
"singularEntry",
"Which will be your application entry point?",
"src/index",
autoGenerateDefaults
);
let { singularEntry } = singleEntryResult;
singularEntry = `\'./${singularEntry.replace(/"|'/g, "").concat(".js")}\'`;
return singularEntry;
}