Skip to content

Commit 688198d

Browse files
authored
Merge pull request #41 from tsully/refactor
Code/Typescript error cleanup and new UI for project management functionality
2 parents 4a9e80b + 67991ea commit 688198d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+948
-1191
lines changed

app/electron/main.js

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@ const {
44
dialog,
55
BrowserWindow,
66
session,
7-
ipcMain,
8-
Menu
7+
ipcMain
98
} = require('electron');
9+
1010
// The splash screen is what appears while the app is loading
1111
const { initSplashScreen, OfficeTemplate } = require('electron-splashscreen');
1212
const { resolve } = require('app-root-path');
13+
const Protocol = require('./protocol');
14+
const MenuBuilder = require('./menu');
15+
const path = require('path');
16+
1317
const {
1418
default: installExtension,
1519
REACT_DEVELOPER_TOOLS
1620
} = require('electron-devtools-installer');
1721
const debug = require('electron-debug');
1822

19-
const Protocol = require('./protocol');
20-
// menu from another file to modularize the code
21-
const MenuBuilder = require('./menu');
22-
23-
const path = require('path');
24-
// const fs = require('fs');
25-
26-
console.log('NODE ENV is ', process.env.NODE_ENV);
2723
const isDev = process.env.NODE_ENV === 'development';
2824
const port = 8080;
2925
const selfHost = `http://localhost:${port}`;
3026

27+
// main.js is what controls the lifecycle of the electron applicaton
28+
3129
// Keep a global reference of the window object, if you don't, the window will
3230
// be closed automatically when the JavaScript object is garbage collected.
3331
let win;
3432
let menuBuilder;
3533

34+
// function to create a new broswer window
35+
// this function will be called when Electron has initialized itself
3636
async function createWindow() {
3737
if (isDev) {
3838
await installExtension([REACT_DEVELOPER_TOOLS])
@@ -52,12 +52,10 @@ async function createWindow() {
5252
height: 1080,
5353
// window title
5454
title: `ReacType`,
55+
// the browser window will not display intiially as it's loading
56+
// once the browser window renders, a function is called below that hides the splash screen and displays the browser window
5557
show: false,
56-
icon: path.join(__dirname, '../src/public/icons/png/256x256.png'),
57-
win: {
58-
icon: path.join(__dirname, '../src/public/icons/win/icon.ico'),
59-
target: ['portable']
60-
},
58+
// icon: path.join(__dirname, '../src/public/icons/png/256x256.png'),
6159
webPreferences: {
6260
zoomFactor: 0.7,
6361
// enable devtools
@@ -79,10 +77,7 @@ async function createWindow() {
7977
}
8078
});
8179

82-
console.log('PATH is ', resolve('/'));
83-
84-
//splash screen deets
85-
// TODO: splash screen logo/icon aren't loading in dev mode
80+
// Splash screen that appears while loading
8681
const hideSplashscreen = initSplashScreen({
8782
mainWindow: win,
8883
icon: resolve('app/src/public/icons/png/64x64.png'),
@@ -112,9 +107,9 @@ async function createWindow() {
112107
hideSplashscreen();
113108
});
114109

115-
// Only do these things when in development
110+
// automatically open DevTools when opening the app
111+
// Note: devtools is creating many errors in the logs at the moment but can't figure out how to resolve the issue
116112
if (isDev) {
117-
// automatically open DevTools when opening the app
118113
win.webContents.once('dom-ready', () => {
119114
debug();
120115
win.webContents.openDevTools();
@@ -129,6 +124,10 @@ async function createWindow() {
129124
win = null;
130125
});
131126

127+
menuBuilder = MenuBuilder(win, 'ReacType');
128+
menuBuilder.buildMenu();
129+
130+
// Removed this security feature for now since it's not being used
132131
// https://electronjs.org/docs/tutorial/security#4-handle-session-permission-requests-from-remote-content
133132
// TODO: is this the same type of sessions that have in react type
134133
// Could potentially remove this session capability - it appears to be more focused on approving requests from 3rd party notifications
@@ -163,12 +162,8 @@ async function createWindow() {
163162
});
164163
}
165164
});
166-
167-
menuBuilder = MenuBuilder(win, 'ReacType');
168-
menuBuilder.buildMenu();
169165
}
170166

