Skip to content

Commit c993b89

Browse files
authored
Merge pull request #27 from oslabs-beta/sean
Possible final merge?
2 parents 7c793b1 + 7c6fa88 commit c993b89

File tree

19 files changed

+255
-181
lines changed

19 files changed

+255
-181
lines changed

app/.electron/main.js renamed to app/.electron/main.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
1-
const electron = require('electron');
2-
31
/*
42
@description: main.js is what controls the lifecycle of the electron application from initialization to termination.
53
@actions: codes for Github Oauth has been commented out because of lack of functionality.
64
*/
75
require('dotenv').config();
86
const { DEV_PORT } = require('../../config.js');
97
const path = require('path');
10-
const {
8+
import {
119
app,
1210
protocol,
13-
dialog,
1411
BrowserWindow,
1512
session,
16-
ipcMain
17-
} = require('electron');
13+
ipcMain,
14+
dialog
15+
} from 'electron';
1816
// The splash screen is what appears while the app is loading
1917
// const { initSplashScreen, OfficeTemplate } = require('electron-splashscreen');
20-
const { resolve } = require('app-root-path');
21-
18+
import { resolve } from 'app-root-path';
2219
// to install react dev tool extension
2320
// const {
2421
// default: installExtension,
2522
// REACT_DEVELOPER_TOOLS
2623
// } = require('electron-devtools-installer');
27-
const debug = require('electron-debug');
28-
24+
import debug from 'electron-debug';
2925
// import custom protocol in protocol.js
30-
const Protocol = require('./protocol');
26+
import Protocol from './protocol';
3127
// menu from another file to modularize the code
32-
const MenuBuilder = require('./menu');
28+
29+
import MenuBuilder from './menu';
3330

3431
// mode that the app is running in
3532
const isDev =
@@ -39,8 +36,8 @@ const selfHost = `http://localhost:${port}`;
3936

4037
// Keep a global reference of the window object, if you don't, the window will
4138
// be closed automatically when the JavaScript object is garbage collected.
42-
let win;
43-
let menuBuilder;
39+
let win: BrowserWindow | null;
40+
let menuBuilder: any;
4441

4542
// function to create a new browser window
4643
// this function will be called when Electron has initialized itself
@@ -108,15 +105,15 @@ async function createWindow() {
108105

109106
// load page once window is loaded
110107
win.once('ready-to-show', () => {
111-
win.show();
108+
win!.show();
112109
});
113110

114111
// automatically open DevTools when opening the app
115112
// Note: devtools is creating many errors in the logs at the moment but can't figure out how to resolve the issue
116113
if (isDev) {
117114
win.webContents.once('dom-ready', () => {
118115
debug();
119-
win.webContents.openDevTools();
116+
win!.webContents.openDevTools();
120117
});
121118
}
122119

@@ -128,7 +125,7 @@ async function createWindow() {
128125
app.quit();
129126
});
130127

131-
menuBuilder = MenuBuilder(win, 'ReacType');
128+
menuBuilder = MenuBuilder(win!, 'ReacType');
132129
menuBuilder.buildMenu();
133130

