Skip to content

Commit 9a8ac5b

Browse files
committed
Some code enhancements
1 parent 97f918b commit 9a8ac5b

4 files changed

Lines changed: 35 additions & 40 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
node_modules
22
dist
33
package-lock.json
4-
rcc/qresource/
54
*.rcc

js/mainRenderer.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,13 @@ ipcRenderer.on("populate-list", (event, images) => {
4141
list.removeChild(list.firstChild);
4242
}
4343

44-
for (let index = 0; index < images.length; ++index) {
45-
const image = images[index];
46-
44+
for (const [index, image] of images.entries()) {
4745
if (!image.isImage) {
4846
continue;
4947
}
5048

51-
let btn = document.createElement("button");
52-
btn.innerText = `${image.name}`;
49+
const btn = document.createElement("button");
50+
btn.innerText = image.name;
5351
btn.id = `btn-${index}`;
5452
btn.onclick = (event) => {
5553
if (focused == event.target) {
@@ -65,7 +63,7 @@ ipcRenderer.on("populate-list", (event, images) => {
6563
ipcRenderer.send("get-image-data", index);
6664
};
6765

68-
let img = document.createElement("img");
66+
const img = document.createElement("img");
6967
img.className = "miniature";
7068
img.src = `data:image/png;base64,${Buffer.from(image.data).toString(
7169
"base64"

js/reader.js

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,14 @@ const { dialog, BrowserWindow, app } = require("electron");
22
const path = require("path");
33
const util = require("util");
44
const execFile = util.promisify(require("child_process").execFile);
5-
6-
const Promise = require("bluebird");
7-
const fs = Promise.promisifyAll(require("fs-extra"));
5+
const fs = require("fs-extra");
86

97
let loadedFilePath = null;
108
let images = [];
119

12-
const getResourcePath = () => {
13-
if (app.isPackaged) {
14-
return process.resourcesPath;
15-
} else {
16-
return ".";
17-
}
18-
};
10+
const imageExt = [".png", ".jpg"];
11+
12+
const resourcePath = app.isPackaged ? process.resourcesPath : ".";
1913

2014
const getFiles = async (path = "./") => {
2115
const entries = await fs.readdir(path, { withFileTypes: true });
@@ -31,30 +25,35 @@ const getFiles = async (path = "./") => {
3125
};
3226

3327
const loadRcc = async (filePath) => {
34-
const localPath = `${path.resolve(getResourcePath(), "rcc")}`;
28+
const localPath = `${path.resolve(resourcePath, "rcc")}`;
3529

3630
// clear previous images
3731
images = [];
3832

3933
// delete res directory
4034
try {
41-
await fs.rmdir(`${localPath}/qresource`, { recursive: true });
35+
await fs.rmdir(path.join(localPath, "qresource"), { recursive: true });
4236
} catch {}
4337

44-
await fs.copyFile(filePath, `${localPath}/res.rcc`);
38+
await fs.copyFile(filePath, path.join(localPath, "res.rcc"));
4539

46-
const result = await execFile(`${localPath}/rcc.exe`, ["--reverse"], {
40+
await execFile(path.join(localPath, "rcc.exe"), ["--reverse"], {
4741
cwd: `${localPath}/`,
4842
});
4943

5044
// get directory content
51-
const files = await getFiles(`${localPath}/qresource/res/res.rcc`);
45+
const files = await getFiles(
46+
path.join(localPath, "qresource", "res", "res.rcc")
47+
);
48+
5249
for (const file of files) {
53-
const ext = path.extname(file.path);
5450
images.push({
5551
name: path.parse(file.name).name,
56-
path: path.relative(`${localPath}/qresource/res/res.rcc`, file.path),
57-
isImage: ext === ".png" || ext === ".jpg",
52+
path: path.relative(
53+
path.join(localPath, "qresource", "res", "res.rcc"),
54+
file.path
55+
),
56+
isImage: imageExt.includes(path.extname(file.path)),
5857
data: Buffer.from(await fs.readFile(file.path, "binary"), "binary"),
5958
});
6059
}
@@ -63,23 +62,23 @@ const loadRcc = async (filePath) => {
6362
images.sort((a, b) => a.name.localeCompare(b.name));
6463

6564
// cleanup
66-
await fs.rmdir(`${localPath}/qresource`, { recursive: true });
67-
await fs.rm(`${localPath}/res.rcc`);
65+
await fs.rmdir(path.join(localPath, "qresource"), { recursive: true });
66+
await fs.rm(path.join(localPath, "res.rcc"));
6867

6968
loadedFilePath = filePath;
7069

7170
BrowserWindow.getAllWindows()[0].webContents.send("populate-list", images);
7271
};
7372

7473
const extractToPng = async (directoryPath) => {
75-
if (images.length === 0) {
74+
if (!images.length) {
7675
dialog.showErrorBox("Error", "Nothing to extract.");
7776
return;
7877
}
7978

8079
for (const image of images) {
8180
if (image.isImage) {
82-
await fs.outputFileAsync(`${directoryPath}/${image.path}`, image.data);
81+
await fs.outputFile(`${directoryPath}/${image.path}`, image.data);
8382
}
8483
}
8584

@@ -89,14 +88,12 @@ const extractToPng = async (directoryPath) => {
8988
});
9089
};
9190

92-
const saveRcc = async (filePath) => {
91+
const saveRcc = async (filePath = loadedFilePath) => {
9392
if (images.length === 0) {
9493
return;
9594
}
9695

97-
filePath = filePath || loadedFilePath;
98-
99-
const localPath = `${path.resolve(getResourcePath(), "rcc")}`;
96+
const localPath = path.resolve(resourcePath, "rcc");
10097

10198
// create .qrc file
10299
let data = `<!DOCTYPE RCC><RCC version="1.0">\n<qresource>\n`;
@@ -107,15 +104,15 @@ const saveRcc = async (filePath) => {
107104

108105
data += `</qresource>\n</RCC>`;
109106

110-
await fs.outputFileAsync(`${localPath}/res/res.qrc`, data);
107+
await fs.outputFile(path.join(localPath, "res", "res.qrc"), data);
111108

112109
// dump images
113110
for (const image of images) {
114-
await fs.outputFileAsync(`${localPath}/res/${image.path}`, image.data);
111+
await fs.outputFile(path.join(localPath, "res", image.path), image.data);
115112
}
116113

117-
const result = await execFile(
118-
`${localPath}/rcc.exe`,
114+
await execFile(
115+
path.join(localPath, "rcc.exe"),
119116
[
120117
"--format-version",
121118
"1",
@@ -129,10 +126,12 @@ const saveRcc = async (filePath) => {
129126
}
130127
);
131128

132-
await fs.move("./rcc/res/res_output.rcc", `${filePath}`, { overwrite: true });
129+
await fs.move("./rcc/res/res_output.rcc", filePath, { overwrite: true });
133130

134131
// cleanup
135-
await fs.rmdir(`${localPath}/res`, { recursive: true });
132+
await fs.rmdir(path.join(localPath, "res"), {
133+
recursive: true,
134+
});
136135

137136
dialog.showMessageBox(null, {
138137
message: `Rcc saved successfully.`,

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
}
3636
},
3737
"dependencies": {
38-
"bluebird": "^3.7.2",
3938
"fs-extra": "^10.0.0"
4039
}
4140
}

0 commit comments

Comments
 (0)