Coder Social home page Coder Social logo

electron-util's Introduction

electron-util Build Status

Useful utilities for developing Electron apps and modules

You can use this module directly in both the main and renderer process.

Install

$ npm install electron-util

Usage

const {appReady, is} = require('electron-util');

(async () => {
	await appReady;

	console.log(is.macos && is.main);
	//=> true
})();

API

api

Type: Object

Access some Electron APIs in both main and renderer process without having to care about which one you're in:

is

Type: Object

Check for various things:

  • macos - Running on macOS
  • linux - Running on Linux
  • windows - Running on Windows
  • main - Running on the main process
  • renderer - Running on the renderer process
  • usingAsar - The app is using ASAR.
  • development - Running in development, not in production
  • macAppStore - The app is an Mac App Store build
  • windowsStore - The app is a Windows Store AppX build

appReady

Type: Promise

Resolves when the app is ready.

electronVersion

Type: string
Example: 1.7.9

Electron version.

chromeVersion

Type: string
Example: 62.0.3202

Chrome version in Electron.

platform(choices)

Type: Function

Accepts an object with the keys as either macos, windows, linux, or default, and picks the appropriate key depending on the current platform. If platform key is matched, the default key is used if it exists. If the value is a function, it will be executed, and the returned value will be used.

init({
	enableUnicorn: util.platform({
		macos: true,
		windows: false,
		linux: () => false
	})
});

activeWindow()

Type: Function

Returns the active window.

runJS(code, [window])

Type: Function

Run some JavaScript in the active or given window.

Returns a promise for the result of the executed code or a rejected promise if the result is a rejected promise.

fixPathForAsarUnpack(path)

Type: Function

ASAR is great, but it has limitations when it comes to executing binaries. For example, child_process.spawn() is not automatically handled. So you would have to unpack the binary, for example, with the asarUnpack option in electron-builder. This creates a problem as the path to the binary changes, but your path.join(__dirname, 'binary') is not changed. To make it work you need to fix the path. That's the purpose of this method.

Before:

/Users/sindresorhus/Kap.app/Contents/Resources/app.asar/node_modules/foo/binary

After:

/Users/sindresorhus/Kap.app/Contents/Resources/app.asar.unpack/node_modules/foo/binary

Node.js API

This is for non-Electron code that might be included in an Electron app. For example, if you want to add special support for Electron in a vanilla Node.js module.

const electronUtil = require('electron-util/node');

if (electronUtil.isElectron) {
	// Electron workaround
} else {
	// Normal code
}

isElectron

Type: boolean

Check if you're running in an Electron app.

electronVersion

Type: string
Example: 1.7.9

Electron version. Returns 0.0.0 when not in an Electron app.

isUsingAsar

Type: boolean

Check if the Electron app you're running in is using ASAR.

fixPathForAsarUnpack(path)

Same as the above Electron version.

Related

License

MIT © Sindre Sorhus

electron-util's People

Contributors

sindresorhus avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.