在 electron-builder 中检索或指定输出文件名
Retrieve or Specify output file name in electron-builder
我正在以编程方式使用 electron-builder 来生成安装包。到目前为止,我将此作为我的实用程序来为当前 OS 类型创建安装包:
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
};
它所做的是从包含此部分的应用程序 MAIN package.json
文件中提取所需的元数据(因此应用程序 package.json
为空):
...
"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"
}
}
...
当我 运行 在 Windows 中构建时,我得到一个名为 Node App Setup 1.0.0.exe
的文件。到目前为止。但是我如何实际控制最终文件名呢?或者至少以编程方式检索该文件名,以便我可以读入它并以某种方式响应客户端?显然,我可以从 json 文件设置中拼凑出来,但我希望它更明确。
应作者的要求,作者将其添加到当前版本 (8.5.1) 中:
https://github.com/electron-userland/electron-builder/issues/899
所以现在我们可以做:
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
});
您可以在 package.json
的 build
部分使用 artifactName
指定输出文件名。
文档说 artifact file name template 支持 ${ext}
宏:
${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 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).
例子
"build": {
"appId": "com.electron.app.my",
"artifactName": "node-app-${version}.${ext}",
...
},
如果您的包版本是 1.0.0,Windows 目标将输出:
node-app-1.0.0.exe
我正在以编程方式使用 electron-builder 来生成安装包。到目前为止,我将此作为我的实用程序来为当前 OS 类型创建安装包:
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
};
它所做的是从包含此部分的应用程序 MAIN package.json
文件中提取所需的元数据(因此应用程序 package.json
为空):
...
"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"
}
}
...
当我 运行 在 Windows 中构建时,我得到一个名为 Node App Setup 1.0.0.exe
的文件。到目前为止。但是我如何实际控制最终文件名呢?或者至少以编程方式检索该文件名,以便我可以读入它并以某种方式响应客户端?显然,我可以从 json 文件设置中拼凑出来,但我希望它更明确。
应作者的要求,作者将其添加到当前版本 (8.5.1) 中:
https://github.com/electron-userland/electron-builder/issues/899
所以现在我们可以做:
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
});
您可以在 package.json
的 build
部分使用 artifactName
指定输出文件名。
文档说 artifact file name template 支持 ${ext}
宏:
${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}",
...
},
如果您的包版本是 1.0.0,Windows 目标将输出:
node-app-1.0.0.exe