首页 > 解决方案 > 打包时的电子写入文件

问题描述

在电子中,使用电子打包器打包应用程序时如何编写文件。

以下将创建和更新开发中的文件。但是一旦我使用 electron-packager 打包应用程序,将不再创建该文件。我需要改变什么?

// imports
const path = require('path');
const fs = require('fs');

// create stream for appending to the log file
stream = fs.createWriteStream(
    path.join(__dirname, 'logfile.log'),
    {
        flags:'a'
    }
);

// append content to the log file
stream.write('test');

我是这样打包的:

  "scripts": {
    "start": "electron .",
    "pack:win64": "electron-packager . my-app --out=dist/win64 --platform=win32 --arch=x64 --icon=assets/icon.png --prune=true --overwrite --asar"
  },

标签: electronelectron-packager

解决方案


我还没有尝试过,但也许你可以使用afterCopy钩子来调用你需要的函数?

复制后

函数数组

将您的应用程序目录复制到临时目录后要调用的函数数组。每个函数都使用五个参数调用:

buildPath (String): The path to the temporary folder where your app has been copied to
electronVersion (String): The version of electron you are packaging for
platform (String): The target platform you are packaging for
arch (String): The target architecture you are packaging for
callback (Function): Must be called once you have completed your actions

const packager = require('electron-packager')
const { serialHooks } = require('electron-packager/hooks')

packager({
  // ...
  afterCopy: [serialHooks([
    (buildPath, electronVersion, platform, arch) => {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          console.log('first function')
          resolve()
        }, 1000)
      })
    },
    (buildPath, electronVersion, platform, arch) => {
      console.log('second function')
    }
  ])],
  // ...
})

推荐阅读