What is electron?
Electron is a framework for creating native applications with web technologies like JavaScript, HTML, and CSS. It combines the Chromium rendering engine and the Node.js runtime, allowing you to build cross-platform desktop applications.
What are electron's main functionalities?
Creating a Browser Window
This feature allows you to create a new browser window in your Electron application. The code sample demonstrates how to create a window and load a URL into it.
const { app, BrowserWindow } = require('electron');
app.on('ready', () => {
const mainWindow = new BrowserWindow({ width: 800, height: 600 });
mainWindow.loadURL('https://example.com');
});
Inter-Process Communication (IPC)
Electron provides IPC (Inter-Process Communication) to allow communication between the main process and renderer processes. The code sample shows how to send and receive messages asynchronously.
const { ipcMain, ipcRenderer } = require('electron');
// Main process
ipcMain.on('asynchronous-message', (event, arg) => {
console.log(arg); // prints 'ping'
event.reply('asynchronous-reply', 'pong');
});
// Renderer process
ipcRenderer.send('asynchronous-message', 'ping');
ipcRenderer.on('asynchronous-reply', (event, arg) => {
console.log(arg); // prints 'pong'
});
Using Node.js Modules
Electron allows you to use Node.js modules in your application. The code sample demonstrates how to use the 'fs' module to read a file.
const fs = require('fs');
fs.readFile('/path/to/file', (err, data) => {
if (err) throw err;
console.log(data.toString());
});
Packaging the Application
Electron applications can be packaged for distribution using tools like 'electron-packager'. The code sample shows how to package an Electron app for Windows using a child process.
const { exec } = require('child_process');
exec('electron-packager . myApp --platform=win32 --arch=x64', (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
Other packages similar to electron
nw
NW.js (previously known as node-webkit) is another framework for building desktop applications using web technologies. It also combines Node.js and Chromium but offers different APIs and a different approach to application architecture compared to Electron.
proton-native
Proton Native is a framework for building native desktop applications using React. Unlike Electron, which uses web technologies and Chromium, Proton Native uses native components for rendering, which can result in better performance and a more native look and feel.
Electron
A simple command-line interface framework for node.js.
Features
- reimagined
process.argv
parsing utility - framework for single or multiple command programs
- automatic
--help
command generation with multiple theming options - built in cli coloring
- chainable api
Quick Start Guide
This "Quick Start Guide" and the full API reference can be found
on electron's documentation website.
Installation
The electron
package is available through npm. It is recommended
that you add it to your project's package.json
.
npm install electron
Parsing Arguments
The argument parsing utility can be used independently of the program
framework. Just pass the process.argv
from any node modules and your
ready to go.
The following command execution...
$ node cli.js build --minify --out saved.min.js
Could be captured as so...
var argv = require('electron').argv();
argv.commands;
argv.modes;
argv.params;
argv.command('build');
argv.mode('m', 'minify');
argv.param('o', 'out');
Recommend reading the "Argument Parsing Utility" section of the
documentation
to learn about the methodologies and specifics of each of the helpers.
Your First Program
To construct your first program, simply execute the electron export
with a parameter of the namespace you wish to use for your program.
Then proceed to define your settings and commands.
var myApp = require('../lib/myapp')
, program = require('electron')('myapp');
program
.name('My Cool App')
.desc('http://docs.mycoolapp.com')
.version(myApp.version);
program
.command('build')
.desc('start a build task')
.option('-m, --minify', 'flag to set enable minification')
.option('-o, --out [file.js]', 'name of output file')
.action(function (argv) {
var minify = argv.mode('m', 'minify')
, savefile = argv.param('o', 'out')
, cwd = argv.cwd;
program.colorize();
console.log('Welcome to myApp'.gray + myApp.version);
console.log('It works if it ends with '.gray + 'myApp ' + 'ok'.green);
});
program.parse();
Your -h, --help
and -v, --version
will be generated for you automatically.
Recommend reading the "Program Framework" and "Constructing Commands" sections
of the documentation
to learn about all of the available chainable commands and theming options
available to construct your programs.
Tests
Tests are writting in Mocha using
the Chai should
BDD assertion library. To make sure you
have that installed, clone this repo, install dependacies using npm install
.
$ npm test
Contributors
Interested in contributing? Fork to get started. Contact @logicalparadox
if you are interested in being regular contributor.
License
(The MIT License)
Copyright (c) 2012 Jake Luer jake@alogicalparadox.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.