Skip to content

Commit

Permalink
feat: add electron 9 support (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Bender authored Jun 27, 2020
1 parent d692149 commit ab713a6
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ workflows:
- test-electron:
name: test-electron-8
electron_version: ^8.0.0
- test-electron:
name: test-electron-9
electron_version: ^9.0.0
- release:
requires:
- test-electron-4
- test-electron-5
- test-electron-6
- test-electron-7
- test-electron-8
- test-electron-9
filters:
branches:
only:
Expand Down
37 changes: 31 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BrowserWindow } from 'electron';
import { BrowserWindow, session } from 'electron';
import fs from 'fs';
import path from 'path';
import semver from 'semver';
Expand Down Expand Up @@ -48,19 +48,44 @@ const install = (extensionReference, forceDownload = false) => {
);
}
const extensionName = IDMap[chromeStoreID];
const extensionInstalled =
extensionName &&
BrowserWindow.getDevToolsExtensions &&
BrowserWindow.getDevToolsExtensions()[extensionName];
let extensionInstalled = extensionName;

// For Electron >=9.
if (session.defaultSession.getExtension) {
extensionInstalled =
extensionInstalled &&
session.defaultSession.getAllExtensions().find((e) => e.name === extensionName);
} else {
extensionInstalled =
extensionInstalled &&
BrowserWindow.getDevToolsExtensions &&
BrowserWindow.getDevToolsExtensions().hasOwnProperty(extensionName);
}

if (!forceDownload && extensionInstalled) {
return Promise.resolve(IDMap[chromeStoreID]);
}
return downloadChromeExtension(chromeStoreID, forceDownload).then((extensionFolder) => {
// Use forceDownload, but already installed
if (extensionInstalled) {
BrowserWindow.removeDevToolsExtension(extensionName);
// For Electron >=9.
if (session.defaultSession.removeExtension) {
const extensionId = session.defaultSession.getAllExtensions().find((e) => e.name).id;
session.defaultSession.removeExtension(extensionId);
} else {
BrowserWindow.removeDevToolsExtension(extensionName);
}
}

// For Electron >=9.
if (session.defaultSession.loadExtension) {
return session.defaultSession.loadExtension(extensionFolder).then((ext) => {
return Promise.resolve(ext.name);
});
}

const name = BrowserWindow.addDevToolsExtension(extensionFolder); // eslint-disable-line

fs.writeFileSync(
getIDMapPath(),
JSON.stringify(
Expand Down
88 changes: 68 additions & 20 deletions test/install_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import chaiAsPromised from 'chai-as-promised';
import chaiFs from 'chai-fs';
import { given } from 'mocha-testdata';
import path from 'path';
import { BrowserWindow } from 'electron';
import { BrowserWindow, session } from 'electron';

// Actual Test Imports
import installExtension, { REACT_DEVELOPER_TOOLS } from '../src/';
Expand All @@ -31,20 +31,51 @@ describe('Extension Installer', () => {
it('should upgraded the extension with forceDownload', (done) => {
const extensionName = 'React Developer Tools';
const oldVersion = '0.14.0';
BrowserWindow.removeDevToolsExtension(extensionName);
BrowserWindow.addDevToolsExtension(
path.join(__dirname, 'fixtures/simple_extension'),
).should.be.equal(extensionName);
BrowserWindow.getDevToolsExtensions()[extensionName].version.should.be.equal(oldVersion);

installExtension(REACT_DEVELOPER_TOOLS, true)
.then(() => {
BrowserWindow.getDevToolsExtensions()[extensionName].version.should.not.be.equal(
oldVersion,
);
done();
})
.catch((err) => done(err));
// For Electron >=9.
if (session.defaultSession.removeExtension) {
session.defaultSession.removeExtension(extensionName);
} else {
BrowserWindow.removeDevToolsExtension(extensionName);
}

// For Electron >=9.
if (session.defaultSession.loadExtension) {
session.defaultSession
.loadExtension(path.join(__dirname, 'fixtures/simple_extension'))
.then((ext) => {
ext.name.should.be.equal(extensionName);
session.defaultSession
.getAllExtensions()
.find((e) => e.name === extensionName)
.version.should.be.equal(oldVersion);

installExtension(REACT_DEVELOPER_TOOLS, true)
.then(() => {
session.defaultSession
.getAllExtensions()
.find((e) => e.name === extensionName)
.version.should.not.be.equal(oldVersion);
done();
})
.catch((err) => done(err));
})
.catch((err) => done(err));
} else {
BrowserWindow.addDevToolsExtension(
path.join(__dirname, 'fixtures/simple_extension'),
).should.be.equal(extensionName);
BrowserWindow.getDevToolsExtensions()[extensionName].version.should.be.equal(oldVersion);

installExtension(REACT_DEVELOPER_TOOLS, true)
.then(() => {
BrowserWindow.getDevToolsExtensions()[extensionName].version.should.not.be.equal(
oldVersion,
);
done();
})
.catch((err) => done(err));
}
});
});
});
Expand All @@ -53,10 +84,20 @@ describe('Extension Installer', () => {
it('should resolve the promise and install all of them', (done) => {
installExtension(knownExtensions)
.then(() => {
const installed = BrowserWindow.getDevToolsExtensions();
for (const extension of knownExtensions) {
installed.should.have.property(extension.description);
BrowserWindow.removeDevToolsExtension(extension.description);
// For Electron >=9.
if (session.defaultSession.getAllExtensions) {
const installed = session.defaultSession.getAllExtensions();
for (const extension of knownExtensions) {
installed.map((e) => e.name).should.include(extension.description);
const extensionId = installed.find((e) => e.name === extension.description).id;
session.defaultSession.removeExtension(extensionId);
}
} else {
const installed = BrowserWindow.getDevToolsExtensions();
for (const extension of knownExtensions) {
installed.should.have.property(extension.description);
BrowserWindow.removeDevToolsExtension(extension.description);
}
}
done();
})
Expand All @@ -69,8 +110,15 @@ describe('Extension Installer', () => {
});

afterEach((done) => {
const exts = BrowserWindow.getDevToolsExtensions();
Object.keys(exts).forEach((ext) => BrowserWindow.removeDevToolsExtension(ext));
// For Electron >=9.
if (session.defaultSession.getAllExtensions) {
session.defaultSession
.getAllExtensions()
.forEach((ext) => session.defaultSession.removeExtension(ext.id));
} else {
const exts = BrowserWindow.getDevToolsExtensions();
Object.keys(exts).forEach((ext) => BrowserWindow.removeDevToolsExtension(ext));
}
setTimeout(done, 200);
});
});

0 comments on commit ab713a6

Please sign in to comment.