Skip to content

Commit 1c03932

Browse files
committed
Moved fs and code formatting methods from renderer process to the main process
1 parent 51fa427 commit 1c03932

File tree

14 files changed

+548
-489
lines changed

14 files changed

+548
-489
lines changed

app/electron/main.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const {
22
app,
33
protocol,
4+
dialog,
45
BrowserWindow,
56
session,
67
ipcMain,
@@ -13,6 +14,8 @@ const {
1314
default: installExtension,
1415
REACT_DEVELOPER_TOOLS
1516
} = require('electron-devtools-installer');
17+
const debug = require('electron-debug');
18+
1619
const Protocol = require('./protocol');
1720
// menu from another file to modularize the code
1821
const MenuBuilder = require('./menu');
@@ -70,9 +73,9 @@ async function createWindow() {
7073
// Electron API only available from preload, not loaded page
7174
contextIsolation: true,
7275
// disables remote module. critical for ensuring that rendering process doesn't have access to node functionality
73-
enableRemoteModule: false
76+
enableRemoteModule: false,
7477
// path of preload script. preload is how the renderer page will have access to electron functionality
75-
// preload: path.join(__dirname, "preload.js"),
78+
preload: path.join(__dirname, 'preload.js')
7679
}
7780
});
7881

@@ -112,8 +115,10 @@ async function createWindow() {
112115
// Only do these things when in development
113116
if (isDev) {
114117
// automatically open DevTools when opening the app
115-
win.webContents.openDevTools();
116-
require('electron-debug')(); // https://github.com/sindresorhus/electron-debug
118+
win.webContents.once('dom-ready', () => {
119+
debug();
120+
win.webContents.openDevTools();
121+
});
117122
}
118123

119124
// Emitted when the window is closed.
@@ -212,8 +217,11 @@ app.on('activate', () => {
212217
app.on('web-contents-created', (event, contents) => {
213218
contents.on('will-navigate', (event, navigationUrl) => {
214219
const parsedUrl = new URL(navigationUrl);
215-
const validOrigins = [selfHost];
216-
220+
const validOrigins = [
221+
selfHost,
222+
'https://localhost:8080',
223+
'https://github.com/'
224+
];
217225
// Log and prevent the app from navigating to a new page if that page's origin is not whitelisted
218226
if (!validOrigins.includes(parsedUrl.origin)) {
219227
console.error(
@@ -227,7 +235,11 @@ app.on('web-contents-created', (event, contents) => {
227235

228236
contents.on('will-redirect', (event, navigationUrl) => {
229237
const parsedUrl = new URL(navigationUrl);
230-
const validOrigins = [];
238+
const validOrigins = [
239+
selfHost,
240+
'https://localhost:8080',
241+
'https://github.com/'
242+
];
231243

232244
// Log and prevent the app from redirecting to a new page
233245
if (!validOrigins.includes(parsedUrl.origin)) {
@@ -288,3 +300,21 @@ app.on('remote-get-current-window', (event, webContents) => {
288300
app.on('remote-get-current-web-contents', (event, webContents) => {
289301
event.preventDefault();
290302
});
303+
304+
// When a user selects "Export project", a function (chooseAppDir loaded via preload.js)
305+
// is triggered that sends a "choose_app_dir" message to the main process
306+
// when the "choose_app_dir" message is received it triggers this event listener
307+
ipcMain.on('choose_app_dir', event => {
308+
// dialog displays the native system's dialogue for selecting files
309+
// once a directory is chosen send a message back to the renderer with the path of the directory
310+
dialog
311+
.showOpenDialog(win, {
312+
properties: ['openDirectory'],
313+
buttonLabel: 'Export'
314+
})
315+
.then(directory => {
316+
if (!directory) return;
317+
event.sender.send('app_dir_selected', directory.filePaths[0]);
318+
})
319+
.catch(err => console.log('ERROR on "choose_app_dir" event: ', err));
320+
});

app/electron/preload.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// contextBridge is what allows context to be translated between the main process and the render process
22
// ipcRenderer sends messsages between render process and the main process
33
const { contextBridge, ipcRenderer } = require('electron');
4-
// const fs = require("fs");
5-
// const i18nextBackend = require("i18next-electron-fs-backend");
6-
// const Store = require("secure-electron-store").default;
7-
// const ContextMenu = require("secure-electron-context-menu").default;
8-
9-
// Create the electron store to be made available in the renderer process
10-
// the store is a way to persist user preferences and data
11-
// let store = new Store();
4+
const { existsSync, writeFileSync, mkdirSync, writeFile } = require('fs');
5+
const formatCode = require('./preloadFunctions/format');
6+
const {
7+
chooseAppDir,
8+
appDirChosen
9+
} = require('./preloadFunctions/chooseAppDir');
1210

1311
// Expose protected methods that allow the renderer process to use
1412
// the ipcRenderer without exposing the entire object
@@ -19,9 +17,11 @@ const { contextBridge, ipcRenderer } = require('electron');
1917
// the api object (second arg) can contain functions, strings, bools, numbers, arrays, obects in value
2018
// data primitives sent on the bridge are immutable and changes in one context won't carry over to another context
2119
contextBridge.exposeInMainWorld('api', {
22-
// i18nextElectronBackend: i18nextBackend.preloadBindings(ipcRenderer),
23-
// generally the store uses fs directly from the renderer to access the store
24-
// this beinding will access the store using the
25-
// store: store.preloadBindings(ipcRenderer, fs),
26-
contextMenu: ContextMenu.preloadBindings(ipcRenderer)
20+
formatCode: formatCode,
21+
chooseAppDir: chooseAppDir,
22+
appDirChosen: appDirChosen,
23+
existsSync: existsSync,
24+
writeFileSync: writeFileSync,
25+
mkdirSync: mkdirSync,
26+
writeFile: writeFile
2727
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { ipcRenderer } = require('electron');
2+
3+
const chooseAppDir = () => {
4+
return ipcRenderer.send('choose_app_dir');
5+
};
6+
7+
// once an app directory is chosen, the main process will send an "app_dir_selected" event
8+
// when this event occurs, exucte the callback passed in by the user
9+
const appDirChosen = callback => {
10+
return ipcRenderer.on('app_dir_selected', (event, path) => {
11+
callback(path);
12+
});
13+
};
14+
15+
module.exports = { chooseAppDir, appDirChosen };
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { format } = require('prettier');
2+
3+
// format code using prettier
4+
// this format function is used in the render process to format the code in the code preview
5+
const formatCode = code => {
6+
return format(code, {
7+
singleQuote: true,
8+
trailingComma: 'es5',
9+
bracketSpacing: true,
10+
jsxBracketSameLine: true,
11+
// parser: 'babel'
12+
parser: 'typescript'
13+
});
14+
};
15+
16+
module.exports = formatCode;

app/src/components/main/Canvas.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,3 @@ function Canvas() {
108108
}
109109

110110
export default Canvas;
111-
112-
113-

app/src/containers/LeftContainer.tsx

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,38 +115,28 @@ const LeftContainer = (): JSX.Element => {
115115
))}
116116
</List>
117117
);
118-
118+
const chooseAppDir = () => window.api.chooseAppDir();
119119
// helper function called by showGenerateAppModal
120120
// this function will prompt the user to choose an app directory once they've chosen their export option
121121
const chooseGenOptions = (genOpt: number) => {
122122
// set export option: 0 --> export only components, 1 --> export full project
123123
console.log('in chooseGenOptions');
124124
setGenOption(genOpt);
125125
console.log('Gen option is ', genOpt);
126-
// closeModal
127-
exportProject('/Users', 'NEW PROJECT', genOpt, state.components);
128-
closeModal();
129126
// Choose app dir
130-
// NOTE: This functionality isn't working right now. Will upgrade Electron and see if that fixes it
131-
//chooseAppDir();
127+
// window.api.chooseAppDir;
128+
chooseAppDir();
129+
// closeModal
132130

133-
// exportProject('/Users/tylersullberg/', 'NEW PROJECT', 1);
131+
closeModal();
134132
};
135133

136-
const chooseAppDir = () => IPC.send('choose_app_dir');
137-
138-
// when the user selects a directory from the modal,
139-
// we're going to create the application in their chosen directory
140-
IPC.on('app_dir_selected', (event: string, path: string) => {
141-
// createApplication({
142-
// path,
143-
// components,
144-
// genOption,
145-
// appName: 'reactype_app',
146-
// exportAppBool: null
147-
// });
148-
// exportProject();
149-
console.log('app directory selected!!!');
134+
// add listener for when an app directory is chosen
135+
// when a directory is chosen, the callback will export the project to the chosen folder
136+
// Note: this listener is imported from the main process via preload.js
137+
window.api.appDirChosen(path => {
138+
console.log('App dir chosen : ', path);
139+
exportProject(path, 'NEW PROJECT', genOption, state.components);
150140
});
151141

152142
setModal(

app/src/helperFunctions/generateCode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// import { format } from 'prettier';
21
import { Component, State, ChildElement } from '../interfaces/InterfacesNew';
32
import HTMLTypes from '../context/HTMLTypes';
43

@@ -147,7 +146,8 @@ const generateUnformattedCode = (comps: Component[], componentId: number) => {
147146

148147
// formats code with prettier linter
149148
const formatCode = (code: string) => {
150-
return code;
149+
return window.api.formatCode(code);
150+
// return code;
151151
// return format(code, {
152152
// singleQuote: true,
153153
// trailingComma: 'es5',

app/src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const PrivateRoute = ({ component: Component, ...rest }) => (
2222
/>
2323
);
2424

25-
// Temporarily removing private routes for the new electron build
2625
// ReactDOM.render(
2726
// <Router>
2827
// <Switch>

0 commit comments

Comments
 (0)