/
remove-generator.ts
140 lines (130 loc) · 3.85 KB
/
remove-generator.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
import * as fs from "fs";
import * as path from "path";
import * as Generator from "yeoman-generator";
import PROP_TYPES from "@webpack-cli/utils/prop-types";
import { List } from "@webpack-cli/webpack-scaffold";
import { WebpackOptions } from "./types";
/**
*
* Generator for removing properties
* @class RemoveGenerator
* @extends Generator
* @returns {Void} After execution, transforms are triggered
*
*/
export default class RemoveGenerator extends Generator {
private configuration: {
config: {
configName?: string;
topScope?: string[];
webpackOptions?: WebpackOptions;
};
};
private webpackOptions: WebpackOptions | string;
public constructor(args, opts) {
super(args, opts);
this.configuration = {
config: {
webpackOptions: {}
}
};
const { configFile } = opts;
let configPath = path.resolve(process.cwd(), configFile);
const webpackConfigExists = fs.existsSync(configPath);
this.webpackOptions = require(configPath);
}
public getPropTypes(): string[] {
return Object.keys(this.webpackOptions);
}
public getModuleLoadersNames(): string[] {
if (typeof this.webpackOptions === "object") {
if (this.webpackOptions.module && this.webpackOptions.module.rules) {
return this.webpackOptions.module.rules.map(
(rule: { loader: string }): string | null => (rule ? rule.loader : null)
);
}
}
}
public prompting(): Promise<void | {}> {
const done: () => {} = this.async();
let propValue: object | string | boolean;
return this.prompt([List("propType", "Which property do you want to remove?", Array.from(this.getPropTypes()))])
.then(
({ propType }: { propType: string }): Promise<void | {}> => {
if (!PROP_TYPES.has(propType)) {
console.error("Invalid webpack config prop");
return;
}
propValue = this.webpackOptions[propType];
if (typeof propValue === "object") {
if (Array.isArray(propValue)) {
return this.prompt([
List(
"keyType",
`Which key do you want to remove from ${propType}?`,
Array.from(propValue)
)
]).then(
({ keyType }: { keyType: string }): void => {
this.configuration.config.webpackOptions[propType] = [keyType];
}
);
} else {
return this.prompt([
List(
"keyType",
`Which key do you want to remove from ${propType}?`,
Array.from(Object.keys(propValue))
)
]).then(
({ keyType }: { keyType: string }): Promise<void | {}> => {
if (propType === "module" && keyType === "rules") {
return this.prompt([
List(
"rule",
"Which loader do you want to remove?",
Array.from(this.getModuleLoadersNames())
)
]).then(
({ rule }: { rule: string }): void => {
if (typeof this.webpackOptions === "object") {
const loaderIndex: number = this.getModuleLoadersNames().indexOf(
rule
);
const loader: object = this.webpackOptions.module.rules[
loaderIndex
];
this.configuration.config.webpackOptions.module = {
rules: [loader]
};
}
}
);
} else {
// remove the complete prop object if there is only one key
if (Object.keys(this.webpackOptions[propType]).length <= 1) {
this.configuration.config.webpackOptions[propType] = null;
} else {
this.configuration.config.webpackOptions[propType] = {
[keyType]: null
};
}
}
}
);
}
} else {
this.configuration.config.webpackOptions[propType] = null;
}
}
)
.then(
(): void => {
done();
}
);
}
public writing(): void {
this.config.set("configuration", this.configuration);
}
}