From ab713a639c0ea96ffcdb3970f869de7e05f55ee5 Mon Sep 17 00:00:00 2001 From: Erik Bender Date: Sat, 27 Jun 2020 04:20:23 +0200 Subject: [PATCH] feat: add electron 9 support (#131) --- .circleci/config.yml | 4 ++ src/index.js | 37 ++++++++++++++++--- test/install_spec.js | 88 ++++++++++++++++++++++++++++++++++---------- 3 files changed, 103 insertions(+), 26 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index dd11fc1..37caa1f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -52,6 +52,9 @@ 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 @@ -59,6 +62,7 @@ workflows: - test-electron-6 - test-electron-7 - test-electron-8 + - test-electron-9 filters: branches: only: diff --git a/src/index.js b/src/index.js index 4a48f3c..0959d54 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; @@ -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( diff --git a/test/install_spec.js b/test/install_spec.js index dd12235..8fc5e41 100644 --- a/test/install_spec.js +++ b/test/install_spec.js @@ -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/'; @@ -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)); + } }); }); }); @@ -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(); }) @@ -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); }); });