Launching Electron
Now that we have exposed Electron inside our Aurelia app it would be nice to be able to modify our source and have all the same debugging features available that Aurelia affords us.Lets start by modifying the Aurelia run script so that we can launch Electron from the Aurelia CLI.
- Open the run.js file located in the aurelia_project\tasks folder
- Add the following referenceimport * as childProcess from 'child_process';
- Now add the following code to launch Electronlet elect = gulp.series(build,done => {childProcess.exec('npm start').on("close", () => {// User closed the app. Kill the host process.process.exit();})done();});
- Next we modify the run code to use the electron launcherlet job;if (CLIOptions.hasFlag('electron')) {job = elect} else {job = serve}let run = gulp.series(job,done => { watch(reload); done(); });
- The file should now look like this
- Now we can launch Electron by including our new --electron flag to the CLI command
- Run command 'nvm use 10.14.0'
- Run command 'au run --watch --server --electron'
Refreshing Electron App
One of the really nice features of developing in Chrome is the use of BrowserSync to keep the Chrome window refreshed automatically with changes we make in source.
There is an electron reload package that we can add that watches files for changes and re-loads the electron window ... perfect ... well almost. Since our solution is getting fairly complex we are monitoring and copying a lot of files every time the Aurelia watcher detects a change. Unfortunately watching our app directory causes multiple reloads of the electron app for every change we make. This isn't ideal. The good news is we can manipulate the reloader by adding a single file to our copy command and then watching the single file for a change.
- Create a new file in the \root folder called reload ... It doesn't need an extension because it is just a dummy file used to trigger the reloader.
- Next we want to include the dummy reload file in our push files script that copies files to the electron app directory when a change is triggered.
- Open the aurelia.json file in the aurelia_project folder
- Add "reload": "" to the end of the sources node in the pushFiles node as shown below
- Now we need to add the electron-reload package to our project
- Open a command prompt in the /root folder of our project.
- Run command 'nvm use 10.14.0'
- Run command 'npm install electron-reload --save-dev'
- Next we need to load the package at run time.
- Open the file main.js located in the /app directory
- Add the following code// load the electron reloadertry {const path = require('path');//watch the reload file for changesrequire('electron-reload')(path.join(__dirname,'reload'), {electron: path.join(__dirname, 'node_modules', '.bin', 'electron')});} catch(e) {}
- The file should now look like this
No comments:
Post a Comment