【问题标题】:Bash Scripts Are Not Executing From Production Build of Electron AppBash 脚本未从 Electron 应用程序的生产构建中执行
【发布时间】:2020-08-23 15:29:25
【问题描述】:

更新:添加了 index.js 文件内容。 我有这个电子应用程序正在执行一些 bash 脚本(*.sh)文件来执行一些任务。

在开发环境中一切正常,但在为 Ubuntu 平台deb 安装程序构建生产版本时,一切正常,例如在应用程序上打开,其他 NodeJS 东西,但 bash 脚本不是正在执行。

问题陈述:如何在 Linux(Ubuntu 操作系统)的电子应用程序的生产版本中执行 shell 脚本。收到此错误

app/terminal_scripts/timer.sh 未找到

以下是该应用的详细说明。


**项目目录设置**:
项目名称
|
应用程序 > CSS |图片 | js |渲染
终端脚本
节点模块
包.json
package-lock.json

在 app 目录中,我有所有 CSS、图像、js、HTML 和终端脚本。

package.json

{
  "name": "timer",
  "productName": "Timely",
  "version": "1.0.25",
  "description": "This desktop app shows you system clock",
  "main": "app/js/main/index.js",
  "scripts": {
    "start": "electron .",
    "test": "echo \"Error: no test specified\" && exit 1",
    "watch": "nodemon --exec 'electron .'",
    "dist": "electron-builder"
  },
  "homepage": ".",
  "keywords": [
    "Electron",
    "Desktop App"
  ],
  "author": "NotABot Ltd <contact@notabot.com>",
  "contributors": [
    {
      "name": "Not A Bot",
      "email": "nab@notabot.com"
    }
  ],
  "license": "ISC",
  "dependencies": {
    "desandro-matches-selector": "^2.0.2",
    "electron-context-menu": "^1.0.0",
    "electron-is": "^3.0.0",
    "fix-path": "^3.0.0",
    "isotope-layout": "^3.0.6",
    "jquery": "^3.5.0",
    "jquery-bridget": "^2.0.1"
  },
  "build": {
    "appId": "com.test.timely",
    "productName": "Timely",
    "linux": {
      "target": "deb",
      "category": "System"
    }
  },
  "devDependencies": {
    "electron": "^8.1.1", 
    "electron-builder": "^22.6.0"
  }
}

HTML

<html>
  <head>
    <title>Timely</title>
  </head>
  <body>
    <button onclick="displayTime()">Display Time</button>
    <textarea rows="20" cols="90" id="command-output" disabled="true"></textarea>
   
    <script>
        const {app} = require('electron');
        function displayTime(){
            console.log("button clicked");
            let cmd = `bash app/terminal_scripts/timer.sh`;
            
            let completeMessage = 'This is the message';
            backgroundProcess(cmd, completeMessage);
        }

        function getCommandOutput() { return document.getElementById("command-output");  };
        function getStatus()      { return document.getElementById("status");  };


        function appendOutput(msg) { getCommandOutput().value += (msg+'\n'); };
        function setStatus(msg)    { getStatus().innerHTML = msg; };
        
        function backgroundProcess(cmd, completeMessage){
            const process = require('child_process');
            
            var child = process.execFile(cmd, [] , {shell: true} );
            appendOutput("Processing......");
            child.on('error', function(err) {
                appendOutput('stderr: '+err );
            });

            child.stdout.on('data', function (data) {
                appendOutput(data);
            });

            child.stderr.on('data', function (data) {
                appendOutput(data );
            });

            return new Promise((resolve, reject) => {
                child.on('close', function (code) {
                    console.log(`code is: ${code}`);
                    if (code == 0){
                        setStatus(completeMessage);
                        resolve(1);  
                    }
                    else{
                        setStatus('Exited with error code ' + code);
                        resolve(-1);
                    }
                });
            });
        }
    </script>

  </body>
</html>


Bash 脚本

#!/bin/bash
timer="$(date)"
echo "$timer" 

此 shell 文件的权限设置为 777



平台信息

  1. 操作系统:Ubuntu 18.04.4 LTS
  2. NodeJS:13.6.0
  3. NPM:6.14.5
  4. 电子:8.1.1
  5. 电子生成器:22.6.0

index.js

const {app, BrowserWindow, Menu, Tray, ipcMain, MenuItem} = require('electron');
const path = require('path');
const contextMenu = require('electron-context-menu');

let splashWindow;


function createMainWindow(){
    mainWindow = new BrowserWindow({
        minHeight: 700,
        minWidth: 800,
        webPreferences: {
            nodeIntegration: true,
            webviewTag: true
        },
        show: false
    });
    //For dev only
    // mainWindow.webContents.openDevTools();
    mainWindow.loadFile('app/renderer/index.html');
    mainWindow.maximize();
}
app.on('ready', () =>{
    createMainWindow();
});

【问题讨论】:

  • 也许您可以粘贴您 package.json 中的 app/js/main/index.js 示例。
  • @Philippe 我已经添加了 index.js 文件,你可以看看,但我不认为这个文件与 shell 脚本的执行有关。
  • 您确定app/terminal_scripts/timer.sh 包含在生产版本中吗?您是如何进行生产构建的?
  • @Philippe 使用电子构建。正如您在我的 index.html 文件中看到的那样,我正在使用 process.execFile 执行 shell 文件,但在生产构建中它显示 app/terminal_scripts_timer.sh 未找到。

标签: javascript node.js bash electron electron-builder


【解决方案1】:

另一种方法是将苍蝇移动到app 目录之外的新目录,并将其命名为extraResources

在该目录中,您可以添加所有 bash 文件,对于生产,您可以使用以下方法。

let urlPath = path.join(process.resourcesPath, '/extraResources/')

然后使用let cmd = `${urlPath}timer.sh`;

【讨论】:

    【解决方案2】:

    我在app 目录旁边创建了一个名为termainal_scripts 的新目录。

    在其中,我有我的 bash 文件 timer.sh

    我通过在path.join() 中使用process.resourcesPath 了解了如何在生产环境中执行shell 脚本。

    所以,让固定路径为:

    let fixedURL = path.join(process.resourcesPath, '/terminal_scripts/');

    那么要执行的命令是:

    let cmd = let cmd = `${fixedURL}timer.sh`

    【讨论】:

      猜你喜欢
      • 2022-10-18
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 2017-11-17
      • 2011-09-12
      • 2016-03-15
      • 1970-01-01
      • 2017-05-03
      相关资源
      最近更新 更多