Skip to content

Commit

Permalink
Require Electron 5
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 21, 2019
1 parent 56d4913 commit ac7fa06
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 29 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
os: osx
language: node_js
node_js:
- 'node'
- '12'
30 changes: 17 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
'use strict';
const {inspect} = require('util');
const path = require('path');
const electron = require('electron');
const chokidar = require('chokidar');
const isDev = require('electron-is-dev');
const dateTime = require('date-time');
const chalk = require('chalk');

function getMainProcessPaths(topModuleObj) {
const cwd = path.dirname(topModuleObj.filename);
const paths = new Set([topModuleObj.filename]);
function getMainProcessPaths(topModuleObject) {
const cwd = path.dirname(topModuleObject.filename);
const paths = new Set([topModuleObject.filename]);

const getPaths = moduleObj => {
for (const child of moduleObj.children) {
const getPaths = moduleObject => {
for (const child of moduleObject.children) {
if (path.relative(cwd, child.filename).includes('node_modules')) {
continue;
}
Expand All @@ -19,7 +22,7 @@ function getMainProcessPaths(topModuleObj) {
}
};

getPaths(topModuleObj);
getPaths(topModuleObject);

return paths;
}
Expand All @@ -35,9 +38,10 @@ module.exports = (moduleObj, options) => {
throw new Error('You have to pass the `module` object');
}

options = Object.assign({
watchRenderer: true
}, options);
options = {
watchRenderer: true,
...options
};

const cwd = path.dirname(moduleObj.filename);
const mainProcessPaths = getMainProcessPaths(moduleObj);
Expand All @@ -55,21 +59,21 @@ module.exports = (moduleObj, options) => {

if (options.debug) {
watcher.on('ready', () => {
console.log('Watched paths:', watcher.getWatched());
console.log('Watched paths:', inspect(watcher.getWatched(), {compact: false, colors: true}));
});
}

watcher.on('change', filePath => {
if (options.debug) {
console.log('File changed:', filePath);
console.log('File changed:', chalk.bold(filePath), chalk.dim(`(${dateTime().split(' ')[1]})`));
}

if (mainProcessPaths.has(path.join(cwd, filePath))) {
electron.app.relaunch();
electron.app.exit(0);
} else {
for (const win of electron.BrowserWindow.getAllWindows()) {
win.webContents.reloadIgnoringCache();
for (const window_ of electron.BrowserWindow.getAllWindows()) {
window_.webContents.reloadIgnoringCache();
}
}
});
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
"development"
],
"dependencies": {
"chokidar": "^2.0.0",
"electron-is-dev": "^0.3.0"
"chalk": "^2.4.2",
"chokidar": "^3.0.0",
"date-time": "^3.1.0",
"electron-is-dev": "^1.1.0"
},
"devDependencies": {
"electron": "^1.7.10",
"xo": "*"
"electron": "^5.0.1",
"xo": "^0.24.0"
},
"xo": {
"envs": [
Expand Down
10 changes: 6 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Note that it will not work correctly if you transpile the main process JS files
$ npm install --save-dev electron-reloader
```

*Requires Electron 5 or later.*


## Usage

Expand All @@ -21,7 +23,7 @@ The following must be included in the app entry file, usually named `index.js`:
```js
try {
require('electron-reloader')(module);
} catch (err) {}
} catch (_) {}
```

You have to pass the `module` object so we can read the module graph and figure out which files belong to the main process.
Expand All @@ -35,13 +37,13 @@ The `try/catch` is needed so it doesn't throw `Cannot find module 'electron-relo

#### module

Type: `Object`
Type: `object`

The global `module` object.

#### options

Type: `Object`
Type: `object`

##### debug

Expand All @@ -52,7 +54,7 @@ Prints watched paths and when files change. Can be useful to make sure you set i

##### ignore

Type: `Array<string|RegExp>`
Type: `Array<string | RegExp>`

Ignore patterns passed to [`chokidar`](https://github.com/paulmillr/chokidar#path-filtering). By default, files/directories starting with a `.`, `.map` files, and `node_modules` directories are ignored. This option is additive to those.

Expand Down
15 changes: 10 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
'use strict';
const electron = require('electron');
const path = require('path');
const {app, BrowserWindow} = require('electron');

require('..')(module, {
debug: true
});

electron.app.on('ready', () => {
const win = new electron.BrowserWindow();
win.loadURL(`file://${__dirname}/index.html`);
});
let mainWindow;

(async () => {
await app.whenReady();

mainWindow = new BrowserWindow();
await mainWindow.loadFile(path.join(__dirname, 'index.html'));
})();

0 comments on commit ac7fa06

Please sign in to comment.