-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
187 lines (150 loc) · 5.13 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
const {
electron,
app,
BrowserWindow,
ipcMain
} = require('electron');
const url = require('url');
const path = require('path');
const fs = require('fs');
const { resolve } = require('path');
const { DefaultDeserializer } = require('v8');
const __devTools = path.join(__dirname, 'development_tools');
let appWindow;
const createWindow = () => {
appWindow = new BrowserWindow({
width: 990,
minWidth: 990,
height: 520,
minHeight: 520,
frame: false,
icon: path.join(__dirname, 'icon.ico'),
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'src/scripts/preload.js'),
},
});
appWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
appWindow.setMenuBarVisibility(false);
}
const isDevToolWithValidConfig = async (__devTool) => {
let __dtConfig = path.join(__devTool, 'dt_config.json');
// Get and validate "dt_config.json" file's content
let data = await fs.promises.readFile(__dtConfig, 'utf8')
const dtConfig = JSON.parse(data);
if ( // If valid keys are present
dtConfig.hasOwnProperty('title')
&& dtConfig.hasOwnProperty('icon')
&& dtConfig.hasOwnProperty('index')
&& dtConfig.hasOwnProperty('menu')
&& dtConfig.hasOwnProperty('body_page')
&& dtConfig.hasOwnProperty('footer_page')
&& dtConfig.hasOwnProperty('version')
&& dtConfig.hasOwnProperty('author')
)
{ // Return false when data is empty or exceeds 50 characters
for (let key in dtConfig)
{
let keyValue = dtConfig[key];
if (
keyValue === ''
|| keyValue === null
|| keyValue.length >= 50
)
{ return false }
}
} else { return false }
// Validate file format for
// keys: icon, index, menu, body_page & footer_page
if (
!/.+?(\.png)/g.test(dtConfig.icon)
|| !/.+?(\.html)/g.test(dtConfig.index)
|| !/.+?(\.html)/g.test(dtConfig.menu)
|| !/.+?(\.html)/g.test(dtConfig.body_page)
|| !/.+?(\.html)/g.test(dtConfig.footer_page)
)
{ return false }
// Return false if necessary files do not exist
// at specified directories:
if (
!fs.existsSync(path.join(__devTool, dtConfig.icon))
|| !fs.existsSync(path.join(__devTool, dtConfig.index))
|| !fs.existsSync(path.join(__devTool, dtConfig.menu))
|| !fs.existsSync(path.join(__devTool, dtConfig.body_page))
|| !fs.existsSync(path.join(__devTool, dtConfig.footer_page))
)
{ return false }
return true;
}
const importDevTools = async () => {
// Read files from development_tools directory
let files = await fs.promises.readdir(__devTools)
files.map(async (file) =>
{
// Check If file is a directory
let __devTool = path.join(__devTools, file);
let stats = fs.statSync(__devTool);
if (!stats.isFile())
{
// Read files from development_tool's directory
let files = await fs.promises.readdir(__devTool)
files.map(async (file) =>
{
// If file from development_tool's direcotry has valid "dt_config.json"
let __dtFile = path.join(__devTool, file);
let stats = fs.statSync(__dtFile);
let isConfigValid = await isDevToolWithValidConfig(__devTool);
if (stats.isFile() && file === 'dt_config.json' && isConfigValid === true)
{
// Read development_tool's configuration
let __dtConfig = path.join(__devTool, 'dt_config.json');
let data = await fs.promises.readFile(__dtConfig, 'utf8')
let dtConfig = await JSON.parse(data);
let __dtMenu = path.join(__devTool, dtConfig.menu);
const dtMenuData = await fs.promises.readFile(__dtMenu, 'utf8');
const appMenuData = await fs.promises.readFile(path.join(__dirname, 'menu.html'), 'utf8');
// Build and append development tool component
appWindow.webContents.send('importDevTool', {
__devTool,
dtConfig,
dtMenuData,
appMenuData,
});
}
});
}
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow();
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
ipcMain.on('minimize', () => {
BrowserWindow.getFocusedWindow().minimize();
});
ipcMain.on('maximize', () => {
let window = BrowserWindow.getFocusedWindow();
window.isMaximized() ? window.unmaximize() : window.maximize();
});
ipcMain.on('terminate', () => {
appWindow.close();
});
ipcMain.on('importDevTools', importDevTools);
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});