【发布时间】: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
平台信息:
- 操作系统:Ubuntu 18.04.4 LTS
- NodeJS:13.6.0
- NPM:6.14.5
- 电子:8.1.1
- 电子生成器: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