134131
// Removed this security feature for now since it's not being used
@@ -141,7 +138,7 @@ async function createWindow() {
141138
ses
142139
.fromPartition(partition)
143140
.setPermissionRequestHandler((webContents, permission, callback) => {
144-
const allowedPermissions = []; // Full list here: https://developer.chrome.com/extensions/declare_permissions#manifest
141+
const allowedPermissions: string[] = []; // Full list here: https://developer.chrome.com/extensions/declare_permissions#manifest
145142

146143
if (allowedPermissions.includes(permission)) {
147144
callback(true); // Approve permission request
@@ -196,7 +193,7 @@ app.on('ready', createWindow);
196193

197194
// Quit when all windows are closed.
198195
app.on('window-all-closed', () => {
199-
win.webContents.executeJavaScript('window.localStorage.clear();');
196+
win!.webContents.executeJavaScript('window.localStorage.clear();');
200197
app.quit();
201198
});
202199

@@ -343,7 +340,7 @@ ipcMain.on('choose_app_dir', (event) => {
343340
// dialog displays the native system's dialogue for selecting files
344341
// once a directory is chosen send a message back to the renderer with the path of the directory
345342
dialog
346-
.showOpenDialog(win, {
343+
.showOpenDialog(win!, {
347344
properties: ['openDirectory'],
348345
buttonLabel: 'Export'
349346
})
@@ -419,7 +416,7 @@ ipcMain.on('github', (event) => {
419416

420417
if (code || error) {
421418
// Close the browser if code found or error
422-
authWindow.destroy();
419+
github.destroy();
423420
}
424421

425422
// If there is a code, proceed to get token from github
@@ -464,9 +461,9 @@ ipcMain.on('github', (event) => {
464461
message: 'Github Oauth Successful!'
465462
});
466463
github.close();
467-
win.webContents
464+
win!.webContents
468465
.executeJavaScript(`window.localStorage.setItem('ssid', '${ssid}')`)
469-
.then((result) => win.loadURL(`${redirectUrl}`))
466+
.then((result) => win!.loadURL(`${redirectUrl}`))
470467
.catch((err) => console.log(err));
471468
}
472469
});

app/.electron/menu.js renamed to app/.electron/menu.ts

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
const { Menu } = require('electron');
2-
const { BrowserWindow } = require('electron');
1+
import { Menu, BrowserWindow, Shell } from 'electron';
32
const isMac = process.platform === 'darwin';
4-
const Protocol = require('./protocol');
3+
import Protocol from './protocol';
54
/*
65
DESCRIPTION: This file generates an array containing a menu based on the operating system the user is running.
76
menuBuilder: The entire file is encompassed in menuBuilder. Ultimately, menuBuilder returns a function called
@@ -25,14 +24,14 @@ defaultTemplate: returns an array of submenus (each an array)
2524
*/
2625

2726
// Create a template for a menu and create menu using that template
28-
var MenuBuilder = function(mainWindow, appName) {
27+
var MenuBuilder = function (mainWindow, appName) {
2928
// https://electronjs.org/docs/api/menu#main-process
3029
// "roles" are predefined by Electron and used for standard actions
3130
// https://www.electronjs.org/docs/api/menu-item
3231
// you can also create custom menu items with their own "on click" functionality if you need to
3332
// different roles are available between mac and windows
3433

35-
const openTutorial = () => {
34+
function openTutorial(): void {
3635
const tutorial = new BrowserWindow({
3736
width: 1180,
3837
height: 900,
@@ -54,47 +53,28 @@ var MenuBuilder = function(mainWindow, appName) {
5453
tutorial.loadURL(`${Protocol.scheme}://rse/index-prod.html#/tutorial`);
5554
}
5655
tutorial.show();
57-
};
56+
}
5857

59-
let defaultTemplate = function() {
60-
return [
58+
const defaultTemplate= (): Electron.MenuItemConstructorOptions[] => [
6159
...(isMac
6260
? [
6361
{
6462
// on Mac, the first menu item name should be the name of the app
6563
label: appName,
6664
submenu: [
67-
{
68-
role: 'about'
69-
},
70-
{
71-
type: 'separator'
72-
},
73-
{
74-
role: 'services'
75-
},
76-
{
77-
type: 'separator'
78-
},
79-
{
80-
role: 'hide'
81-
},
82-
{
83-
role: 'hideothers'
84-
},
85-
{
86-
role: 'unhide'
87-
},
88-
{
89-
type: 'separator'
90-
},
91-
{
92-
role: 'quit'
93-
}
94-
]
95-
}
65+
{role: 'about'},
66+
{type: 'separator'},
67+
{role: 'services'},
68+
{type: 'separator'},
69+
{role: 'hide'},
70+
{role: 'hideothers'},
71+
{role: 'unhide'},
72+
{type: 'separator'},
73+
{role: 'quit'}
74+
] as Electron.MenuItemConstructorOptions[],
75+
} ,
9676
]
97-
: []),
77+
: []),Electron.MenuItemConstructorOptions[],
9878
{
9979
label: 'File',
10080
submenu: [
@@ -250,11 +230,13 @@ var MenuBuilder = function(mainWindow, appName) {
250230
]
251231
}
252232
];
253-
};
233+
234+
return template;
235+
}
254236

255237
// constructs menu from default template
256238
return {
257-
buildMenu: function() {
239+
buildMenu: function () {
258240
const menu = Menu.buildFromTemplate(defaultTemplate());
259241
Menu.setApplicationMenu(menu);
260242

@@ -263,4 +245,4 @@ var MenuBuilder = function(mainWindow, appName) {
263245
};
264246
};
265247

266-
module.exports = MenuBuilder;
248+
export { MenuBuilder };

app/.electron/preload.js renamed to app/.electron/preload.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
// contextBridge is what allows context to be translated between the main process and the render process
2-
const { contextBridge } = require('electron');
3-
const { existsSync, writeFileSync, mkdirSync, writeFile } = require('fs');
4-
const formatCode = require('./preloadFunctions/format');
5-
const {
2+
import { contextBridge } from 'electron';
3+
import { existsSync, writeFileSync, mkdirSync, writeFile } from 'fs';
4+
import formatCode from './preloadFunctions/format';
5+
import {
66
chooseAppDir,
77
addAppDirChosenListener,
88
removeAllAppDirChosenListeners
9-
} = require('./preloadFunctions/chooseAppDir');
10-
const {
9+
} from './preloadFunctions/chooseAppDir';
10+
import {
1111
setCookie,
1212
getCookie,
1313
delCookie,
1414
github,
1515
tutorial
16-
} = require('./preloadFunctions/cookies');
16+
} from './preloadFunctions/cookies';
1717

1818
/*
1919
DESCRIPTION: This file appears to limit the node methods the Electron app can access.

app/.electron/preloadFunctions/chooseAppDir.js

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { ipcRenderer, IpcRendererEvent } from 'electron';
2+
import { type } from 'os';
3+
4+
type AppDirSelectedCallback = (path: string) => void;
5+
6+
const chooseAppDir = (): void => {
7+
ipcRenderer.send('choose_app_dir');
8+
};
9+
10+
// once an app directory is chosen, the main process will send an "app_dir_selected" event
11+
// when this event occurs, exucte the callback passed in by the user
12+
const addAppDirChosenListener = (callback: AppDirSelectedCallback): void => {
13+
ipcRenderer.on(
14+
'app_dir_selected',
15+
(event: IpcRendererEvent, path: string) => {
16+
callback(path);
17+
}
18+
);
19+
};
20+
21+
// removes all listeners for the app_dir_selected event
22+
// this is important because otherwise listeners will pile up and events will trigger multiple events
23+
const removeAllAppDirChosenListeners = (): void => {
24+
ipcRenderer.removeAllListeners('app_dir_selected');
25+
};
26+
27+
export {
28+
chooseAppDir,
29+
addAppDirChosenListener,
30+
removeAllAppDirChosenListeners
31+
};

app/.electron/preloadFunctions/cookies.js

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { ipcRenderer, IpcRendererEvent } from 'electron';
2+
3+
type GetCookieCallback = (cookie: string) => void;
4+
5+
const setCookie = (): void => {
6+
ipcRenderer.send('set_cookie');
7+
};
8+
9+
const getCookie = (callback: GetCookieCallback): void => {
10+
ipcRenderer.on('give_cookie', (event: IpcRendererEvent, cookie: string) => {
11+
callback(cookie);
12+
});
13+
};
14+
15+
const delCookie = (): void => {
16+
ipcRenderer.send('delete_cookie');
17+
};
18+
19+
const github = (): void => {
20+
ipcRenderer.send('github');
21+
};
22+
23+
const tutorial = (): void => {
24+
ipcRenderer.send('tutorial');
25+
};
26+
27+
export { setCookie, getCookie, delCookie, github, tutorial };
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const { format } = require('prettier');
1+
import { format } from '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
55
// the format function is defined in the main process because it needs to access node functionality ('fs')
6-
const formatCode = code => {
6+
const formatCode = (code: string): string => {
77
return format(code, {
88
singleQuote: true,
99
trailingComma: 'es5',
@@ -13,4 +13,4 @@ const formatCode = code => {
1313
});
1414
};
1515

16-
module.exports = formatCode;
16+
export default formatCode;

0 commit comments

Comments
 (0)