-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
124 lines (99 loc) · 3.03 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
"use strict";
const { app, BrowserWindow, ipcMain, dialog, globalShortcut, nativeImage, Menu, Template } = require("electron");
const path = require("node:path");
const fs = require("fs");
const { setupTitlebar, attachTitlebarToWindow } = require("custom-electron-titlebar/main");
const os = require("os");
app.setPath("userData", path.join(os.tmpdir(), "electron-file-editor"));
setupTitlebar();
let win;
//threw an error causing it to break
/* require("electron-reloader")(__dirname, {
electron: path.join(__dirname, "node_modules", "bin", "electron"),
hardResetMethod: "exit"
}); */
const createWindow = () => {
win = new BrowserWindow({
show: false,
titleBarStyle: "hidden",
titleBarOverlay: true,
webPreferences: {
//sandbox: false,
preload: path.join(__dirname, "preload.js"),
contextIsolation: true,
enableRemoteModule: true,
nodeIntegration: true,
}
});
//const menu = Menu.buildFromTemplate(template);
//Menu.setApplicationMenu(menu);
attachTitlebarToWindow(win);
win.once("ready-to-show", () => {
win.maximize();
win.show();
})
win.loadFile("index.html");
}
try {
require("electron-reloader")(module);
} catch (err) {
console.error("Failed to load electron-reloader:", err);
}
app.on("ready", createWindow);
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
ipcMain.on("find-and-replace", (event, args) => {
const { files, searchValue, replaceValue } = args;
findAndReplaceInFiles(files, searchValue, replaceValue);
});
function findAndReplaceInFiles(files, searchValue, replaceValue) {
files.forEach((file) => {
const filePath = path.resolve(file);
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error(`Error reading file ${filePath}:`, err);
return;
}
if (data.includes(searchValue)) {
const updatedContent = data.split(searchValue).join(replaceValue);
fs.writeFile(filePath, updatedContent, "utf8", (err) => {
if (err) {
console.error(`Error writing to file ${filePath}:`, err);
return;
}
console.log(`Successfully updated ${filePath}`);
});
} else {
console.log(`Search value not found in ${filePath}`);
}
});
});
}
ipcMain.handle("open-file-dialog", async () => {
const result = await dialog.showOpenDialog({
properties: ["openFile", "multiSelections"]
});
if (result.canceled) {
return null;
} else {
return result.filePaths;
}
});
app.whenReady().then(() => {
const ret = globalShortcut.register("f5", () => {
console.log("f5 is pressed");
win.reload();
})
globalShortcut.register("CommandOrControl+R", () => {
console.log("CommandOrControl+R is pressed");
win.reload();
});
})