Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve or Specify output file name in electron-builder

I am working with electron-builder programmatically to generate installation packages. So far I have this as my utility to create the installation package for the current OS type:

const packagejson = require("../package.json");
const builder = require("electron-builder");
const Platform = builder.Platform;

function buildPromise(){
    //Development package.json
    const devMetadata  = packagejson.electronBuilder;

    //Application package.json
    const appMetadata = {
        name: packagejson.name,
        version: packagejson.version,
        description: packagejson.description,
        author: packagejson.author,
        productName: packagejson.productName
    };

    //Build for the current target and send back promise
    return builder.build({
        projectDir: "./",
        devMetadata,
        appMetadata
    });

}

module.exports = { 
    buildPromise,
    outputPath : packagejson.electronBuilder.directories.output
};

What it does is pull in the needed metadata from the apps MAIN package.json file which contains this section (so the application package.json is empty):

  ...
  "electronBuilder": {
    "build": {
      "productName": "Node App",
      "appId": "my.id",
      "asar": false,
      "win": {
        "iconUrl": "http://localhost:5000/images/logo-multi.ico",
        "target": "nsis"
      },
      "nsis" :{
        "oneClick": false
      }
    },
    "directories": {
      "output": "electron/output",
      "app":"electron/app",
      "buildResources": "electron/buildResources"
    }
  }
  ...

When I run the build in Windows I get a file out called Node App Setup 1.0.0.exe. So far so go. But how do I actually control that final file name? Or at least retrieve that file name programmatically so I can read it in and respond to the client in some way? Obviously, I could piece it together from the json file settings but it I would rather it be more definitive.

like image 326
Ernie S Avatar asked Nov 11 '16 13:11

Ernie S


People also ask

What is Appid Electron builder?

¶ A complete solution to package and build a ready for distribution Electron app for macOS, Windows and Linux with “auto update” support out of the box. NPM packages management: Native application dependencies compilation (including Yarn support).

What does Electron builder do?

electron-builder is a CLI tool that helps us create multi-platform distributions for Electron applications. It supports macOS, Windows, and Linux targets out of the box. It supports multiple file formats for each of these platforms such as dmg , pkg , mas and, mas-dev for macOS.

How do I upload an Electron app to GitHub?

Running the publish command​Add Forge's publish command to your npm scripts. This command will run your configured makers and publish the output distributables to a new GitHub release. By default, this will only publish a single distributable for your host operating system and architecture.


2 Answers

You can specify the output filename using artifactName in the build section of your package.json.

The docs say the artifact file name template supports the ${ext} macro:

${ext} macro is supported in addition to file macros.

File Macros

You can use macros in the file patterns, artifact file name patterns and publish configuration url:

${arch} — expanded to ia32, x64. If no arch, macro will be removed from your pattern with leading space, - and _ (so, you don't need to worry and can reuse pattern).
${os} — expanded to mac, linux or win according to target platform.
${name} – package.json name.
${productName} — Sanitized product name.
${version} — from package.json
${channel} — detected prerelease component from version (e.g. beta).
${env.ENV_NAME} — any environment variable.
Any property of AppInfo (e.g. buildVersion, buildNumber).

Example

"build": {
    "appId": "com.electron.app.my",
    "artifactName": "node-app-${version}.${ext}",
    ...
},

If your package version is 1.0.0, a Windows target would output:

node-app-1.0.0.exe

like image 191
Greg K Avatar answered Sep 19 '22 21:09

Greg K


At my request the author added it to the current version (8.5.1):

https://github.com/electron-userland/electron-builder/issues/899

so now we can do:

builder.build()
    .then(paths => {
        //paths contains an array of export file paths, e.g.:
        console.log(paths[0]); //= c:/MyProject/dist/My Project Setup 1.0.0.exe
        console.log(paths[1]); //= c:/MyProject/dist/myproject-1.0.0-x86_64.AppImage
    });
like image 44
Ernie S Avatar answered Sep 19 '22 21:09

Ernie S