简体   繁体   English

在电子构建器中检索或指定输出文件名

[英]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): 它的作用是从包含此部分的应用程序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"
    }
  }
  ...

When I run the build in Windows I get a file out called Node App Setup 1.0.0.exe . 当我在Windows中运行构建时,我得到一个名为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. 显然,我可以从json文件设置中将它拼凑在一起,但我宁愿它更明确。

You can specify the output filename using artifactName in the build section of your package.json . 您可以在package.jsonbuild部分中使用artifactName指定输出文件名。

The docs say the artifact file name template supports the ${ext} macro: 文档说工件文件名模板支持${ext}宏:

${ext} macro is supported in addition to file macros . 文件宏外,还支持$ {ext}

File Macros 文件宏

You can use macros in the file patterns, artifact file name patterns and publish configuration url: 您可以在文件模式,工件文件名模式和发布配置URL中使用宏:

${arch} — expanded to ia32 , x64 . ${arch} - 扩展为ia32x64 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. ${os} - 根据目标平台扩展到mac,linux或win。
${name} – package.json name. ${name} - package.json名称。
${productName} — Sanitized product name. ${productName} - 清理产品名称。
${version} — from package.json ${version} - 来自package.json
${channel} — detected prerelease component from version (eg beta). ${channel} - 从版本中检测到的预发布组件(例如beta)。
${env.ENV_NAME} — any environment variable. ${env.ENV_NAME} - 任何环境变量。
Any property of AppInfo (eg buildVersion, buildNumber). AppInfo的任何属性(例如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: 如果您的软件包版本是1.0.0,则会输出Windows目标:

node-app-1.0.0.exe

At my request the author added it to the current version (8.5.1): 根据我的要求,作者将其添加到当前版本(8.5.1):

https://github.com/electron-userland/electron-builder/issues/899 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
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM