Skip to content

Commit daadd04

Browse files
committed
extensions: Further refactors and fixes.
• Refactor loadStyles/addStyle functions in module to use `await` over `.then` syntax. • Fix regression caused by some unfinished refactoring. Fixes: #558
1 parent ff8b18b commit daadd04

1 file changed

Lines changed: 37 additions & 44 deletions

File tree

sources/code/main/modules/extensions.ts

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,32 @@ async function parseImports(cssString: string, importCalls: string[], maxTries=5
5757
return cssString;
5858
}
5959

60+
async function encrypt(buffer:Buffer) {
61+
if((await safeStoragePromise).isEncryptionAvailable())
62+
return (await safeStoragePromise).encryptString(buffer.toString());
63+
return buffer.toString();
64+
}
65+
66+
async function decrypt(app:Electron.App, string:Buffer) {
67+
if(!(await safeStoragePromise).isEncryptionAvailable() && !app.isReady() && process.platform !== "darwin")
68+
await app.whenReady();
69+
if(!(await safeStoragePromise).isEncryptionAvailable())
70+
return string.toString();
71+
if(!string.toString("utf-8").includes("�"))
72+
throw new Error("One of loaded styles was not encrypted and could not be loaded.");
73+
return (await safeStoragePromise).decryptString(string);
74+
}
75+
6076
async function addStyle(window?:Electron.BrowserWindow) {
6177
const [
62-
electron,
78+
e,
6379
fs,
64-
pth,
65-
safeStorage
80+
pth
6681
] = [
6782
import("electron/main"),
6883
import("fs/promises"),
69-
import("path"),
70-
safeStoragePromise
84+
import("path")
7185
];
72-
async function optionalCrypt(buffer:Buffer) {
73-
if((await safeStorage).isEncryptionAvailable())
74-
return (await safeStorage).encryptString(buffer.toString());
75-
return buffer.toString();
76-
}
7786
const options = {
7887
title: "Select a Discord theme to add to WebCord",
7988
properties: ["multiSelections", "openFile"],
@@ -82,18 +91,16 @@ async function addStyle(window?:Electron.BrowserWindow) {
8291
]
8392
} satisfies Electron.OpenDialogOptions;
8493
const result = window
85-
? await (await electron).dialog.showOpenDialog(window, options)
86-
: await (await electron).dialog.showOpenDialog(options);
94+
? await (await e).dialog.showOpenDialog(window, options)
95+
: await (await e).dialog.showOpenDialog(options);
8796
if(result.canceled)
8897
return;
8998
const promises:Promise<unknown>[] = [];
90-
for (const path of result.filePaths) {
91-
const data = fs.then(fs => fs.readFile(path)).then(path => optionalCrypt(path));
92-
electron.then(async electron => [electron.app, await pth] as const).then(([app,pth]) => {
93-
const out = pth.resolve(app.getPath("userData"),"Themes", pth.basename(path, ".css"));
94-
if(pth.resolve(path) === out) return;
95-
promises.push(data.then(async data => (await fs).writeFile(out, data)));
96-
}).catch((err:unknown) => { throw err; });
99+
for await (const path of result.filePaths) {
100+
const data = encrypt(await (await fs).readFile(path));
101+
const out = (await pth).resolve((await e).app.getPath("userData"),"Themes", (await pth).basename(path, ".css"));
102+
if((await pth).resolve(path) === out) return;
103+
promises.push((await fs).writeFile(out, await data));
97104
}
98105
await Promise.all(promises);
99106
}
@@ -109,52 +116,38 @@ async function loadStyles(webContents:Electron.WebContents) {
109116
app,
110117
fsp,
111118
fs,
112-
pth,
113-
safeStorage
119+
pth
114120
] = [
115121
import("electron/main").then(mod => mod.app),
116122
import("fs/promises"),
117123
import("fs"),
118124
import("path"),
119-
safeStoragePromise
120125
];
121126
const stylesDir = (await pth).resolve((await app).getPath("userData"),"Themes");
122127
if(!(await fs).existsSync(stylesDir)) (await fs).mkdirSync(stylesDir, {recursive:true});
123-
const callback = async () => {
128+
async function callback() {
124129
// Read CSS module directories.
125130
const {readdir,readFile} = (await fsp).default;
126-
const paths = await readdir(stylesDir);
127131
const promises:Promise<[string,Buffer]>[] = [];
128-
for(const path of paths) {
129-
const index = resolve(stylesDir,path);
130-
fs.then(fs => {
131-
if (!path.endsWith(".theme.css") && fs.statSync(index).isFile())
132-
promises.push(Promise.all([index,readFile(index)]));
133-
}).catch((error:unknown) => { throw error; });
132+
for await(const path of await readdir(stylesDir)) {
133+
const index = (await pth).resolve(stylesDir,path);
134+
console.log(index);
135+
if (!path.endsWith(".theme.css") && (await fs).statSync(index).isFile())
136+
promises.push(Promise.all([index,readFile(index)]));
134137
}
135-
const resArray = await Promise.all(promises);
136138
const themeIDs:Promise<string>[] = [];
137-
const decrypt = async (string:Buffer) => {
138-
if(!(await safeStorage).isEncryptionAvailable() && !(await app).isReady() && process.platform !== "darwin")
139-
await (await app).whenReady();
140-
if(!(await safeStorage).isEncryptionAvailable())
141-
return string.toString();
142-
if(!string.toString("utf-8").includes("�"))
143-
throw new Error("One of loaded styles was not encrypted and could not be loaded.");
144-
return (await safeStorage).decryptString(string);
145-
};
146-
for(const res of resArray)
139+
for await(const res of await Promise.all(promises))
147140
themeIDs.push(
148-
decrypt(res[1])
141+
decrypt(await app,res[1])
149142
.then(data => parseImports(data,[res[0]]))
150143
/* Makes all CSS variables and color / background properties
151144
* `!important` (this should fix most styles).
152145
*/
153146
.then(data => data.replaceAll(/((?:--|color|background)[^:;{]*:(?![^:]*?!important)[^:;]*)(;|})/g, "$1 !important$2"))
154147
.then(data => webContents.insertCSS(data))
155148
);
156-
return themeIDs;
157-
};
149+
return Promise.all(themeIDs);
150+
}
158151
(await fs).watch(stylesDir).once("change", () => {
159152
webContents.reload();
160153
});

0 commit comments

Comments
 (0)