2020-07-16 07:42:56 +02:00
|
|
|
import Builtin from "../../structs/builtin";
|
|
|
|
import Modals from "../../ui/modals";
|
2020-07-19 01:01:49 +02:00
|
|
|
import {Strings} from "modules";
|
2020-07-16 07:42:56 +02:00
|
|
|
|
|
|
|
const electron = require("electron");
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
|
|
|
|
const BrowserWindow = electron.remote.BrowserWindow;
|
|
|
|
const webContents = electron.remote.getCurrentWebContents();
|
|
|
|
|
|
|
|
export default new class ReactDevTools extends Builtin {
|
|
|
|
get name() {return "ReactDevTools";}
|
|
|
|
get category() {return "developer";}
|
|
|
|
get id() {return "reactDevTools";}
|
|
|
|
|
|
|
|
initialize() {
|
|
|
|
super.initialize();
|
|
|
|
this.findExtension();
|
|
|
|
}
|
|
|
|
|
|
|
|
findExtension() {
|
|
|
|
let extensionPath = "";
|
|
|
|
if (process.platform === "win32") extensionPath = path.resolve(process.env.LOCALAPPDATA, "Google/Chrome/User Data");
|
|
|
|
else if (process.platform === "linux") extensionPath = path.resolve(process.env.HOME, ".config/google-chrome");
|
|
|
|
else if (process.platform === "darwin") extensionPath = path.resolve(process.env.HOME, "Library/Application Support/Google/Chrome");
|
|
|
|
else extensionPath = path.resolve(process.env.HOME, ".config/chromium");
|
|
|
|
extensionPath += "/Default/Extensions/fmkadmapgofadopljbjfkapdkoienihi/";
|
|
|
|
if (fs.existsSync(extensionPath)) {
|
|
|
|
const versions = fs.readdirSync(extensionPath);
|
|
|
|
extensionPath = path.resolve(extensionPath, versions[versions.length - 1]);
|
|
|
|
}
|
|
|
|
this.extensionPath = extensionPath;
|
|
|
|
this.isExtensionInstalled = fs.existsSync(extensionPath);
|
|
|
|
this.listener = this.listener.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
enabled() {
|
|
|
|
if (!this.isExtensionInstalled) this.findExtension();
|
2020-07-19 01:01:49 +02:00
|
|
|
if (!this.isExtensionInstalled) return Modals.alert(Strings.ReactDevTools.notFound, Strings.ReactDevTools.notFoundDetails);
|
2020-07-16 07:42:56 +02:00
|
|
|
setImmediate(() => webContents.on("devtools-opened", this.listener));
|
|
|
|
if (webContents.isDevToolsOpened()) this.listener();
|
|
|
|
}
|
|
|
|
|
|
|
|
disabled() {
|
|
|
|
webContents.removeListener("devtools-opened", this.listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
listener() {
|
|
|
|
if (!this.isExtensionInstalled) return;
|
|
|
|
BrowserWindow.removeDevToolsExtension("React Developer Tools");
|
|
|
|
const didInstall = BrowserWindow.addDevToolsExtension(this.extensionPath);
|
|
|
|
|
|
|
|
if (didInstall) this.log("Successfully installed react devtools.");
|
|
|
|
else this.error("Couldn't find react devtools in chrome extensions!");
|
|
|
|
}
|
|
|
|
};
|