gpt4 book ai didi

javascript - Bash脚本未从Electron App的生产版本中执行

转载 作者:行者123 更新时间:2023-12-03 12:27:49 25 4
gpt4 key购买 nike

UPDATE :添加了index.js文件内容。
我有一个 Electron 应用程序,正在执行一些bash scrips(* .sh )文件来执行某些任务。
一切在开发环境中都可以正常工作,但是在为构建Ubuntu平台deb安装程序的生产版本时,一切正常,例如在应用程序上打开,其他NodeJS东西,但bash脚本未执行。
问题声明:如何在Linux(Ubuntu OS)的 Electron 应用程序的生产版本中执行Shell脚本。得到这个错误

app/terminal_scripts/timer.sh Not Found


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

**项目目录设置**:
项目名
|
应用> CSS |图片| js |渲染
terminal_scripts
node_modules
package.json
package-lock.json
在应用程序目录中的任何地方,我都有所有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"

Permission is set 777 for this shell file



平台信息:
  • 操作系统:Ubuntu 18.04.4 LTS
  • NodeJS:13.6.0
  • NPM:6.14.5
  • Electron :8.1.1
  • Electron 生成器: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();
    });

    最佳答案

    另一种方法是将苍蝇移到app目录之外的新目录,并将其称为extraResources
    在该目录内,您可以添加所有bash文件,并且可以使用以下方法进行生产。let urlPath = path.join(process.resourcesPath, '/extraResources/')然后使用let cmd = `${urlPath}timer.sh`;

    关于javascript - Bash脚本未从Electron App的生产版本中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61672972/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com