Skip to content

Commit

Permalink
Require Node.js 18 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 1, 2024
1 parent 865b0d7 commit c98a3d2
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 179 deletions.
4 changes: 0 additions & 4 deletions .github/funding.yml

This file was deleted.

7 changes: 4 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 20
- 18
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
115 changes: 51 additions & 64 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,67 @@
import {BrowserWindow} from 'electron';
import {type BrowserWindow} from 'electron';

declare namespace electronDebug {
interface Options {
/**
Default: [Only in development](https://github.com/sindresorhus/electron-is-dev)
*/
readonly isEnabled?: boolean;

/**
Show DevTools on each created `BrowserWindow`.
@default true
*/
readonly showDevTools?: boolean;

/**
The dock state to open DevTools in.
@default 'previous'
*/
readonly devToolsMode?:
| 'undocked'
| 'right'
| 'bottom'
| 'previous'
| 'detach';
}
}

declare const electronDebug: {
export type Options = {
/**
Install keyboard shortcuts and optionally activate DevTools on each created `BrowserWindow`.
@example
```
import {app, BrowserWindow} from 'electron';
import debug = require('electron-debug');
debug();
let mainWindow;
(async () => {
await app.whenReady();
mainWindow = new BrowserWindow();
});
```
Default: [Only in development](https://github.com/sindresorhus/electron-is-dev)
*/
(options?: electronDebug.Options): void;
readonly isEnabled?: boolean;

/**
Reload the specified `BrowserWindow` instance or the focused one.
Show DevTools on each created `BrowserWindow`.
@param window - Default: `BrowserWindow.getFocusedWindow()`
@default true
*/
refresh(window?: BrowserWindow): void;
readonly showDevTools?: boolean;

/**
Toggle DevTools for the specified `BrowserWindow` instance or the focused one.
The dock state to open DevTools in.
@param window - Default: `BrowserWindow.getFocusedWindow()`
@default 'previous'
*/
devTools(window?: BrowserWindow): void;
readonly devToolsMode?:
| 'undocked'
| 'right'
| 'bottom'
| 'previous'
| 'detach';
};

/**
Open DevTools for the specified `BrowserWindow` instance or the focused one.
/**
Install keyboard shortcuts and optionally activate DevTools on each created `BrowserWindow`.
@param window - Default: `BrowserWindow.getFocusedWindow()`
*/
openDevTools(window?: BrowserWindow): void;
@example
```
import {app, BrowserWindow} from 'electron';
import debug from 'electron-debug';
/**
The absolute path to a preload script to use in [`session#setPreloads()`](https://www.electronjs.org/docs/api/session#sessetpreloadspreloads).
debug();
Use it to enable `devtron` even when [`nodeIntegration`](https://www.electronjs.org/docs/api/browser-window#new-browserwindowoptions) is turned off.
*/
preloadScriptPath: string;
};
let mainWindow;
(async () => {
await app.whenReady();
mainWindow = new BrowserWindow();
});
```
*/
export default function debug(options?: Options): void;

/**
Reload the specified `BrowserWindow` instance or the focused one.
@param window - Default: `BrowserWindow.getFocusedWindow()`
*/
export function refresh(window?: BrowserWindow): void;

/**
Toggle DevTools for the specified `BrowserWindow` instance or the focused one.
@param window - Default: `BrowserWindow.getFocusedWindow()`
*/
export function developmentTools(window?: BrowserWindow): void;

/**
Open DevTools for the specified `BrowserWindow` instance or the focused one.
export = electronDebug;
@param window - Default: `BrowserWindow.getFocusedWindow()`
*/
export function openDevelopmentTools(window?: BrowserWindow): void;
69 changes: 18 additions & 51 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
'use strict';
const {app, BrowserWindow, session} = require('electron');
const path = require('path');
const localShortcut = require('electron-localshortcut');
const isDev = require('electron-is-dev');
import process from 'node:process';
import {app, BrowserWindow} from 'electron';
import localShortcut from 'electron-localshortcut';
import isDev from 'electron-is-dev';

const isMacOS = process.platform === 'darwin';

const devToolsOptions = {};
const developmentToolsOptions = {};

function toggleDevTools(win = BrowserWindow.getFocusedWindow()) {
function toggleDevelopmentTools(win = BrowserWindow.getFocusedWindow()) {
if (win) {
const {webContents} = win;
if (webContents.isDevToolsOpened()) {
webContents.closeDevTools();
} else {
webContents.openDevTools(devToolsOptions);
webContents.openDevTools(developmentToolsOptions);
}
}
}

function devTools(win = BrowserWindow.getFocusedWindow()) {
// eslint-disable-next-line unicorn/prevent-abbreviations
export function devTools(win = BrowserWindow.getFocusedWindow()) {
if (win) {
toggleDevTools(win);
toggleDevelopmentTools(win);
}
}

function openDevTools(win = BrowserWindow.getFocusedWindow()) {
// eslint-disable-next-line unicorn/prevent-abbreviations
export function openDevTools(win = BrowserWindow.getFocusedWindow()) {
if (win) {
win.webContents.openDevTools(devToolsOptions);
win.webContents.openDevTools(developmentToolsOptions);
}
}

function refresh(win = BrowserWindow.getFocusedWindow()) {
export function refresh(win = BrowserWindow.getFocusedWindow()) {
if (win) {
win.webContents.reloadIgnoringCache();
}
Expand All @@ -53,45 +54,20 @@ function inspectElements() {
}
}

const addExtensionIfInstalled = (name, getPath) => {
const isExtensionInstalled = name => {
// For Electron >=9.
if (session.defaultSession.getAllExtensions) {
return {}.hasOwnProperty.call(session.defaultSession.getAllExtensions(), name);
}

// TODO: Remove this when targeting Electron >=9.
return BrowserWindow.getDevToolsExtensions &&
{}.hasOwnProperty.call(BrowserWindow.getDevToolsExtensions(), name);
};

try {
if (!isExtensionInstalled(name)) {
// For Electron >=9.
if (session.defaultSession.loadExtension) {
session.defaultSession.loadExtension(getPath(name));
} else {
// TODO: Remove this when targeting Electron >=9.
BrowserWindow.addDevToolsExtension(getPath(name));
}
}
} catch (_) {}
};

module.exports = options => {
export default function debug(options) {
options = {
isEnabled: null,
showDevTools: true,
devToolsMode: 'previous',
...options
...options,
};

if (options.isEnabled === false || (options.isEnabled === null && !isDev)) {
return;
}

if (options.devToolsMode !== 'previous') {
devToolsOptions.mode = options.devToolsMode;
developmentToolsOptions.mode = options.devToolsMode;
}

app.on('browser-window-created', (event, win) => {
Expand All @@ -106,19 +82,10 @@ module.exports = options => {
(async () => {
await app.whenReady();

addExtensionIfInstalled('devtron', name => require(name).path);
addExtensionIfInstalled('electron-react-devtools', name => require(name).path);

localShortcut.register('CommandOrControl+Shift+C', inspectElements);
localShortcut.register(isMacOS ? 'Command+Alt+I' : 'Control+Shift+I', devTools);
localShortcut.register('F12', devTools);

localShortcut.register('CommandOrControl+R', refresh);
localShortcut.register('F5', refresh);
})();
};

module.exports.refresh = refresh;
module.exports.devTools = devTools;
module.exports.openDevTools = openDevTools;
module.exports.preloadScriptPath = path.join(__dirname, 'preload.js');
}
17 changes: 0 additions & 17 deletions index.test-d.ts

This file was deleted.

26 changes: 16 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": ">=18"
},
"scripts": {
"start": "electron test.js",
"test": "xo && tsd"
"test": "xo"
},
"files": [
"index.js",
"index.d.ts",
"preload.js"
"index.d.ts"
],
"keywords": [
"electron",
Expand All @@ -29,16 +37,14 @@
"development"
],
"dependencies": {
"electron-is-dev": "^1.1.0",
"electron-localshortcut": "^3.1.0"
"electron-is-dev": "^3.0.1",
"electron-localshortcut": "^3.2.1"
},
"devDependencies": {
"@types/node": "^12.0.0",
"devtron": "^1.4.0",
"electron": "^5.0.1",
"@types/node": "^20.12.7",
"electron": "^30.0.1",
"electron-react-devtools": "^0.5.3",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"xo": "^0.58.0"
},
"xo": {
"envs": [
Expand Down
1 change: 0 additions & 1 deletion preload.js

This file was deleted.

29 changes: 6 additions & 23 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,19 @@ Open DevTools and focus the Element Inspector tool.
- Linux: <kbd>Ctrl</kbd> <kbd>Shift</kbd> <kbd>C</kbd>
- Windows: <kbd>Ctrl</kbd> <kbd>Shift</kbd> <kbd>C</kbd>

### Activates DevTools extensions

Just install any of these extension and they'll be activated for you:

- [devtron](https://electronjs.org/devtron) - The official Electron DevTools extension
- You need to use [`preloadScriptPath`](#preloadScriptPath) if the `BrowserWindow`'s `nodeIntegration` is off.
- [electron-react-devtools](https://github.com/firejune/electron-react-devtools) - React DevTools extension for Electron

## Install

```
$ npm install electron-debug
```sh
npm install electron-debug
```

*Requires Electron 5 or later.*
*Requires Electron 30 or later.*

## Usage

```js
const {app, BrowserWindow} = require('electron');
const debug = require('electron-debug');
import {app, BrowserWindow} from 'electron';
import debug from 'electron-debug';

debug();

Expand Down Expand Up @@ -117,21 +109,12 @@ Open DevTools for the specified `BrowserWindow` instance or the focused one.
Type: `BrowserWindow`\
Default: The focused `BrowserWindow`

### preloadScriptPath

Type: `string`

The absolute path to a preload script to use in [`session#setPreloads()`](https://www.electronjs.org/docs/api/session#sessetpreloadspreloads).

Use it to enable `devtron` even when [`nodeIntegration`](https://www.electronjs.org/docs/api/browser-window#new-browserwindowoptions) is turned off.

## Related

- [electron-util](https://github.com/sindresorhus/electron-util) - Useful utilities for developing Electron apps and modules
- [electron-store](https://github.com/sindresorhus/electron-store) - Save and load data like user preferences, app state, cache, etc
- [electron-store](https://github.com/sindresorhus/electron-store) - Save and load data like user settings, app state, cache, etc
- [electron-context-menu](https://github.com/sindresorhus/electron-context-menu) - Context menu for your Electron app
- [electron-dl](https://github.com/sindresorhus/electron-dl) - Simplified file downloads for your Electron app
- [electron-unhandled](https://github.com/sindresorhus/electron-unhandled) - Catch unhandled errors and promise rejections in your Electron app
- [electron-reloader](https://github.com/sindresorhus/electron-reloader) - Simple auto-reloading for Electron apps during development
- [electron-serve](https://github.com/sindresorhus/electron-serve) - Static file serving for Electron apps
- [debug-menu](https://github.com/parro-it/debug-menu) - Chrome-like debug context-menu for Electron
Loading

0 comments on commit c98a3d2

Please sign in to comment.