171-
// TODO: unclear of whether this is necsssary or not. Looks like a security best practice but will likely introduce complications
172167
// Needs to be called before app is ready;
173168
// gives our scheme access to load relative files,
174169
// as well as local storage, cookies, etc.
@@ -197,8 +192,6 @@ app.on('window-all-closed', () => {
197192
if (process.platform !== 'darwin') {
198193
app.quit();
199194
} else {
200-
// TODO: remove i18nextbackend
201-
// i18nextBackend.clearMainBindings(ipcMain);
202195
ContextMenu.clearMainBindings(ipcMain);
203196
}
204197
});
@@ -214,7 +207,6 @@ app.on('activate', () => {
214207
// https://electronjs.org/docs/tutorial/security#12-disable-or-limit-navigation
215208
// limits navigation within the app to a whitelisted array
216209
// redirects are a common attack vector
217-
// TODO: add github to the validOrigins whitelist array
218210

219211
// after the contents of the webpage are rendered, set up event listeners on the webContents
220212
app.on('web-contents-created', (event, contents) => {

app/electron/preload.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// contextBridge is what allows context to be translated between the main process and the render process
2-
// ipcRenderer sends messsages between render process and the main process
3-
const { contextBridge, ipcRenderer } = require('electron');
2+
const { contextBridge } = require('electron');
43
const { existsSync, writeFileSync, mkdirSync, writeFile } = require('fs');
54
const formatCode = require('./preloadFunctions/format');
65
const {
@@ -15,24 +14,24 @@ const {
1514
reload
1615
} = require('./preloadFunctions/cookies');
1716

18-
// Expose protected methods that allow the renderer process to use
19-
// the ipcRenderer without exposing the entire object
17+
// Expose protected methods that allow the renderer process to use select node methods
18+
// without exposing all node functionality. This is a critical security feature
2019
// 'mainWorld" is the context that the main renderer runs in
2120
// with contextIsolation on (see webpreferences on main.js), this preload script runs isolated
2221
// "api" is the key that injects the api into the window
23-
// to access these keys in the renderer process, we'll do window.key
22+
// to access these keys in the renderer process, we'll do window.api
2423
// the api object (second arg) can contain functions, strings, bools, numbers, arrays, obects in value
2524
// data primitives sent on the bridge are immutable and changes in one context won't carry over to another context
2625
contextBridge.exposeInMainWorld('api', {
27-
formatCode: formatCode,
28-
chooseAppDir: chooseAppDir,
29-
addAppDirChosenListener: addAppDirChosenListener,
30-
removeAllAppDirChosenListeners: removeAllAppDirChosenListeners,
31-
existsSync: existsSync,
32-
writeFileSync: writeFileSync,
33-
mkdirSync: mkdirSync,
34-
writeFile: writeFile,
35-
setCookie: setCookie,
36-
getCookie: getCookie,
37-
delCookie: delCookie
26+
formatCode,
27+
chooseAppDir,
28+
addAppDirChosenListener,
29+
removeAllAppDirChosenListeners,
30+
existsSync,
31+
writeFileSync,
32+
mkdirSync,
33+
writeFile,
34+
setCookie,
35+
getCookie,
36+
delCookie
3837
});

app/electron/preloadFunctions/format.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ const { format } = require('prettier');
22

33
// format code using prettier
44
// this format function is used in the render process to format the code in the code preview
5+
// the format function is defined in the main process because it needs to access node functionality ('fs')
56
const formatCode = code => {
67
return format(code, {
78
singleQuote: true,
89
trailingComma: 'es5',
910
bracketSpacing: true,
1011
jsxBracketSameLine: true,
11-
// parser: 'babel'
1212
parser: 'typescript'
1313
});
1414
};

app/electron/protocol.js

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,44 @@
1-
/*
2-
Reasonably Secure Electron
3-
Copyright (C) 2019 Bishop Fox
4-
This program is free software; you can redistribute it and/or
5-
modify it under the terms of the GNU General Public License
6-
as published by the Free Software Foundation; either version 2
7-
of the License, or (at your option) any later version.
8-
This program is distributed in the hope that it will be useful,
9-
but WITHOUT ANY WARRANTY; without even the implied warranty of
10-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11-
GNU General Public License for more details.
12-
You should have received a copy of the GNU General Public License
13-
along with this program; if not, write to the Free Software
14-
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15-
-------------------------------------------------------------------------
16-
Implementing a custom protocol achieves two goals:
17-
1) Allows us to use ES6 modules/targets for Angular
18-
2) Avoids running the app in a file:// origin
19-
*/
201

21-
const fs = require("fs");
22-
const path = require("path");
2+
// Implementing a custom protocol achieves two goals:
3+
// 1) Allows us to use ES6 modules/targets for Angular
4+
// 2) Avoids running the app in a file:// origin
235

24-
const DIST_PATH = path.join(__dirname, "../../app/dist");
25-
const scheme = "app";
6+
const fs = require('fs');
7+
const path = require('path');
8+
9+
const DIST_PATH = path.join(__dirname, '../../app/dist');
10+
const scheme = 'app';
2611

2712
const mimeTypes = {
28-
".js": "text/javascript",
29-
".mjs": "text/javascript",
30-
".html": "text/html",
31-
".htm": "text/html",
32-
".json": "application/json",
33-
".css": "text/css",
34-
".svg": "application/svg+xml",
35-
".ico": "image/vnd.microsoft.icon",
36-
".png": "image/png",
37-
".jpg": "image/jpeg",
38-
".map": "text/plain"
13+
'.js': 'text/javascript',
14+
'.mjs': 'text/javascript',
15+
'.html': 'text/html',
16+
'.htm': 'text/html',
17+
'.json': 'application/json',
18+
'.css': 'text/css',
19+
'.svg': 'application/svg+xml',
20+
'.ico': 'image/vnd.microsoft.icon',
21+
'.png': 'image/png',
22+
'.jpg': 'image/jpeg',
23+
'.map': 'text/plain'
3924
};
4025

4126
function charset(mimeType) {
42-
return [".html", ".htm", ".js", ".mjs"].some((m) => m === mimeType)
43-
? "utf-8"
27+
return ['.html', '.htm', '.js', '.mjs'].some(m => m === mimeType)
28+
? 'utf-8'
4429
: null;
4530
}
4631

4732
function mime(filename) {
48-
const type = mimeTypes[path.extname(`${filename || ""}`).toLowerCase()];
33+
const type = mimeTypes[path.extname(`${filename || ''}`).toLowerCase()];
4934
return type ? type : null;
5035
}
5136

5237
function requestHandler(req, next) {
5338
const reqUrl = new URL(req.url);
5439
let reqPath = path.normalize(reqUrl.pathname);
55-
if (reqPath === "/") {
56-
reqPath = "/index-prod.html";
40+
if (reqPath === '/') {
41+
reqPath = '/index-prod.html';
5742
}
5843
const reqFilename = path.basename(reqPath);
5944
fs.readFile(path.join(DIST_PATH, reqPath), (err, data) => {

app/src/components/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useReducer, useEffect } from 'react';
1+
import React, { useReducer, useEffect } from 'react';
22
import '../public/styles/style.css';
33
import '../public/styles/styleNew.css';
44
import { DndProvider } from 'react-dnd';

0 commit comments

Comments
 (0)