|
1 | | -const { app, BrowserWindow } = require('electron'); |
2 | | -const path = require('path'); |
3 | | -const isDev = require('electron-is-dev'); |
4 | | -let mainWindow; |
5 | | - |
6 | | -if (isDev) console.log('electron version', process.versions.electron); |
7 | | - |
8 | | -if (isDev) { |
9 | | - const { |
10 | | - default: installExtension, |
11 | | - REACT_DEVELOPER_TOOLS, |
12 | | - } = require('electron-devtools-installer'); |
13 | | - function addDevTools() { |
14 | | - app.whenReady().then(() => { |
15 | | - installExtension(REACT_DEVELOPER_TOOLS) |
16 | | - .then((name) => console.log(`Added Extension: ${name}`)) |
17 | | - .catch((err) => console.log('An error occurred: ', err)); |
18 | | - }); |
19 | | - } |
20 | | -} |
21 | | -function createWindow() { |
22 | | - mainWindow = new BrowserWindow({ |
23 | | - width: 1550, |
24 | | - height: 750, |
25 | | - minHeight: 750, |
26 | | - icon: path.join(__dirname, 'public/icon.png'), |
27 | | - webPreferences: { |
28 | | - nodeIntegration: true, |
29 | | - webviewTag: true, |
30 | | - }, |
31 | | - }); |
32 | | - mainWindow.loadURL( |
33 | | - isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}` |
34 | | - ); |
35 | | - mainWindow.on('closed', () => (mainWindow = null)); |
36 | | -} |
37 | | - |
38 | | -if (isDev) { |
39 | | - app.on('ready', addDevTools); |
40 | | -} |
41 | | - |
42 | | -app.on('ready', createWindow); |
43 | | -app.on('window-all-closed', () => { |
44 | | - if (process.platform !== 'darwin') { |
45 | | - app.quit(); |
46 | | - } |
47 | | -}); |
48 | | -app.on('activate', () => { |
49 | | - if (mainWindow === null) { |
50 | | - createWindow(); |
51 | | - } |
52 | | -}); |
| 1 | +const { |
| 2 | + app, BrowserWindow, ipcMain, dialog, |
| 3 | +} = require('electron'); |
| 4 | +const path = require('path'); |
| 5 | +const isDev = require('electron-is-dev'); |
| 6 | +const fs = require('fs'); |
| 7 | +const os = require('os'); |
| 8 | +const pty = require('node-pty'); |
| 9 | +const fixPath = require('fix-path'); |
| 10 | + |
| 11 | +//Dynamic variable to change terminal type based on os |
| 12 | +const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash'; |
| 13 | +console.log("process.env.Path1: ", process.env.PATH); |
| 14 | +//=> '/usr/bin' |
| 15 | + |
| 16 | +fixPath(); |
| 17 | + |
| 18 | +console.log("process.env.Path2: ", process.env.PATH); |
| 19 | +//=> '/usr/local/bin:/usr/bin' |
| 20 | +let mainWindow; |
| 21 | + |
| 22 | +if (isDev) console.log('electron version', process.versions.electron); |
| 23 | + |
| 24 | +if (isDev) { |
| 25 | + const { |
| 26 | + default: installExtension, |
| 27 | + REACT_DEVELOPER_TOOLS, |
| 28 | + } = require('electron-devtools-installer'); |
| 29 | + function addDevTools() { |
| 30 | + app.whenReady().then(() => { |
| 31 | + installExtension(REACT_DEVELOPER_TOOLS) |
| 32 | + .then((name) => console.log(`Added Extension: ${name}`)) |
| 33 | + .catch((err) => console.log('An error occurred: ', err)); |
| 34 | + }); |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +function createWindow() { |
| 39 | + mainWindow = new BrowserWindow({ |
| 40 | + width: 1550, |
| 41 | + height: 750, |
| 42 | + minHeight: 750, |
| 43 | + icon: path.join(__dirname, 'icon.png'), |
| 44 | + webPreferences: { |
| 45 | + nodeIntegration: true, |
| 46 | + webviewTag: true, |
| 47 | + contextIsolation: false, |
| 48 | + }, |
| 49 | + }); |
| 50 | + mainWindow.loadURL( |
| 51 | + isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`, |
| 52 | + ); |
| 53 | + mainWindow.on('closed', () => (mainWindow = null)); |
| 54 | + |
| 55 | + // PTY PROCESS FOR IN APP TERMINAL |
| 56 | + const ptyArgs = { |
| 57 | + name: 'xterm-color', |
| 58 | + cols: 80, |
| 59 | + rows: 80, |
| 60 | + cwd: process.env.HOME, |
| 61 | + env: process.env, |
| 62 | + }; |
| 63 | + console.log("process.env.HOME: ", process.env.HOME); |
| 64 | + |
| 65 | + const ptyProcess = pty.spawn(shell, [], ptyArgs); |
| 66 | + // with ptyProcess, we want to send incoming data to the channel terminal.incData |
| 67 | + ptyProcess.on('data', (data) => { |
| 68 | + mainWindow.webContents.send('terminal.incData', data); |
| 69 | + }); |
| 70 | + // in the main process, at terminal.toTerm channel, when data is received, |
| 71 | + // main process will write to ptyProcess |
| 72 | + ipcMain.on('terminal.toTerm', (event, data) => { |
| 73 | + ptyProcess.write(data); |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +// EDITORVIEW.JSX SAVE FILE FUNCTIONALITY |
| 78 | +ipcMain.on('EditorView.saveFile', (e, filePath, editedText) => { |
| 79 | + fs.writeFile(filePath, editedText, (err) => { |
| 80 | + if (err) throw err; |
| 81 | + }); |
| 82 | + // Return a success message upon save |
| 83 | + e.returnValue = 'Changes Saved'; |
| 84 | +}); |
| 85 | + |
| 86 | +/* |
| 87 | + EXPORTFILEMODAL.JSX FILE FUNCTIONALITY |
| 88 | + (check existence and create folder) |
| 89 | +*/ |
| 90 | +ipcMain.on('ExportFileModal.exists', (e, fileOrFolderPath) => { |
| 91 | + e.returnValue = fs.existsSync(fileOrFolderPath, (err) => { |
| 92 | + if (err) throw err; |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +ipcMain.on('ExportFileModal.mkdir', (e, folderPath) => { |
| 97 | + e.returnValue = fs.mkdirSync(folderPath, (err) => { |
| 98 | + if (err) throw err; |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +ipcMain.on('ExportFileModal.fileCreate', (e, filePath, file) => { |
| 103 | + e.returnValue = fs.writeFile(filePath, file, (err) => { |
| 104 | + if (err) throw err; |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +ipcMain.on('ExportFileModal.readFile', (e, filePath) => { |
| 109 | + e.returnValue = fs.readFileSync(filePath, 'utf8', (err) => { |
| 110 | + if (err) throw err; |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +// OPENFOLDERBUTTON.JSX FILE FUNCTIONALITY |
| 115 | +ipcMain.on('OpenFolderButton.isDirectory', (e, filePath) => { |
| 116 | + e.returnValue = fs.statSync(filePath).isDirectory(); |
| 117 | +}); |
| 118 | + |
| 119 | +ipcMain.on('OpenFolderButton.dialog', (e) => { |
| 120 | + const dialogOptions = { |
| 121 | + properties: ['openDirectory', 'createDirectory'], |
| 122 | + filters: [ |
| 123 | + { name: 'Javascript Files', extensions: ['js', 'jsx'] }, |
| 124 | + { name: 'Style', extensions: ['css'] }, |
| 125 | + { name: 'Html', extensions: ['html'] }, |
| 126 | + ], |
| 127 | + message: 'Please select your project folder', |
| 128 | + }; |
| 129 | + e.returnValue = dialog.showOpenDialogSync(dialogOptions); |
| 130 | +}); |
| 131 | + |
| 132 | +/* |
| 133 | +UNIVERSAL IPC CALLS |
| 134 | +(The following IPC calls are made from various components in the codebase) |
| 135 | +*/ |
| 136 | +ipcMain.on('Universal.stat', (e, filePath) => { |
| 137 | + e.returnValue = fs.statSync(filePath).isDirectory(); |
| 138 | +}); |
| 139 | + |
| 140 | +ipcMain.on('Universal.readDir', (e, projectFilePath) => { |
| 141 | + e.returnValue = fs.readdirSync(projectFilePath, (err) => { |
| 142 | + if (err) throw err; |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +ipcMain.on('Universal.readFile', (e, filePath) => { |
| 147 | + e.returnValue = fs.readFileSync(filePath, 'utf8', (err) => { |
| 148 | + if (err) throw err; |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | +ipcMain.on('Universal.path', (e, folderPath, filePath) => { |
| 153 | + e.returnValue = path.relative(folderPath, filePath, (err) => { |
| 154 | + if (err) throw err; |
| 155 | + }); |
| 156 | +}) |
| 157 | + |
| 158 | +// ELECTRON BOILERPLATE FOR DEVTOOLS AND WINDOW CREATION |
| 159 | +if (isDev) { |
| 160 | + app.on('ready', addDevTools); |
| 161 | +} |
| 162 | + |
| 163 | +app.on('ready', createWindow); |
| 164 | +app.on('window-all-closed', () => { |
| 165 | + if (process.platform !== 'darwin') { |
| 166 | + app.quit(); |
| 167 | + } |
| 168 | +}); |
| 169 | +app.on('activate', () => { |
| 170 | + if (mainWindow === null) { |
| 171 | + createWindow(); |
| 172 | + } |
| 173 | +}); |
0 commit comments