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.
¶ 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).
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.
Running the publish commandAdd 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.
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.
You can use macros in the file patterns, artifact file name patterns and publish configuration url:
${arch}
— expanded toia32
,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).
"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
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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With