generated from LiteLoaderQQNT/Plugin-Template
-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.js
244 lines (231 loc) · 8 KB
/
main.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const fs = require("fs");
const path = require("path");
const { BrowserWindow, ipcMain, webContents, shell } = require("electron");
const { extractUserStyleMetadata } = require("./modules/main/parser");
const { listStyles } = require("./modules/main/walker");
const { normalize, debounce, simpleLog, dummyLog, renderStylus } = require("./modules/main/utils");
const isDebug = process.argv.includes("--transitio-debug");
const updateInterval = 1000;
const log = isDebug ? simpleLog : dummyLog;
let devMode = false;
let watcher = null;
const supportedPreprocessors = ["none", "transitio", "stylus"];
const dataPath = LiteLoader.plugins.transitio.path.data;
const stylePath = path.join(dataPath, "styles");
const debouncedSet = debounce(LiteLoader.api.config.set, updateInterval);
// Create `styles` directory if not exists
if (!fs.existsSync(stylePath)) {
log(`${stylePath} does not exist, creating...`);
fs.mkdirSync(stylePath, { recursive: true });
}
// IPC events
ipcMain.on("LiteLoader.transitio.rendererReady", (event) => {
const window = BrowserWindow.fromWebContents(event.sender);
reloadStyle(window.webContents);
});
ipcMain.on("LiteLoader.transitio.reloadStyle", (event) => {
reloadStyle();
});
ipcMain.on("LiteLoader.transitio.importStyle", (event, fname, content) => {
importStyle(fname, content);
});
ipcMain.on("LiteLoader.transitio.removeStyle", (event, absPath) => {
log("removeStyle", absPath);
fs.unlinkSync(absPath);
delete config.styles[absPath];
updateConfig();
if (!devMode) {
const msg = {
path: absPath, enabled: false, css: "/* Removed */", meta: {
name: " [已删除] ",
description: "[此样式已被删除]",
enabled: false,
preprocessor: "transitio",
vars: {}
}
};
webContents.getAllWebContents().forEach((webContent) => {
webContent.send("LiteLoader.transitio.updateStyle", msg);
});
}
});
ipcMain.on("LiteLoader.transitio.resetStyle", (event, absPath) => {
log("resetStyle", absPath);
delete config.styles[absPath].variables;
updateConfig();
updateStyle(absPath);
});
ipcMain.on("LiteLoader.transitio.open", (event, type, uri) => {
log("open", type, uri);
switch (type) {
case "link":
shell.openExternal(uri);
break;
case "path":
shell.openPath(path.normalize(uri));
break;
case "show":
shell.showItemInFolder(path.normalize(uri));
break;
default:
break;
}
});
ipcMain.on("LiteLoader.transitio.configChange", onConfigChange);
ipcMain.on("LiteLoader.transitio.devMode", onDevMode);
ipcMain.handle("LiteLoader.transitio.queryDevMode", async (event) => {
log("queryDevMode", devMode);
return devMode;
});
ipcMain.handle("LiteLoader.transitio.queryIsDebug", async (event) => {
log("queryIsDebug", isDebug);
return isDebug;
});
function updateConfig() {
log("Calling updateConfig");
debouncedSet("transitio", config);
}
let config = LiteLoader.api.config.get("transitio", { styles: {} });
// Get CSS content
function getStyle(absPath) {
if (absPath.endsWith(".lnk") && shell.readShortcutLink) { // lnk file & on Windows
const { target } = shell.readShortcutLink(absPath);
absPath = target;
}
try {
return fs.readFileSync(absPath, "utf-8");
} catch (err) {
log("getStyle", absPath, err);
return "";
}
}
// Send updated style to renderer
async function updateStyle(absPath, webContent) {
absPath = normalize(absPath);
log("updateStyle", absPath);
let css = getStyle(absPath);
if (!css) return;
// Initialize style configuration
if (typeof config.styles[absPath] !== "object") {
config.styles[absPath] = {
enabled: Boolean(config.styles[absPath] ?? true)
};
updateConfig();
}
// Read metadata
const enabled = config.styles[absPath].enabled;
const meta = extractUserStyleMetadata(css);
meta.name ??= path.basename(absPath, ".css");
meta.description ??= "此文件没有描述";
meta.preprocessor ??= "transitio";
if (!supportedPreprocessors.includes(meta.preprocessor)) {
log(`Unsupported preprocessor "${meta.preprocessor}" at ${absPath}`);
return;
}
// Read variables config, delete non-existent ones
const udfVariables = config.styles[absPath].variables ?? {};
for (const [varName, varValue] of Object.entries(udfVariables)) {
if (varName in meta.vars) {
meta.vars[varName].value = varValue;
} else {
log(`Variable "${varName}" not found in ${absPath}`);
delete config.styles[absPath].variables[varName];
updateConfig();
}
}
if (meta.preprocessor === "stylus") {
try {
css = await renderStylus(absPath, css, meta.vars);
} catch (err) {
log(`Failed to render ${absPath}:`, err);
css = `/* Stylus 编译失败: ${err.name} (使用 Debug 模式查看终端输出来获得更多信息) */`;
meta.name += " (编译失败)";
}
}
// Send message to renderer
const msg = { path: absPath, enabled, css, meta };
if (webContent) {
webContent.send("LiteLoader.transitio.updateStyle", msg);
} else {
webContents.getAllWebContents().forEach((webContent) => {
webContent.send("LiteLoader.transitio.updateStyle", msg);
});
}
}
// Reload all styles
async function reloadStyle(webContent) {
log("reloadStyle");
if (webContent) {
webContent.send("LiteLoader.transitio.resetStyle");
} else {
webContents.getAllWebContents().forEach((webContent) => {
webContent.send("LiteLoader.transitio.resetStyle");
});
}
config = LiteLoader.api.config.get("transitio", { styles: {} });
const styles = listStyles(stylePath);
for (const absPath of styles) {
updateStyle(absPath, webContent);
}
const removedStyles = new Set(Object.keys(config.styles)).difference(new Set(styles));
for (const absPath of removedStyles) {
log("Removed style", absPath);
delete config.styles[absPath];
}
if (removedStyles.size) {
updateConfig();
}
}
// Import style from renderer
function importStyle(fname, content) {
log("importStyle", fname);
const filePath = path.join(stylePath, fname);
fs.writeFileSync(filePath, content, "utf-8");
if (!devMode) {
updateStyle(filePath);
}
}
// Reload styles when file changes
function onStyleChange(eventType, filename) {
log("onStyleChange", eventType, filename);
// Ideally, we should only update the changed style:
// if (eventType === "change" && filename) {
// updateStyle(filename.slice(0, -4));
// } else {
// resetStyle();
// }
// However, Node's fs.watch is not reliable enough.
// Renaming a file will trigger a `change` event instead of `rename`, making it hard to distinguish.
reloadStyle(); // For now, just reload all styles. (Any way, only in dev mode)
}
// Listen to config modification (from renderer)
function onConfigChange(event, absPath, arg) {
log("onConfigChange", absPath, arg);
const styleConfig = config.styles[absPath];
if (typeof arg === "boolean") {
styleConfig.enabled = arg;
} else if (typeof arg === "object") {
styleConfig.variables = Object.assign(styleConfig.variables ?? {}, arg);
}
updateConfig();
updateStyle(absPath);
}
// Listen to dev mode switch (from renderer)
function onDevMode(event, enable) {
log("onDevMode", enable);
devMode = enable;
if (enable && !watcher) {
watcher = watchStyleChange();
log("watcher created");
} else if (!enable && watcher) {
watcher.close();
watcher = null;
log("watcher closed");
}
}
// Listen to `styles` directory
function watchStyleChange() {
return fs.watch(stylePath, "utf-8",
debounce(onStyleChange, updateInterval)
);
}