📝 Code Analysis

Code Analysis #001

date
Jun 12, 2023
slug
code-analysis-001
author
status
Public
tags
Electron
NodeJS
JavaScript
summary
type
Post
thumbnail
category
📝 Code Analysis
updatedAt
Jun 16, 2023 03:38 AM

Source Code:

const electron = require('electron'); const path = require('path'); const url = require('url'); // SET ENV process.env.NODE_ENV = 'development'; const {app, BrowserWindow, Menu, ipcMain} = electron; let mainWindow; let addWindow; // Listen for app to be ready app.on('ready', function(){ // Create new window mainWindow = new BrowserWindow({}); // Load html in window mainWindow.loadURL(url.format({ pathname: path.join(__dirname, 'mainWindow.html'), protocol: 'file:', slashes:true })); // Quit app when closed mainWindow.on('closed', function(){ app.quit(); }); // Build menu from template const mainMenu = Menu.buildFromTemplate(mainMenuTemplate); // Insert menu Menu.setApplicationMenu(mainMenu); }); // Handle add item window function createAddWindow(){ addWindow = new BrowserWindow({ width: 300, height:200, title:'Add Shopping List Item' }); addWindow.loadURL(url.format({ pathname: path.join(__dirname, 'addWindow.html'), protocol: 'file:', slashes:true })); // Handle garbage collection addWindow.on('close', function(){ addWindow = null; }); } // Catch item:add ipcMain.on('item:add', function(e, item){ mainWindow.webContents.send('item:add', item); addWindow.close(); // Still have a reference to addWindow in memory. Need to reclaim memory (Grabage collection) //addWindow = null; }); // Create menu template const mainMenuTemplate = [ // Each object is a dropdown { label: 'File', submenu:[ { label:'Add Item', click(){ createAddWindow(); } }, { label:'Clear Items', click(){ mainWindow.webContents.send('item:clear'); } }, { label: 'Quit', accelerator:process.platform == 'darwin' ? 'Command+Q' : 'Ctrl+Q', click(){ app.quit(); } } ] } ]; // If OSX, add empty object to menu if(process.platform == 'darwin'){ mainMenuTemplate.unshift({}); } // Add developer tools option if in dev if(process.env.NODE_ENV !== 'production'){ mainMenuTemplate.push({ label: 'Developer Tools', submenu:[ { role: 'reload' }, { label: 'Toggle DevTools', accelerator:process.platform == 'darwin' ? 'Command+I' : 'Ctrl+I', click(item, focusedWindow){ focusedWindow.toggleDevTools(); } } ] }); }
electronshoppinglist\main.js
 

Summary

 

Code Analysis

 
process.env.NODE_ENV = 'development';
LINE #6
Sets the environment variable NODE_ENV to 'development'. This can be used to dictate behavior of the app depending on the environment it's in.