The mentalist Coder
The mentalist Coder

Reputation: 362

How to read from .env file in electron-builder yaml config file?

I am new to electron. I have an angular application wrapped in electron that I want to build the package/installer using electron-builder. I am using electron-builder-config.yaml file to build the installer.

I would like to know how do I read values from .env environment file into electron-builder-config.yaml file ?

I want to set the version of the package that is generated by command electron-builder -w --publish always -c ./builder-config.yaml.

I did try using buildVersion property but the problem is that there is an installer.nsh file that needs to run as part of nsis installer to set the path and that file uses ${version}.

There is very little documentation on environment variables usage in electron-builder-config.yaml

Here is my electron-builder-config.yaml

directories:
  output: ./dist/electron
  buildResources: ./electron/build
  app: ''
electronVersion: X.Y.Z
appId:  com.sample.app
copyright: "Copyright © 2020 ${author}"
productName: TestApp
forceCodeSigning: true
artifactName: "${productName}-${os}-${version}.${ext}"
files:
  - "**/dist/electron/*"
  - "**/electron/*"
asar: true
compression: maximum
mac:
  category: public.app-category.reference
  icon: "./icon-file.icns"
  publish: [{
      "provider": "generic",
      "url": "http://localhost:8080"
  }]
dmg:
  background: "./build/sample.jpg"
  icon: "./build/nw.icns"
  iconSize: 96
  contents:
    - x: 650
      y: 230
      type: link
      path: /Applications
    - x: 350
      y: 230
      type: file
win:
  cscLink: "./somelink.pfx"
  cscKeyPassword: "XXXXXX"
  target: [nsis]
  icon: "./appinfo.ico"
  publish: [{
      "provider": "generic",
      "url": "http://localhost:8080"
  }]
msi:
  shortcutName: "TestApp - ${version}"
  createDesktopShortcut: true
  createStartMenuShortcut: true
nsis:
  include: "./installer.nsh"
  installerIcon: "./appinfo.ico"
  uninstallerIcon: "./appinfo.ico"
  packElevateHelper: true
  allowToChangeInstallationDirectory: true
  perMachine: true
  oneClick: false
  createDesktopShortcut: true
  createStartMenuShortcut: true
  shortcutName: "TestApp - ${version}"
  guid: "someguid"
npmRebuild: true
nodeGypRebuild: false

Also, I am not sure about the macro ${ext}. From where does this electron-builder-config.yaml file is picking up this value ? Even in the documentation for file-macros, the version does not have the clear definition. Any suggestions ?

Upvotes: 7

Views: 12030

Answers (2)

Navid Shad
Navid Shad

Reputation: 1016

For those who want to read env file directly, use dotenv package:

npm i dotenv
// Inside the main process file
require('dotenv').config();
const GOOGLE_CLOUD_API_KEY = process.env.GOOGLE_CLOUD_API_KEY

Upvotes: 2

The mentalist Coder
The mentalist Coder

Reputation: 362

I got it figured out. In case someone else is looking for the answer to this question, here is how I got it working.

Step 1: Create a file by the name electron-builder.env at the root level where your package.json resides. Please make sure that you keep the file name as electron-builder.env

Step 2: Define the variables that you would like to inside the electron-builder.env file, for example ELECTRON_BUILD_VERSION=99.99

Step 3: Inside your builder-config.yaml file, access the environment variable with the syntax {env.ELECTRON_BUILD_VERSION}

There you go. Have fun. Happy Coding 😊

Upvotes: 13

Related Questions