-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.js
211 lines (183 loc) · 7.12 KB
/
generate.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const fs = require("node:fs");
const path = require("node:path");
const ts = require("typescript");
const mappedTypes = {
"discord/actions/ContextMenuActionCreators": "ContextMenuActionCreators",
"discord/actions/UserSettingsModalActionCreators": "UserSettingsModalActionCreators",
"discord/Constants": "Constants",
"discord/components/common/Alerts": "Alerts",
"discord/components/common/BaseHeaderBar": "BaseHeaderBar",
"discord/components/common/FileUpload": "FileUpload",
"discord/components/common/FormSwitch.css": "FormSwitchCSS",
"discord/components/common/HeaderBar.css": "HeaderBarCSS",
"discord/components/common/HelpMessage.css": "HelpMessageCSS",
"discord/components/common/index": "Components",
"discord/components/common/PanelButton": "PanelButton",
"discord/components/common/Scroller.css": "ScrollerCSS",
"discord/components/modals/ConfirmModal": "ConfirmModal",
"discord/Dispatcher": "DispatcherInstance",
"discord/lib/web/Storage": "Storage",
"discord/lib/BaseRecord": "BaseRecord",
"discord/modules/build_overrides/web/BuildOverride.css": "BuildOverrideCSS",
"discord/modules/discovery/web/Discovery.css": "DiscoveryCSS",
"discord/modules/forums/web/Forums.css": "ForumsCSS",
"discord/modules/forums/web/Header.css": "ForumHeaderCSS",
"discord/modules/forums/web/SortMenu.css": "ForumSortMenuCSS",
"discord/modules/forums/web/Tag": "ForumTag",
"discord/modules/guild_settings/roles/web/GuildSettingsRoleEdit.css": "GuildSettingsRoleEditCSS",
"discord/modules/guild_settings/web/AppCard.css": "AppCardCSS",
"discord/modules/guild_settings/web/AppCardItem.css": "AppCardItemCSS",
"discord/modules/guild_settings/web/SearchSection.css": "GuildSettingsSearchSectionCSS",
"discord/modules/guild_sidebar/web/CategoryChannel.css": "CategoryChannelCSS",
"discord/modules/markup/MarkupUtils": "MarkupUtils",
"discord/modules/menus/web/Menu": "Menu",
"discord/modules/messages/web/Markup.css": "MarkupCSS",
"discord/modules/messages/web/Message.css": "MessageCSS",
"discord/modules/oauth2/index": "Oauth2",
"discord/modules/people/web/PeoplePage.css": "PeoplePageCSS",
"discord/modules/user_profile/web/BiteSizeActivity.css": "ByteSizeActivityCSS",
"discord/packages/flux": "Flux",
"discord/packages/flux/BatchedStoreListener": "BatchedStoreListener",
"discord/packages/flux/ChangeListeners": "ChangeListeners",
"discord/packages/flux/connectStores": "connectStores",
"discord/packages/flux/Dispatcher": "_Dispatcher",
"discord/packages/flux/Emitter": "Emitter",
"discord/packages/flux/LoggingUtils": "LoggingUtils",
"discord/packages/flux/PersistedStore": "PersistedStore",
"discord/packages/flux/Store": "Store",
"discord/records/UserRecord": "UserRecord",
"discord/styles/shared/Margins.css": "MarginsCSS",
"discord/uikit/Flex": "Flex",
"discord/utils/ClipboardUtils": "ClipboardUtils",
"discord/utils/HTTPUtils": "HTTPUtils",
"discord/utils/MaskedLinkUtils": "MaskedLinkUtils",
"discord/utils/NativeUtils": "NativeUtils",
"chroma-js": "ChromaJS",
classnames: "classnames",
"dependency-graph": "DependencyGraph",
"highlight.js": "HighlightJS",
"highlight.js/lib/core": "HighlightJSCore",
lodash: "lodash",
murmurhash: "murmurhash",
"platform.js": "PlatformJS",
react: "React",
"uuid/v4": "UUID"
};
const write = process.argv.includes("--write");
function getExports(path) {
let sourcePath = "./src/mappings/" + path + ".ts";
if (!fs.existsSync(sourcePath)) sourcePath = "./src/mappings/" + path + "/index.ts";
if (!fs.existsSync(sourcePath)) return null;
const program = ts.createProgram([sourcePath], {
target: ts.ScriptTarget.ES2022,
module: ts.ScriptTarget.ES2022,
noEmit: true
});
const source = program.getSourceFile(sourcePath);
const typeChecker = program.getTypeChecker();
const alias = source.statements.find(
(s) => (ts.isTypeAliasDeclaration(s) || ts.isInterfaceDeclaration(s)) && s.name.getText(source) === "Exports"
);
if (!alias) return null;
const type = typeChecker.getTypeAtLocation(alias);
return type.getProperties().map((p) => p.getName());
}
function getPaths() {
const paths = fs.readdirSync("src/mappings", { recursive: true });
const out = [];
for (const filePath of paths) {
if (!filePath.endsWith(".ts")) continue;
if (path.basename(filePath).startsWith("_")) continue;
out.push(filePath.replace(".ts", "").replaceAll("\\", "/"));
}
return out.sort();
}
function generateImports() {
let str = "// auto-generated\n";
for (const path of getPaths()) {
str += `import "./mappings/${path}";\n`;
}
if (write) {
fs.writeFileSync("src/modules.ts", str);
} else {
console.log(str);
}
}
function generateTypes() {
const paths = Object.keys(mappedTypes).sort();
let str = "// auto-generated\n";
for (const path of paths) {
const type = mappedTypes[path];
//?? path.substring(path.lastIndexOf("/") + 1).replace(".css", "CSS");
str += `import ${type} from "./mappings/${path}";\n`;
}
str += "\n";
str += "export type MappedModules = {\n";
for (const path of paths) {
const type = mappedTypes[path];
//?? path.substring(path.lastIndexOf("/") + 1).replace(".css", "CSS");
str += ` "${path}": ${type};\n`;
}
str += "};\n\n";
str += "export declare function WebpackRequire<T extends keyof MappedModules>(\n module: T\n): MappedModules[T];\n";
if (write) {
fs.writeFileSync("src/types.ts", str);
} else {
console.log(str);
}
}
function generateDeclares(prefix) {
let str = "// auto-generated\n";
for (const path of Object.keys(mappedTypes).sort()) {
str += `declare module "${prefix}${path}" {\n`;
const properties = getExports(path);
if (properties.some((s) => s === "__mappings_exportEquals")) {
str += ` import { MappedModules } from "@moonlight-mod/mappings";\n`;
str += ` const _: Omit<MappedModules["${path}"], "__mappings_exportEquals">;\n`;
str += ` export = _;\n`;
} else if (properties.length > 0) {
str += ` import { MappedModules } from "@moonlight-mod/mappings";\n`;
for (const name of properties) {
if (name === "default") {
str += ` const _default: MappedModules["${path}"]["default"];\n`;
str += ` export default _default;\n`;
} else {
// :(
if (name.includes("-")) continue;
str += ` export const ${name}: MappedModules["${path}"]["${name}"];\n`;
}
}
}
str += `}\n\n`;
}
console.log(str);
}
function generatePaths() {
const paths = getPaths();
const exclude = ["discord/packages/flux/index"];
for (const path of paths) {
const properties = getExports(path);
if (!properties) continue;
if (!(path in mappedTypes) && !exclude.includes(path)) {
console.log("Missing:", path);
}
}
}
const command = process.argv.length > 2 ? process.argv[2] : null;
switch (command) {
case "imports":
generateImports();
break;
case "types":
generateTypes();
break;
case "declares":
generateDeclares(process.argv[3]);
break;
case "paths":
generatePaths();
break;
default:
console.error(`Usage: node generate.js <imports|types|declares|paths>`);
break;
}