Use electron-builder built Electron to run a different script


#1

I’m working on an electron app that will also have a CLI. I was thinking similar to how VSCode bundles it’s CLI - create a bundle of the CLI and it’s dependencies, put them in resources and use a script in resources to run with that script as the entry point. For example:

#!/usr/bin/env bash
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.

function realpath() { python -c "import os,sys; print(os.path.realpath(sys.argv[1]))" "$0"; }
CONTENTS="$(dirname "$(dirname "$(dirname "$(dirname "$(realpath "$0")")")")")"
ELECTRON="$CONTENTS/MacOS/Electron"
CLI="$CONTENTS/Resources/app/out/cli.js"
ELECTRON_RUN_AS_NODE=1 "$ELECTRON" "$CLI" "$@"
exit $?

The big difference is that the CLI I’m working on also needs to run in Electron in order to use the networking, common code with the main app that depends on Electron, etc. It never creates a browserWindow, though. That part I have working well in development npx electron main/cli.

Is the something equivalent to ELECTRON_RUN_AS_NODE, like ELECTRON_RUN_AS_ELECTRON=1 (tried; fail) or a way to start the main executable with a different main source file?

The app is built with electron-builder


#2

This ended up being more of a question for electron builder.

The short answer, no.