Skip to content

Commit

Permalink
fix: only allow installs from the main process
Browse files Browse the repository at this point in the history
BREAKING CHANGE
  • Loading branch information
MarshallOfSound committed Apr 7, 2020
1 parent a5dfe4c commit fc1d878
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
22 changes: 8 additions & 14 deletions README.md
Expand Up @@ -23,30 +23,24 @@ yarn add electron-devtools-installer -D
```

## Usage
In your `electron-starter.js` file, add the following callback to `app.on('ready', ... )`,
All you have to do now is this in the **main** process of your application.

```js
const { default: installExtension, REDUX_DEVTOOLS } = require('electron-devtools-installer');
const { app, BrowserWindow } = require('electron');
import installExtension, { REDUX_DEVTOOLS } from 'electron-devtools-installer';
// Or if you can not use ES6 imports
/**
const { default: installExtension, REACT_DEVELOPER_TOOLS } = require('electron-devtools-installer');
*/
const { app } = require('electron');

app.on('ready', () => {
app.whenReady().then(() => {
installExtension(REDUX_DEVTOOLS)
.then((name) => console.log(`Added Extension: ${name}`))
.catch((err) => console.log('An error occurred: ', err));
});
```
To install multiple extensions, `installExtension` takes an array.

Alternatively, using `require()` and destructuring (node v6 or higher) you can

```js
const { default: installExtension, REACT_DEVELOPER_TOOLS } = require('electron-devtools-installer');

installExtension(REACT_DEVELOPER_TOOLS)
.then((name) => console.log(`Added Extension: ${name}`))
.catch((err) => console.log('An error occurred: ', err));
```

## What extensions can I use?

Technically you can use whatever extension you want. Simply find the ChromeStore ID
Expand Down
12 changes: 8 additions & 4 deletions src/utils.js
@@ -1,4 +1,4 @@
import electron, { remote } from 'electron';
import { net } from 'electron';
import fs from 'fs';
import path from 'path';
import https from 'https';
Expand All @@ -9,22 +9,26 @@ export const getPath = () => {
};

// Use https.get fallback for Electron < 1.4.5
const { net } = remote || electron;
const request = net ? net.request : https.get;

export const downloadFile = (from, to) =>
new Promise((resolve, reject) => {
export const downloadFile = (from, to) => {
if (process.type !== 'browser') {
return Promise.reject(new Error('electron-devtools-installer can only be used from the main process'));
}
return new Promise((resolve, reject) => {
const req = request(from);
req.on('response', (res) => {
// Shouldn't handle redirect with `electron.net`, this is for https.get fallback
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
return downloadFile(res.headers.location, to).then(resolve).catch(reject);
}
res.pipe(fs.createWriteStream(to)).on('close', resolve);
res.on('error', reject);
});
req.on('error', reject);
req.end();
});
}

export const changePermissions = (dir, mode) => {
const files = fs.readdirSync(dir);
Expand Down

0 comments on commit fc1d878

Please sign in to comment.