-
-
Notifications
You must be signed in to change notification settings - Fork 622
/
Copy pathindex.ts
executable file
·156 lines (140 loc) · 3.05 KB
/
index.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import * as jscodeshift from "jscodeshift";
import * as Generator from "yeoman-generator";
export function createArrowFunction(value: string): string {
return `() => '${value}'`;
}
export function createRegularFunction(value: string): string {
return `function () {\n return '${value}'\n}`;
}
export function createDynamicPromise(arrOrString: string[] | string): string {
if (Array.isArray(arrOrString)) {
return (
"() => new Promise((resolve) => resolve([" +
arrOrString.map((func: string): string => {
return "'" + func + "'";
}) +
"]))"
);
} else {
return `() => new Promise((resolve) => resolve('${arrOrString}'))`;
}
}
export function createAssetFilterFunction(value: string): string {
return `function (assetFilename) {\n return assetFilename.endsWith('.${value}');\n}`;
}
export function createExternalFunction(regexp: string): string {
return (
"\n function (context, request, callback) {\n if (" +
"/" +
regexp +
"/.test(request)){" +
"\n" +
" return callback(null, 'commonjs' + request);\n}\n" +
"callback();\n}"
);
}
export function parseValue(regexp: string): string {
return jscodeshift(regexp);
}
export function createRequire(val: string): string {
return `const ${val} = require('${val}');`;
}
export function List(
self: any,
name: string,
message: string,
choices: string[],
defaultChoice?: string,
skip: boolean = false
): object | any {
if (skip) return { [name]: defaultChoice };
return self.prompt([
{
choices,
message,
name,
type: "list",
default: defaultChoice
}
]);
}
export function RawList(name: string, message: string, choices: string[]): Generator.Question {
return {
choices,
message,
name,
type: "rawlist"
};
}
export function CheckList(name: string, message: string, choices: string[]): Generator.Question {
return {
choices,
message,
name,
type: "checkbox"
};
}
export function Input(
self: any,
name: string,
message: string,
defaultChoice?: string,
skip: boolean = false
): object | any {
if (skip) return { [name]: defaultChoice };
return self.prompt([
{
default: defaultChoice,
message,
name,
type: "input"
}
]);
}
export function InputValidate(
self: any,
name: string,
message: string,
cb?: (input: string) => string | boolean,
defaultChoice?: string,
skip?: boolean
): object | any {
if (skip) return { [name]: defaultChoice };
const input: Generator.Question = {
message,
name,
type: "input",
validate: cb
};
if (defaultChoice) input.default = defaultChoice;
return self.prompt([input]);
}
export function Confirm(
self: any,
name: string,
message: string,
defaultChoice: boolean = true,
skip: boolean = false
): object | any {
if (skip) return { [name]: defaultChoice };
return self.prompt([
{
default: defaultChoice,
message,
name,
type: "confirm"
}
]);
}
// TODO: to understand this type
// eslint-disable-next-line
export function AutoComplete(name: string, message: string, options: object = {}): any {
return Object.assign(
{
message,
name,
type: "autocomplete"
},
options
);
}